typescript综合练习1(展开音乐播放列表)

Playlist Soundness

What’s up, friend?!
I’m so pumped you’re joining us.
We’ve got a sick project we could totally use your help on!

See, someone’s giving us amazing recommendations for songs to play.
But they’re not just coming in as songs.
Sometimes they’re an album containing a array of songs.
And sometimes they’re a playlist with a method that returns an array of songs.

We’d like you to write a function for us that takes in an array of those items and returns a result playlist.
The result playlist should keep track of which songs appear under each artist, the in-order list of songs, and the total length of time across the playlist?

Can you do this for us, please?
Friend?

Setup

In one terminal, run the TypeScript compiler via the tsc script.
For example, to start the TypeScript compiler in watch mode:

npm run tsc -- --watch

In another terminal, run Jest via the test script.
For example, to start tests in watch mode:

npm run test -- --watch

Specification

Your code should export an unrollPlaylist function with an explicit return type annotation.

Parameters:

  1. items: An array where each element is either a Song, Album, or Playlist

    See ./index.test.ts for examples of data.

Return type: an object with…

  • artists: Object with artist names keyed to the array of songs they’re credited on
  • songs: An array of Songs
  • time: Total length of time across all songs

Notes

  • Don’t import code from one step into another.

代码

export interface Song {
	artist: string | string[];
	length: number;
	name: string;
	type: "song";
}

export interface Album {
	songs: Song[];
	type: "album";
}

export interface Playlist {
	resolve(): Song[];
	type: "playlist";
}

export type PlaylistItem = Album | Song | Playlist;

export interface Artists {
	[i: string]: string[];
}

export interface UnrolledPlaylist {
	artists: Artists;
	songs: string[];
	time: number;
}

export function unrollPlaylist(items: PlaylistItem[]): UnrolledPlaylist {
	const artists: Artists = {};
	const songs: string[] = [];
	let time = 0;

	function addSong(song: Song) {
		const songArtists =
			typeof song.artist === "string" ? [song.artist] : song.artist;

		for (const artist of songArtists) {
			artists[artist] ??= [];
			artists[artist].push(song.name);
		}

		time += song.length;
		songs.push(song.name);
	}

	for (const item of items) {
		switch (item.type) {
			case "song":
				addSong(item);
				break;

			case "album":
				item.songs.forEach(addSong);
				break;

			case "playlist":
				item.resolve().forEach(addSong);
				break;
		}
	}

	return { artists, songs, time };
}


let items : PlaylistItem[] = [
    {
        songs: [
            {
                artist: ["Queen"],
                length: 223,
                name: "Death On Two Legs (Dedicated to...)",
                type: "song",
            },
            {
                artist: "Tenacious D",
                length: 247,
                name: "Tribute",
                type: "song",
            },
            {
                artist: ["Queen"],
                length: 68,
                name: "Lazing on a Sunday Afternoon",
                type: "song",
            },
        ],
        type: "album",
    },
]

let unrolledPlaylist: UnrolledPlaylist = unrollPlaylist(items)
console.log(unrolledPlaylist)

输出

{
  artists: {
    Queen: [
      'Death On Two Legs (Dedicated to...)',
      'Lazing on a Sunday Afternoon'
    ],
    'Tenacious D': [ 'Tribute' ]
  },
  songs: [
    'Death On Two Legs (Dedicated to...)',
    'Tribute',
    'Lazing on a Sunday Afternoon'
  ],
  time: 538
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值