Skip to content

Commit

Permalink
adding util to extrct props from uri
Browse files Browse the repository at this point in the history
  • Loading branch information
Edwards, Chris authored and Edwards, Chris committed Aug 10, 2017
1 parent 9c142d0 commit 7f1a4ae
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/spotify/index.js
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];
}
};
41 changes: 41 additions & 0 deletions test/spotify/test_extractFromUri.js
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);
});
});
});
2 changes: 1 addition & 1 deletion test/util/test_parse.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { describe, it } = require('mocha');
const { expect, config } = require('chai');

const{ parseFormString } = require('../../src/util/parse');
const { parseFormString } = require('../../src/util/parse');

const context = describe;

Expand Down

0 comments on commit 7f1a4ae

Please sign in to comment.