mirror of
https://github.com/maciejpedzich/playlist-entry-validator.git
synced 2024-11-28 00:15:48 +01:00
20 lines
444 B
TypeScript
20 lines
444 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;
|
||
|
|
||
|
if (!isValidPlaylistUrl) return null;
|
||
|
|
||
|
return playlistId;
|
||
|
} catch {
|
||
|
return null;
|
||
|
}
|
||
|
};
|