Skip to content

Commit 7b030fc

Browse files
committed
Rename getArrayOfString to parseIds
- move it to helpers - implement it everywhere - add tests
1 parent 93836f9 commit 7b030fc

File tree

5 files changed

+30
-24
lines changed

5 files changed

+30
-24
lines changed

src/index.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import isEqual from '@gilbarbara/deep-equal';
44
import memoize from 'memoize-one';
55

66
import {
7-
getArrayOfStrings,
87
getItemImage,
98
getLocale,
109
getMergedStyles,
@@ -13,7 +12,7 @@ import {
1312
getSpotifyURIType,
1413
getTrackInfo,
1514
} from '~/modules/getters';
16-
import { loadSpotifyPlayer, parseVolume, round, validateURI } from '~/modules/helpers';
15+
import { loadSpotifyPlayer, parseIds, parseVolume, round, validateURI } from '~/modules/helpers';
1716
import {
1817
getDevices,
1918
getPlaybackState,
@@ -219,7 +218,7 @@ class SpotifyWebPlayer extends PureComponent<Props, State> {
219218
uris,
220219
} = this.props;
221220
const isReady = previousState.status !== STATUS.READY && status === STATUS.READY;
222-
const playOptions = this.getPlayOptions(getArrayOfStrings(uris));
221+
const playOptions = this.getPlayOptions(parseIds(uris));
223222

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

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

848847
try {
849848
if (this.isExternalPlayer) {

src/modules/getters.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable camelcase */
2-
import { validateURI } from '~/modules/helpers';
2+
import { parseIds, validateURI } from '~/modules/helpers';
33
import {
44
getAlbumTracks,
55
getArtistTopTracks,
@@ -73,7 +73,7 @@ export async function getPreloadData(
7373
uris: IDs,
7474
offset: number,
7575
): Promise<SpotifyTrack | null> {
76-
const parsedURIs = getArrayOfStrings(uris);
76+
const parsedURIs = parseIds(uris);
7777
const uri = parsedURIs[offset];
7878

7979
if (!validateURI(uri)) {
@@ -188,11 +188,3 @@ export function getTrackInfo(track: Spotify.Track | SpotifyApi.TrackObjectFull):
188188
uri,
189189
};
190190
}
191-
192-
export function getArrayOfStrings(ids: IDs): string[] {
193-
if (!ids) {
194-
return [];
195-
}
196-
197-
return Array.isArray(ids) ? ids : [ids];
198-
}

src/modules/helpers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SPOTIFY_CONTENT_TYPE } from '~/constants';
2+
import { IDs } from '~/types';
23

34
export function isNumber(value: unknown): value is number {
45
return typeof value === 'number';
@@ -46,6 +47,14 @@ export function millisecondsToTime(input: number) {
4647
return parts.join(':');
4748
}
4849

50+
export function parseIds(ids: IDs): string[] {
51+
if (!ids) {
52+
return [];
53+
}
54+
55+
return Array.isArray(ids) ? ids : [ids];
56+
}
57+
4958
export function parseVolume(value?: unknown): number {
5059
if (!isNumber(value)) {
5160
return 1;

src/modules/spotify.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* eslint-disable camelcase */
2+
import { parseIds } from '~/modules/helpers';
3+
24
import { IDs, RepeatState, SpotifyPlayOptions } from '~/types';
35

46
export async function checkTracksStatus(token: string, tracks: IDs): Promise<boolean[]> {
5-
const ids = Array.isArray(tracks) ? tracks : [tracks];
6-
7-
return fetch(`https://api.spotify.com/v1/me/tracks/contains?ids=${ids}`, {
7+
return fetch(`https://api.spotify.com/v1/me/tracks/contains?ids=${parseIds(tracks)}`, {
88
headers: {
99
Authorization: `Bearer ${token}`,
1010
'Content-Type': 'application/json',
@@ -202,10 +202,8 @@ export async function previous(token: string, deviceId?: string): Promise<void>
202202
}
203203

204204
export async function removeTracks(token: string, tracks: IDs): Promise<void> {
205-
const ids = Array.isArray(tracks) ? tracks : [tracks];
206-
207205
await fetch(`https://api.spotify.com/v1/me/tracks`, {
208-
body: JSON.stringify({ids}),
206+
body: JSON.stringify({ ids: parseIds(tracks) }),
209207
headers: {
210208
Authorization: `Bearer ${token}`,
211209
'Content-Type': 'application/json',
@@ -231,10 +229,8 @@ export async function repeat(token: string, state: RepeatState, deviceId?: strin
231229
}
232230

233231
export async function saveTracks(token: string, tracks: IDs): Promise<void> {
234-
const ids = Array.isArray(tracks) ? tracks : [tracks];
235-
236232
await fetch(`https://api.spotify.com/v1/me/tracks`, {
237-
body: JSON.stringify({ ids }),
233+
body: JSON.stringify({ ids: parseIds(tracks) }),
238234
headers: {
239235
Authorization: `Bearer ${token}`,
240236
'Content-Type': 'application/json',

test/modules/helpers.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
isNumber,
33
loadSpotifyPlayer,
44
millisecondsToTime,
5+
parseIds,
56
parseVolume,
67
round,
78
validateURI,
@@ -41,6 +42,15 @@ describe('millisecondsToTime', () => {
4142
});
4243
});
4344

45+
describe('parseIds', () => {
46+
it('should return properly', () => {
47+
expect(parseIds('sek80pgtykoem9zr189zgyy9')).toEqual(['sek80pgtykoem9zr189zgyy9']);
48+
expect(parseIds(['sek80pgtykoem9zr189zgyy9'])).toEqual(['sek80pgtykoem9zr189zgyy9']);
49+
/* @ts-expect-error - missing parameter */
50+
expect(parseIds()).toEqual([]);
51+
});
52+
});
53+
4454
describe('parseVolume', () => {
4555
it.each([
4656
[0.3, 0.3],

0 commit comments

Comments
 (0)