From 3fac79233c3efa1faf76838b96c66a970be1c5c1 Mon Sep 17 00:00:00 2001 From: Sjoerd Bolten Date: Tue, 12 Mar 2024 23:48:28 +0100 Subject: [PATCH] feat: new types --- src/interfaces/statsfm/stream.ts | 27 +++++++++++++++++++++------ src/interfaces/statsfm/v2/artist.ts | 1 + 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/interfaces/statsfm/stream.ts b/src/interfaces/statsfm/stream.ts index d96dee7..d41f318 100644 --- a/src/interfaces/statsfm/stream.ts +++ b/src/interfaces/statsfm/stream.ts @@ -23,8 +23,8 @@ export interface StreamMinified { h: number; i: number[]; j?: number; - k?: number; - l?: number; + k?: number | string; + l?: number | string; m?: string; } @@ -38,8 +38,8 @@ export const streamToStreamMinified = (stream: Stream): Omit { trackName: stream.g, albumId: stream.h, artistIds: stream.i, - trackReleaseId: stream.k ?? undefined, - albumReleaseId: stream.l ?? undefined, + trackReleaseId: tryParseInt(stream.k), + albumReleaseId: tryParseInt(stream.l), contextId: stream.m ?? undefined }; if ('j' in stream) obj.importId = stream.j; return obj; }; + +export const tryParseInt = (str: string | number | null | undefined): number | null => { + try { + if(typeof str === 'number') return str; + + if (str === null || str === undefined || str.trim() === '') { + return undefined; + } + + const parsedInt = parseInt(str); + return isNaN(parsedInt) ? null : parsedInt; + } catch (e) { + return undefined; + } +}; \ No newline at end of file diff --git a/src/interfaces/statsfm/v2/artist.ts b/src/interfaces/statsfm/v2/artist.ts index 89b3bec..d20fcd2 100644 --- a/src/interfaces/statsfm/v2/artist.ts +++ b/src/interfaces/statsfm/v2/artist.ts @@ -29,6 +29,7 @@ export interface Artist { spotifyId?: string; spotifyPopularity?: number; + spotifyFollowers?: number; spotifyFetchedAt?: Date; appleMusicId?: string;