Skip to content

Commit

Permalink
refactor(provider): moved label splitting code into utility function …
Browse files Browse the repository at this point in the history
…splitLabels
  • Loading branch information
phw committed Jun 9, 2024
1 parent 7d0f1dd commit f929607
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
6 changes: 2 additions & 4 deletions providers/Deezer/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { availableRegions } from './regions.ts';
import { type CacheEntry, MetadataApiProvider, type ProviderOptions, ReleaseApiLookup } from '@/providers/base.ts';
import { DurationPrecision, FeatureQuality, FeatureQualityMap } from '@/providers/features.ts';
import { parseHyphenatedDate, PartialDate } from '@/utils/date.ts';
import { splitLabels } from '@/utils/label.ts';
import { ResponseError } from '@/utils/errors.ts';
import { formatGtin } from '@/utils/gtin.ts';

Expand Down Expand Up @@ -174,10 +175,7 @@ export class DeezerReleaseLookup extends ReleaseApiLookup<DeezerProvider, Releas
}],
media,
releaseDate: parseHyphenatedDate(rawRelease.release_date),
// split label string using slashes if the results have at least 3 characters
labels: rawRelease.label.split(/(?<=[^/]{3,})\/(?=[^/]{3,})/).map((label) => ({
name: label.trim(),
})),
labels: splitLabels(rawRelease.label),
status: 'Official',
packaging: 'None',
images: [{
Expand Down
19 changes: 3 additions & 16 deletions providers/Spotify/mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApiAccessToken, type CacheEntry, MetadataApiProvider, ReleaseApiLookup } from '@/providers/base.ts';
import { DurationPrecision, FeatureQuality, FeatureQualityMap } from '@/providers/features.ts';
import { parseHyphenatedDate, PartialDate } from '@/utils/date.ts';
import { splitLabels } from '@/utils/label.ts';
import { ResponseError } from '@/utils/errors.ts';
import { selectLargestImage } from '@/utils/image.ts';
import { encodeBase64 } from 'std/encoding/base64.ts';
Expand All @@ -17,14 +18,7 @@ import type {
Track,
TrackList,
} from './api_types.ts';
import type {
ArtistCreditName,
EntityId,
HarmonyMedium,
HarmonyRelease,
HarmonyTrack,
Label,
} from '@/harmonizer/types.ts';
import type { ArtistCreditName, EntityId, HarmonyMedium, HarmonyRelease, HarmonyTrack } from '@/harmonizer/types.ts';

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

Expand Down Expand Up @@ -235,7 +229,7 @@ export class SpotifyReleaseLookup extends ReleaseApiLookup<SpotifyProvider, Albu
status: 'Official',
packaging: 'None',
images: artwork ? [artwork] : [],
labels: this.getLabels(rawRelease),
labels: splitLabels(rawRelease.label),
availableIn: rawRelease.available_markets,
info: this.generateReleaseInfo(),
};
Expand Down Expand Up @@ -292,13 +286,6 @@ export class SpotifyReleaseLookup extends ReleaseApiLookup<SpotifyProvider, Albu
};
}

private getLabels(rawRelease: Album): Label[] {
// split label string using slashes if the results have at least 3 characters
return rawRelease.label?.split(/(?<=[^/]{3,})\/(?=[^/]{3,})/).map((label) => ({
name: label.trim(),
}));
}

private getCopyright(copyrights: Copyright[]): string {
return copyrights.map(this.formatCopyright).join('\n');
}
Expand Down
25 changes: 25 additions & 0 deletions utils/label.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { splitLabels } from './label.ts';

import { assertEquals } from 'std/assert/assert_equals.ts';
import { describe, it } from 'std/testing/bdd.ts';

import type { FunctionSpec } from './test_spec.ts';

describe('GTIN validator', () => {
const passingCases: FunctionSpec<typeof splitLabels> = [
['single label', 'Nuclear Blast', [{ name: 'Nuclear Blast' }]],
['multiple labels', 'Roc Nation/RocAFella/IDJ', [{ name: 'Roc Nation' }, { name: 'RocAFella' }, { name: 'IDJ' }]],
['multiple labels, trim values', 'Roc Nation / RocAFella / IDJ', [{ name: 'Roc Nation' }, { name: 'RocAFella' }, {
name: 'IDJ',
}]],
['short label', 'A/B', [{ name: 'A/B' }]],
['two labels minimal length', 'ABC/DEF', [{ name: 'ABC' }, { name: 'DEF' }]],
['two labels, one short', 'A/BCD/EFG', [{ name: 'A/BCD' }, { name: 'EFG' }]],
];

passingCases.forEach(([description, input, expected]) => {
it(`passes for valid ${description}`, () => {
assertEquals(splitLabels(input), expected);
});
});
});
13 changes: 13 additions & 0 deletions utils/label.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Label } from '@/harmonizer/types.ts';

/**
* Split label string using slashes if the results have at least 3 characters.
*
* @param labels String containing one or more label name.
* @returns List of `Label` entries
*/
export function splitLabels(labels: string): Label[] {
return labels?.split(/(?<=[^/]{3,})\/(?=[^/]{3,})/).map((label) => ({
name: label.trim(),
}));
}

0 comments on commit f929607

Please sign in to comment.