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

Typescript improvements / code deduplication #29

Merged
merged 1 commit into from
Jul 25, 2023
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
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Running locally
To start contributing, install the packages using `npm install`. To ensure your changes are working, run `npm run build` followed by `npm run test` and all tests should pass.
19 changes: 1 addition & 18 deletions src/album.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
import { encode } from './util'
import SpotifyUri from './spotify-uri'

export default class Album extends SpotifyUri {
public type = 'album'
public id: string

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

public static is (v: any): v is Album {
return Boolean(typeof v === 'object' && v.type === 'album')
}

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

public toURL (): string {
return `/${this.type}/${encode(this.id)}`
return typeof v === 'object' && v.type === 'album'
}
}
21 changes: 2 additions & 19 deletions src/artist.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
import { encode } from './util'
import SpotifyUri from './spotify-uri'

export default class Artist extends SpotifyUri {
public type = 'artist'
public id: string

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

public static is (v: any): v is Artist {
return Boolean(typeof v === 'object' && v.type === 'artist')
}

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

public toURL (): string {
return `/${this.type}/${encode(this.id)}`
return typeof v === 'object' && v.type === 'artist'
}
}
}
19 changes: 1 addition & 18 deletions src/episode.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
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(typeof v === 'object' && v.type === 'episode')
}

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

public toURL (): string {
return `/${this.type}/${encode(this.id)}`
return typeof v === 'object' && v.type === 'episode'
}
}
12 changes: 5 additions & 7 deletions src/local.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { encode } from './util'
import SpotifyUri from './spotify-uri'

import { encode } from './util'
export default class Local extends SpotifyUri {
public type = 'local'
public artist: string
public album: string
public track: string
Expand All @@ -15,22 +13,22 @@ export default class Local extends SpotifyUri {
track: string,
seconds: number
) {
super(uri)
super(uri, "")
this.artist = artist
this.album = album
this.track = track
this.seconds = seconds
}

public static is (v: any): v is Local {
return Boolean(typeof v === 'object' && v.type === 'local')
return typeof v === 'object' && v.type === 'local'
}

public toURI (): string {
return `spotify:local:${encode(this.artist)}:${encode(this.album)}:${encode(this.track)}:${this.seconds}`
return `spotify:${this.type}:${encode(this.artist)}:${encode(this.album)}:${encode(this.track)}:${this.seconds}`
}

public toURL (): string {
return `/local/${encode(this.artist)}/${encode(this.album)}/${encode(this.track)}/${this.seconds}`
return `/${this.type}/${encode(this.artist)}/${encode(this.album)}/${encode(this.track)}/${this.seconds}`
}
}
44 changes: 22 additions & 22 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import User from './user'
import SpotifyUri from './spotify-uri'
import { decode } from './util'
import { ParsedSpotifyUri } from '.'
import { SpotifyTypes } from './types-enum'

/**
* Parses a "Spotify URI".
*
* @param {String} uri
* @param {String} input
* @return {Object} parsed Spotify uri object
* @api public
*/
Expand All @@ -26,7 +27,7 @@ export default function parse (input: string | SpotifyUri): ParsedSpotifyUri {
if (hostname === 'embed.spotify.com') {
const parsedQs = Object.fromEntries(searchParams)
if (typeof parsedQs.uri !== 'string') {
throw new Error('fo')
throw new Error('Parsed query string was not valid: ' + searchParams.toString())
}
return parse(parsedQs.uri)
}
Expand All @@ -46,14 +47,16 @@ export default function parse (input: string | SpotifyUri): ParsedSpotifyUri {
}

function parseParts (uri: string, parts: string[]): ParsedSpotifyUri {
const len = parts.length
if (parts[1] === 'embed') {
let spotifyType = parts[1]
if (spotifyType === SpotifyTypes.Embed) {
parts = parts.slice(1)
spotifyType = parts[1]
}
if (parts[1] === 'search') {
const len = parts.length
if (spotifyType === SpotifyTypes.Search) {
return new Search(uri, decode(parts.slice(2).join(':')))
}
if (len >= 3 && parts[1] === 'local') {
if (len >= 3 && spotifyType === SpotifyTypes.Local) {
return new Local(
uri,
decode(parts[2]),
Expand All @@ -62,35 +65,32 @@ function parseParts (uri: string, parts: string[]): ParsedSpotifyUri {
+parts[5]
)
}
if (len === 3 && parts[1] === 'playlist') {
if (len >= 4 || spotifyType === SpotifyTypes.Playlist) {
if (len >= 5) {
return new Playlist(uri, decode(parts[4]), decode(parts[2]))
}
if (parts[3] === 'starred') {
return new Playlist(uri, 'starred', decode(parts[2]))
}
return new Playlist(uri, decode(parts[2]))
}
if (len === 3 && parts[1] === 'user') {
if (len === 3 && spotifyType === SpotifyTypes.User) {
return new User(uri, decode(parts[2]))
}
if (len >= 5) {
return new Playlist(uri, decode(parts[4]), decode(parts[2]))
}
if (len >= 4 && parts[3] === 'starred') {
return new Playlist(uri, 'starred', decode(parts[2]))
}
if (parts[1] === 'artist') {
if (spotifyType === SpotifyTypes.Artist) {
return new Artist(uri, parts[2])
}
if (parts[1] === 'album') {
if (spotifyType === SpotifyTypes.Album) {
return new Album(uri, parts[2])
}
if (parts[1] === 'track') {
if (spotifyType === SpotifyTypes.Track) {
return new Track(uri, parts[2])
}
if (parts[1] === 'episode') {
if (spotifyType === SpotifyTypes.Episode) {
return new Episode(uri, parts[2])
}
if (parts[1] === 'show') {
if (spotifyType === SpotifyTypes.Show) {
return new Show(uri, parts[2])
}
if (parts[1] === 'playlist') {
return new Playlist(uri, parts[2])
}
throw new TypeError(`Could not determine type for: ${uri}`)
}
10 changes: 3 additions & 7 deletions src/playlist.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import { encode } from './util'
import SpotifyUri from './spotify-uri'

import { encode } from './util'
export default class Playlist extends SpotifyUri {
public type = 'playlist'
public id: string
public user?: string

constructor (uri: string, id: string, user?: string) {
super(uri)
this.id = id
super(uri, id)
if (typeof user === 'string') {
this.user = user
}
}

public static is (v: any): v is Playlist {
return Boolean(typeof v === 'object' && v.type === 'playlist')
return typeof v === 'object' && v.type === 'playlist'
}

public toURI (): string {
Expand Down
20 changes: 3 additions & 17 deletions src/search.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
import { encode } from './util'
import SpotifyUri from './spotify-uri'

export default class Search extends SpotifyUri {
public type = 'search'
public query: string

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

public static is (v: any): v is Search {
return Boolean(typeof v === 'object' && v.type === 'search')
}

public toURI (): string {
return `spotify:search:${encode(this.query)}`
}

public toURL (): string {
return `/search/${encode(this.query)}`
return typeof v === 'object' && v.type === 'search'
}
}
19 changes: 1 addition & 18 deletions src/show.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
import { encode } from './util'
import SpotifyUri from './spotify-uri'

export default class Show extends SpotifyUri {
public type = 'show'
public id: string

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

public static is (v: any): v is Show {
return Boolean(typeof v === 'object' && v.type === 'show')
}

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

public toURL (): string {
return `/${this.type}/${encode(this.id)}`
return typeof v === 'object' && v.type === 'show'
}
}
25 changes: 19 additions & 6 deletions src/spotify-uri.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import { SpotifyTypes } from './types-enum';
import { encode } from './util'

export default abstract class SpotifyUri {
public uri: string
public abstract toURL (): string
public abstract toURI (): string
public type: SpotifyTypes;
public id: string;
public uri: string;

constructor (uri: string) {
this.uri = uri
constructor (uri: string, id : string) {
this.uri = uri;
this.id = id;
this.type = this.constructor.name.toLowerCase() as SpotifyTypes;
}

public static is (v: any): v is SpotifyUri {
return Boolean(typeof v === 'object' && typeof v.uri === 'string')
return typeof v === 'object' && typeof v.uri === 'string'
}

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

public toURL (): string {
return `/${this.type}/${encode(this.id)}`
}

public toEmbedURL (): string {
Expand Down
21 changes: 2 additions & 19 deletions src/track.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
import { encode } from './util'
import SpotifyUri from './spotify-uri'

export default class Track extends SpotifyUri {
public type = 'track'
public id: string

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

public static is (v: any): v is Track {
return Boolean(typeof v === 'object' && v.type === 'track')
}

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

public toURL (): string {
return `/${this.type}/${encode(this.id)}`
return typeof v === 'object' && v.type === 'track'
}
}
}
12 changes: 12 additions & 0 deletions src/types-enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export enum SpotifyTypes {
Album = 'album',
Artist = 'artist',
Episode = 'episode',
Local = 'local',
Playlist = 'playlist',
Search = 'search',
Show = 'show',
Track = 'track',
User = 'user',
Embed = 'embed'
}
22 changes: 4 additions & 18 deletions src/user.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
import { encode } from './util'
import SpotifyUri from './spotify-uri'

export default class User extends SpotifyUri {
public type = 'user'
public user: string

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

public static is (v: any): v is User {
return Boolean(typeof v === 'object' && v.type === 'user')
}

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

public toURL (): string {
return `/${this.type}/${encode(this.user)}`
return typeof v === 'object' && v.type === 'user'
}
}
}
Loading