Skip to content

Commit f929607

Browse files
committed
refactor(provider): moved label splitting code into utility function splitLabels
1 parent 7d0f1dd commit f929607

File tree

4 files changed

+43
-20
lines changed

4 files changed

+43
-20
lines changed

providers/Deezer/mod.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { availableRegions } from './regions.ts';
22
import { type CacheEntry, MetadataApiProvider, type ProviderOptions, ReleaseApiLookup } from '@/providers/base.ts';
33
import { DurationPrecision, FeatureQuality, FeatureQualityMap } from '@/providers/features.ts';
44
import { parseHyphenatedDate, PartialDate } from '@/utils/date.ts';
5+
import { splitLabels } from '@/utils/label.ts';
56
import { ResponseError } from '@/utils/errors.ts';
67
import { formatGtin } from '@/utils/gtin.ts';
78

@@ -174,10 +175,7 @@ export class DeezerReleaseLookup extends ReleaseApiLookup<DeezerProvider, Releas
174175
}],
175176
media,
176177
releaseDate: parseHyphenatedDate(rawRelease.release_date),
177-
// split label string using slashes if the results have at least 3 characters
178-
labels: rawRelease.label.split(/(?<=[^/]{3,})\/(?=[^/]{3,})/).map((label) => ({
179-
name: label.trim(),
180-
})),
178+
labels: splitLabels(rawRelease.label),
181179
status: 'Official',
182180
packaging: 'None',
183181
images: [{

providers/Spotify/mod.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ApiAccessToken, type CacheEntry, MetadataApiProvider, ReleaseApiLookup } from '@/providers/base.ts';
22
import { DurationPrecision, FeatureQuality, FeatureQualityMap } from '@/providers/features.ts';
33
import { parseHyphenatedDate, PartialDate } from '@/utils/date.ts';
4+
import { splitLabels } from '@/utils/label.ts';
45
import { ResponseError } from '@/utils/errors.ts';
56
import { selectLargestImage } from '@/utils/image.ts';
67
import { encodeBase64 } from 'std/encoding/base64.ts';
@@ -17,14 +18,7 @@ import type {
1718
Track,
1819
TrackList,
1920
} from './api_types.ts';
20-
import type {
21-
ArtistCreditName,
22-
EntityId,
23-
HarmonyMedium,
24-
HarmonyRelease,
25-
HarmonyTrack,
26-
Label,
27-
} from '@/harmonizer/types.ts';
21+
import type { ArtistCreditName, EntityId, HarmonyMedium, HarmonyRelease, HarmonyTrack } from '@/harmonizer/types.ts';
2822

2923
// See https://developer.spotify.com/documentation/web-api
3024

@@ -235,7 +229,7 @@ export class SpotifyReleaseLookup extends ReleaseApiLookup<SpotifyProvider, Albu
235229
status: 'Official',
236230
packaging: 'None',
237231
images: artwork ? [artwork] : [],
238-
labels: this.getLabels(rawRelease),
232+
labels: splitLabels(rawRelease.label),
239233
availableIn: rawRelease.available_markets,
240234
info: this.generateReleaseInfo(),
241235
};
@@ -292,13 +286,6 @@ export class SpotifyReleaseLookup extends ReleaseApiLookup<SpotifyProvider, Albu
292286
};
293287
}
294288

295-
private getLabels(rawRelease: Album): Label[] {
296-
// split label string using slashes if the results have at least 3 characters
297-
return rawRelease.label?.split(/(?<=[^/]{3,})\/(?=[^/]{3,})/).map((label) => ({
298-
name: label.trim(),
299-
}));
300-
}
301-
302289
private getCopyright(copyrights: Copyright[]): string {
303290
return copyrights.map(this.formatCopyright).join('\n');
304291
}

utils/label.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { splitLabels } from './label.ts';
2+
3+
import { assertEquals } from 'std/assert/assert_equals.ts';
4+
import { describe, it } from 'std/testing/bdd.ts';
5+
6+
import type { FunctionSpec } from './test_spec.ts';
7+
8+
describe('GTIN validator', () => {
9+
const passingCases: FunctionSpec<typeof splitLabels> = [
10+
['single label', 'Nuclear Blast', [{ name: 'Nuclear Blast' }]],
11+
['multiple labels', 'Roc Nation/RocAFella/IDJ', [{ name: 'Roc Nation' }, { name: 'RocAFella' }, { name: 'IDJ' }]],
12+
['multiple labels, trim values', 'Roc Nation / RocAFella / IDJ', [{ name: 'Roc Nation' }, { name: 'RocAFella' }, {
13+
name: 'IDJ',
14+
}]],
15+
['short label', 'A/B', [{ name: 'A/B' }]],
16+
['two labels minimal length', 'ABC/DEF', [{ name: 'ABC' }, { name: 'DEF' }]],
17+
['two labels, one short', 'A/BCD/EFG', [{ name: 'A/BCD' }, { name: 'EFG' }]],
18+
];
19+
20+
passingCases.forEach(([description, input, expected]) => {
21+
it(`passes for valid ${description}`, () => {
22+
assertEquals(splitLabels(input), expected);
23+
});
24+
});
25+
});

utils/label.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Label } from '@/harmonizer/types.ts';
2+
3+
/**
4+
* Split label string using slashes if the results have at least 3 characters.
5+
*
6+
* @param labels String containing one or more label name.
7+
* @returns List of `Label` entries
8+
*/
9+
export function splitLabels(labels: string): Label[] {
10+
return labels?.split(/(?<=[^/]{3,})\/(?=[^/]{3,})/).map((label) => ({
11+
name: label.trim(),
12+
}));
13+
}

0 commit comments

Comments
 (0)