mirror of
https://github.com/maciejpedzich/spotifyplaylistarchive.com.git
synced 2024-11-13 00:33:03 +01:00
18 lines
429 B
TypeScript
18 lines
429 B
TypeScript
|
export const getPlaylistIdFromUrl = (url: string) => {
|
||
|
try {
|
||
|
const urlObject = new URL(url);
|
||
|
const [collectionName, playlistId] = urlObject.pathname
|
||
|
.split('/')
|
||
|
.filter(Boolean);
|
||
|
|
||
|
const isValidPlaylistUrl =
|
||
|
urlObject.hostname === 'open.spotify.com' &&
|
||
|
collectionName === 'playlist' &&
|
||
|
playlistId;
|
||
|
|
||
|
return isValidPlaylistUrl ? playlistId : null;
|
||
|
} catch {
|
||
|
return null;
|
||
|
}
|
||
|
};
|