Skip to content

Commit

Permalink
Rename getArrayOfString to parseIds
Browse files Browse the repository at this point in the history
- move it to helpers
- implement it everywhere
- add tests
  • Loading branch information
gilbarbara committed Nov 19, 2024
1 parent b10c22c commit 2dd8a60
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 24 deletions.
9 changes: 4 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import isEqual from '@gilbarbara/deep-equal';
import memoize from 'memoize-one';

import {
getArrayOfStrings,
getItemImage,
getLocale,
getMergedStyles,
Expand All @@ -13,7 +12,7 @@ import {
getSpotifyURIType,
getTrackInfo,
} from '~/modules/getters';
import { loadSpotifyPlayer, parseVolume, round, validateURI } from '~/modules/helpers';
import { loadSpotifyPlayer, parseIds, parseVolume, round, validateURI } from '~/modules/helpers';
import {
getDevices,
getPlaybackState,
Expand Down Expand Up @@ -219,7 +218,7 @@ class SpotifyWebPlayer extends PureComponent<Props, State> {
uris,
} = this.props;
const isReady = previousState.status !== STATUS.READY && status === STATUS.READY;
const playOptions = this.getPlayOptions(getArrayOfStrings(uris));
const playOptions = this.getPlayOptions(parseIds(uris));

const canPlay = !!currentDeviceId && !!(playOptions.context_uri ?? playOptions.uris);
const shouldPlay = isReady && (autoPlay || playProp);
Expand Down Expand Up @@ -832,7 +831,7 @@ class SpotifyWebPlayer extends PureComponent<Props, State> {
private toggleOffset = async () => {
const { currentDeviceId } = this.state;
const { offset, uris } = this.props;
const playOptions = this.getPlayOptions(getArrayOfStrings(uris));
const playOptions = this.getPlayOptions(parseIds(uris));

if (typeof offset === 'number') {
await play(this.token, { deviceId: currentDeviceId, offset, ...playOptions });
Expand All @@ -843,7 +842,7 @@ class SpotifyWebPlayer extends PureComponent<Props, State> {
const { currentDeviceId, isPlaying, needsUpdate } = this.state;
const { offset, uris } = this.props;
const shouldInitialize = force || needsUpdate;
const playOptions = this.getPlayOptions(getArrayOfStrings(uris));
const playOptions = this.getPlayOptions(parseIds(uris));

try {
if (this.isExternalPlayer) {
Expand Down
12 changes: 2 additions & 10 deletions src/modules/getters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable camelcase */
import { validateURI } from '~/modules/helpers';
import { parseIds, validateURI } from '~/modules/helpers';
import {
getAlbumTracks,
getArtistTopTracks,
Expand Down Expand Up @@ -73,7 +73,7 @@ export async function getPreloadData(
uris: IDs,
offset: number,
): Promise<SpotifyTrack | null> {
const parsedURIs = getArrayOfStrings(uris);
const parsedURIs = parseIds(uris);
const uri = parsedURIs[offset];

if (!validateURI(uri)) {
Expand Down Expand Up @@ -188,11 +188,3 @@ export function getTrackInfo(track: Spotify.Track | SpotifyApi.TrackObjectFull):
uri,
};
}

export function getArrayOfStrings(ids: IDs): string[] {
if (!ids) {
return [];
}

return Array.isArray(ids) ? ids : [ids];
}
9 changes: 9 additions & 0 deletions src/modules/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SPOTIFY_CONTENT_TYPE } from '~/constants';
import { IDs } from '~/types';

export function isNumber(value: unknown): value is number {
return typeof value === 'number';
Expand Down Expand Up @@ -46,6 +47,14 @@ export function millisecondsToTime(input: number) {
return parts.join(':');
}

export function parseIds(ids: IDs): string[] {
if (!ids) {
return [];
}

return Array.isArray(ids) ? ids : [ids];
}

export function parseVolume(value?: unknown): number {
if (!isNumber(value)) {
return 1;
Expand Down
14 changes: 5 additions & 9 deletions src/modules/spotify.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable camelcase */
import { parseIds } from '~/modules/helpers';

import { IDs, RepeatState, SpotifyPlayOptions } from '~/types';

export async function checkTracksStatus(token: string, tracks: IDs): Promise<boolean[]> {
const ids = Array.isArray(tracks) ? tracks : [tracks];

return fetch(`https://api.spotify.com/v1/me/tracks/contains?ids=${ids}`, {
return fetch(`https://api.spotify.com/v1/me/tracks/contains?ids=${parseIds(tracks)}`, {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
Expand Down Expand Up @@ -202,10 +202,8 @@ export async function previous(token: string, deviceId?: string): Promise<void>
}

export async function removeTracks(token: string, tracks: IDs): Promise<void> {
const ids = Array.isArray(tracks) ? tracks : [tracks];

await fetch(`https://api.spotify.com/v1/me/tracks`, {
body: JSON.stringify({ids}),
body: JSON.stringify({ ids: parseIds(tracks) }),
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
Expand All @@ -231,10 +229,8 @@ export async function repeat(token: string, state: RepeatState, deviceId?: strin
}

export async function saveTracks(token: string, tracks: IDs): Promise<void> {
const ids = Array.isArray(tracks) ? tracks : [tracks];

await fetch(`https://api.spotify.com/v1/me/tracks`, {
body: JSON.stringify({ ids }),
body: JSON.stringify({ ids: parseIds(tracks) }),
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
Expand Down
10 changes: 10 additions & 0 deletions test/modules/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
isNumber,
loadSpotifyPlayer,
millisecondsToTime,
parseIds,
parseVolume,
round,
validateURI,
Expand Down Expand Up @@ -41,6 +42,15 @@ describe('millisecondsToTime', () => {
});
});

describe('parseIds', () => {
it('should return properly', () => {
expect(parseIds('sek80pgtykoem9zr189zgyy9')).toEqual(['sek80pgtykoem9zr189zgyy9']);
expect(parseIds(['sek80pgtykoem9zr189zgyy9'])).toEqual(['sek80pgtykoem9zr189zgyy9']);
/* @ts-expect-error - missing parameter */
expect(parseIds()).toEqual([]);
});
});

describe('parseVolume', () => {
it.each([
[0.3, 0.3],
Expand Down

0 comments on commit 2dd8a60

Please sign in to comment.