Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the new episode URI formats #10

Merged
merged 1 commit into from
Jul 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/episode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { encode } from './util';
import SpotifyUri from './spotify-uri';

export default class Episode extends SpotifyUri {
public type = 'episode';
public id: string;

constructor(uri: string, id: string) {
super(uri);
this.id = id;
}

public static is(v: any): v is Episode {
return Boolean(v && v.type === 'episode');
}

public toURI(): string {
return `spotify:${this.type}:${encode(this.id)}`;
}

public toURL(): string {
return `/${this.type}/${encode(this.id)}`;
}
}
4 changes: 4 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Playlist from './playlist';
import Artist from './artist';
import Album from './album';
import Track from './track';
import Episode from './episode';
import User from './user';
import SpotifyUri from './spotify-uri';
import { decode } from './util';
Expand Down Expand Up @@ -82,5 +83,8 @@ function parseParts(uri: string, parts: string[]): ParsedSpotifyUri {
if (parts[1] === 'track') {
return new Track(uri, parts[2]);
}
if (parts[1] === 'episode') {
return new Episode(uri, parts[2]);
}
throw new TypeError(`Could not determine type for: ${uri}`);
}
19 changes: 19 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ describe('parse()', function() {
assert.equal('track', obj.type);
assert.equal('10M2REwwztVxgr0szw7UwD', obj.id);
});
it('should parse "episode" URLs', function() {
let url = 'http://open.spotify.com/episode/64TORH3xleuD1wcnFsrH1E';
let obj = parse(url);
assert.equal('episode', obj.type);
assert.equal('64TORH3xleuD1wcnFsrH1E', obj.id);
});
it('should parse user "playlist" URLs', function() {
let url =
'http://open.spotify.com/user/tootallnate/playlist/0Lt5S4hGarhtZmtz7BNTeX';
Expand Down Expand Up @@ -112,6 +118,12 @@ describe('parse()', function() {
assert.equal('track', obj.type);
assert.equal('5CMjjywI0eZMixPeqNd75R', obj.id);
});
it('should parse "episode" URIs', function() {
let uri = 'spotify:episode:64TORH3xleuD1wcnFsrH1E';
let obj = parse(uri);
assert.equal('episode', obj.type);
assert.equal('64TORH3xleuD1wcnFsrH1E', obj.id);
});
it('should parse user "playlist" URIs', function() {
let uri =
'spotify:user:daftpunkofficial:playlist:6jP6EcvAwqNksccDkIe6hX';
Expand Down Expand Up @@ -248,6 +260,13 @@ describe('formatOpenURL()', function() {
let actual = formatOpenURL(obj);
assert.equal(actual, expected);
});
it('should format "episode" URIs', function() {
let uri = 'spotify:episode:64TORH3xleuD1wcnFsrH1E';
let obj = parse(uri);
let expected = 'http://open.spotify.com/episode/64TORH3xleuD1wcnFsrH1E';
let actual = formatOpenURL(obj);
assert.equal(actual, expected);
});
it('should format user "playlist" URIs', function() {
let uri =
'spotify:user:daftpunkofficial:playlist:6jP6EcvAwqNksccDkIe6hX';
Expand Down