This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e11070a
commit 773f743
Showing
6 changed files
with
174 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { parseCollectionRoute } from './collectionRouteParser' | ||
// eslint-disable-next-line | ||
import { mockDecode } from '__mocks__/Hashids' | ||
|
||
describe('parseCollectionRoute', () => { | ||
it('can decode a playlist id route', () => { | ||
const route = '/arizmendi/playlist/croissants-11' | ||
const { | ||
title, | ||
collectionId, | ||
handle, | ||
collectionType | ||
} = parseCollectionRoute(route) | ||
expect(title).toEqual('croissants') | ||
expect(collectionId).toEqual(11) | ||
expect(handle).toEqual('arizmendi') | ||
expect(collectionType).toEqual('playlist') | ||
}) | ||
|
||
it('can decode an album id route', () => { | ||
const route = '/arizmendi/album/scones-20' | ||
const { | ||
title, | ||
collectionId, | ||
handle, | ||
collectionType | ||
} = parseCollectionRoute(route) | ||
expect(title).toEqual('scones') | ||
expect(collectionId).toEqual(20) | ||
expect(handle).toEqual('arizmendi') | ||
expect(collectionType).toEqual('album') | ||
}) | ||
|
||
it('can decode a hashed collection id route', () => { | ||
mockDecode.mockReturnValue([11845]) | ||
|
||
const route = '/playlists/eP9k7' | ||
const { | ||
title, | ||
collectionId, | ||
handle, | ||
collectionType | ||
} = parseCollectionRoute(route) | ||
expect(title).toEqual(null) | ||
expect(collectionId).toEqual(11845) | ||
expect(handle).toEqual(null) | ||
expect(collectionType).toEqual(null) | ||
}) | ||
|
||
it('returns null for invalid id in playlist id route', () => { | ||
const route = '/arizmendi/playlist/name-asdf' | ||
const params = parseCollectionRoute(route) | ||
expect(params).toEqual(null) | ||
}) | ||
|
||
it('returns null for invalid id in album id route', () => { | ||
const route = '/arizmendi/album/name-asdf' | ||
const params = parseCollectionRoute(route) | ||
expect(params).toEqual(null) | ||
}) | ||
|
||
it('returns null for invalid id in hashed collection id route', () => { | ||
mockDecode.mockReturnValue([NaN]) | ||
|
||
const route = '/playlists/asdf' | ||
const params = parseCollectionRoute(route) | ||
expect(params).toEqual(null) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { matchPath } from 'react-router-dom' | ||
import { PLAYLIST_PAGE, ALBUM_PAGE, PLAYLIST_ID_PAGE } from 'utils/route' | ||
import { decodeHashId } from './hashIds' | ||
|
||
/** | ||
* Parses a collection route into handle, title, id, and type | ||
* If the route is a hash id route, title, handle, and type are not returned | ||
* @param route | ||
*/ | ||
export const parseCollectionRoute = (route: string) => { | ||
const collectionIdPageMatch = matchPath<{ id: string }>(route, { | ||
path: PLAYLIST_ID_PAGE, | ||
exact: true | ||
}) | ||
if (collectionIdPageMatch) { | ||
const collectionId = decodeHashId(collectionIdPageMatch.params.id) | ||
if (!collectionId || isNaN(collectionId)) return null | ||
return { collectionId, handle: null, collectionType: null, title: null } | ||
} | ||
|
||
const playlistPageMatch = matchPath<{ | ||
handle: string | ||
playlistName: string | ||
}>(route, { | ||
path: PLAYLIST_PAGE, | ||
exact: true | ||
}) | ||
if (playlistPageMatch) { | ||
const { handle, playlistName } = playlistPageMatch.params | ||
const nameParts = playlistName.split('-') | ||
const title = nameParts.slice(0, nameParts.length - 1).join('-') | ||
const collectionId = parseInt(nameParts[nameParts.length - 1], 10) | ||
if (!collectionId || isNaN(collectionId)) return null | ||
return { title, collectionId, handle, collectionType: 'playlist' } | ||
} | ||
|
||
const albumPageMatch = matchPath<{ | ||
handle: string | ||
albumName: string | ||
}>(route, { | ||
path: ALBUM_PAGE, | ||
exact: true | ||
}) | ||
if (albumPageMatch) { | ||
const { handle, albumName } = albumPageMatch.params | ||
const nameParts = albumName.split('-') | ||
const title = nameParts.slice(0, nameParts.length - 1).join('-') | ||
const collectionId = parseInt(nameParts[nameParts.length - 1], 10) | ||
if (!collectionId || isNaN(collectionId)) return null | ||
return { title, collectionId, handle, collectionType: 'album' } | ||
} | ||
|
||
return null | ||
} |