Skip to content

Commit

Permalink
feat(Tidal): format textual copyright symbols as © or ℗
Browse files Browse the repository at this point in the history
Add a utility function formatCopyrightSymbols to handle this and use
it also in the Spotify implementation.
  • Loading branch information
phw committed Jul 9, 2024
1 parent 4702fcb commit 0102dcf
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
11 changes: 4 additions & 7 deletions providers/Spotify/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { ApiAccessToken, type CacheEntry, MetadataApiProvider, ReleaseApiLookup
import { DurationPrecision, FeatureQuality, FeatureQualityMap } from '@/providers/features.ts';
import { capitalizeReleaseType } from '@/harmonizer/release_types.ts';
import { parseHyphenatedDate, PartialDate } from '@/utils/date.ts';
import { splitLabels } from '@/utils/label.ts';
import { formatCopyrightSymbols } from '@/utils/copyright.ts';
import { ResponseError } from '@/utils/errors.ts';
import { selectLargestImage } from '@/utils/image.ts';
import { splitLabels } from '@/utils/label.ts';
import { encodeBase64 } from 'std/encoding/base64.ts';
import { availableRegions } from './regions.ts';

Expand Down Expand Up @@ -342,12 +343,8 @@ export class SpotifyReleaseLookup extends ReleaseApiLookup<SpotifyProvider, Albu
// copyright those get often entered without the corresponding symbol.
// When only importing the text entry the information gets lost. Hence
// prefix the entries with the © or ℗ symbol if it is not already present.
let { text, type } = copyright;
text = text.replace(/\(c\)/i, '©').replace(/\(p\)/i, '℗');
if (!text.includes('©') && !text.includes('℗')) {
text = `${type === 'P' ? '℗' : '©'} ${text}`;
}
return text;
const symbol = copyright.type === 'P' ? '℗' : '©';
return formatCopyrightSymbols(copyright.text, symbol);
}
}

Expand Down
3 changes: 2 additions & 1 deletion providers/Tidal/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@/providers/base.ts';
import { DurationPrecision, FeatureQuality, FeatureQualityMap } from '@/providers/features.ts';
import { capitalizeReleaseType } from '@/harmonizer/release_types.ts';
import { formatCopyrightSymbols } from '@/utils/copyright.ts';
import { parseHyphenatedDate, PartialDate } from '@/utils/date.ts';
import { ResponseError } from '@/utils/errors.ts';
import { selectLargestImage } from '@/utils/image.ts';
Expand Down Expand Up @@ -220,7 +221,7 @@ export class TidalReleaseLookup extends ReleaseApiLookup<TidalProvider, Album> {
}],
media,
releaseDate: parseHyphenatedDate(rawRelease.releaseDate),
copyright: rawRelease.copyright,
copyright: formatCopyrightSymbols(rawRelease.copyright),
status: 'Official',
types: [capitalizeReleaseType(rawRelease.type)],
packaging: 'None',
Expand Down
30 changes: 30 additions & 0 deletions utils/copyright.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { formatCopyrightSymbols } from './copyright.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('format copyright symbols', () => {
const passingCases: FunctionSpec<typeof formatCopyrightSymbols> = [
['should keep string without symbols', 'Nuclear Blast', undefined, 'Nuclear Blast'],
['should replace (P)', '(P) 2016 Century Media Records Ltd.', undefined, '℗ 2016 Century Media Records Ltd.'],
['should keep symbols', '© 2012 S. Carter Enterprises, LLC.', undefined, '© 2012 S. Carter Enterprises, LLC.'],
[
'should convert multiple symbols',
'(p)(c) 2017 S. CARTER ENTERPRISES, LLC. MARKETED BY ROC NATION & DISTRIBUTED BY ROC NATION/UMG RECORDINGS INC.',
undefined,
'℗© 2017 S. CARTER ENTERPRISES, LLC. MARKETED BY ROC NATION & DISTRIBUTED BY ROC NATION/UMG RECORDINGS INC.',
],
['should prepend expected symbol', 'Nuclear Blast', '℗', '℗ Nuclear Blast'],
['should not prepend symbol if it exists', '2024 ℗ Nuclear Blast', '℗', '2024 ℗ Nuclear Blast'],
['should not prepend symbol if any exists', '2024 © Nuclear Blast', '℗', '2024 © Nuclear Blast'],
['should not prepend symbol if it exists as text', '(c)+(p) Nuclear Blast', '℗', '©+℗ Nuclear Blast'],
];

passingCases.forEach(([description, copyright, expectedSymbol, expected]) => {
it(description, () => {
assertEquals(formatCopyrightSymbols(copyright, expectedSymbol), expected);
});
});
});
12 changes: 12 additions & 0 deletions utils/copyright.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** Formats a copyright string, replacing (c) and (p) with the corresponding symbol.
*
* If expectedSymbol is given the symbol will be prepended if no other copyright symbol
* is already present in the string.
*/
export function formatCopyrightSymbols(copyright: string, expectedSymbol: '©' | '℗' | undefined = undefined): string {
copyright = copyright.replace(/\(c\)/i, '©').replace(/\(p\)/i, '℗');
if (expectedSymbol && !copyright.includes('©') && !copyright.includes('℗')) {
copyright = `${expectedSymbol} ${copyright}`;
}
return copyright;
}

0 comments on commit 0102dcf

Please sign in to comment.