Skip to content

Commit

Permalink
adding a util to convert spotify links to uris
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdevwords authored and chrisdevwords committed Aug 13, 2017
1 parent d7c2b09 commit 56b32f6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/spotify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ module.exports = {
API_BASE,
ERROR_EXPIRED_TOKEN,
ERROR_INVALID_TRACK_URI,
convertLinkToUri(link) {
return link
.replace('https://open.spotify.com', 'spotify')
.split('/').join(':');
},
extractFromUri(uri, property) {
const arr = uri.split(':');
const propIndex = arr.indexOf(property);
Expand Down
35 changes: 35 additions & 0 deletions test/spotify/test_convertLinkToUri.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const {describe, it} = require('mocha');
const { expect, config } = require('chai');
const { convertLinkToUri } = require('../../src/spotify');


const context = describe;

config.includeStack = true;

describe('#spotify.convertLinkToUri', () => {

context('with an http link to a spotify track', () => {
it('converts it to a uri', () => {
const link = 'https://open.spotify.com/track/5uSXPDwXLpYF7aLmTnB3Mb';
const uri = convertLinkToUri(link);
expect(uri).to.equal('spotify:track:5uSXPDwXLpYF7aLmTnB3Mb');
});
});

context('with a uri to a spotify track', () => {
it('returns the uri as is', () => {
const link = 'spotify:track:5uSXPDwXLpYF7aLmTnB3Mb';
const uri = convertLinkToUri(link);
expect(uri).to.equal('spotify:track:5uSXPDwXLpYF7aLmTnB3Mb');
});
});

context('with a string that is not a spotify link', () => {
it('doesn\'t break', () => {
const link = 'https://developer.spotify.com/web-api/get-track/';
const uri = convertLinkToUri(link);
expect(uri).to.be.a('String');
});
});
});
2 changes: 1 addition & 1 deletion test/spotify/test_extractFromUri.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { extractFromUri } = require('../../src/spotify');
const context = describe;


describe('The extractFromUri util', () => {
describe('#spotify.extractFromUri', () => {
context('With a link to a playlist', () => {

const uri = 'spotify:user:awpoops:playlist:5PP1I2m0uxEBb3VKLhI7bP';
Expand Down

0 comments on commit 56b32f6

Please sign in to comment.