-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding util to extrct props from uri
- Loading branch information
Edwards, Chris
authored and
Edwards, Chris
committed
Aug 10, 2017
1 parent
9c142d0
commit 7f1a4ae
Showing
3 changed files
with
52 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
const API_BASE = 'https://api.spotify.com/'; | ||
|
||
module.exports = { | ||
API_BASE | ||
API_BASE, | ||
|
||
extractFromUri(uri, property) { | ||
const arr = uri.split(':'); | ||
const propIndex = arr.indexOf(property); | ||
if (propIndex === -1) { | ||
return undefined; | ||
} | ||
return arr[propIndex + 1]; | ||
} | ||
}; |
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,41 @@ | ||
const {describe, it} = require('mocha'); | ||
const { expect, config } = require('chai'); | ||
const { extractFromUri } = require('../../src/spotify'); | ||
|
||
const context = describe; | ||
|
||
|
||
describe('The extractFromUri util', () => { | ||
context('With a link to a playlist', () => { | ||
|
||
const uri = 'spotify:user:awpoops:playlist:5PP1I2m0uxEBb3VKLhI7bP'; | ||
|
||
it('can extract the playlist id', () => { | ||
const playlistId = extractFromUri(uri, 'playlist'); | ||
expect(playlistId).to.eq('5PP1I2m0uxEBb3VKLhI7bP'); | ||
}); | ||
|
||
it('can extract the user id', () => { | ||
const userId = extractFromUri(uri, 'user'); | ||
expect(userId).to.eq('awpoops'); | ||
}); | ||
}); | ||
|
||
context('with an invalid string', () => { | ||
const uri = 'foo:bar:baz'; | ||
|
||
it('returns an undefined for playlist', () => { | ||
const playlistId = extractFromUri(uri, 'playlist'); | ||
expect(playlistId).to.eq(undefined); | ||
}); | ||
}); | ||
|
||
context('with an empty string', () => { | ||
const uri = ''; | ||
|
||
it('returns an undefined for playlist', () => { | ||
const playlistId = extractFromUri(uri, 'playlist'); | ||
expect(playlistId).to.eq(undefined); | ||
}); | ||
}); | ||
}); |
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