Skip to content

Commit

Permalink
refactor(harmony): sort and merge all release types in one step
Browse files Browse the repository at this point in the history
Avoids merging and sorting the types multiple times.
  • Loading branch information
phw committed Jul 8, 2024
1 parent 740647f commit db4e0ef
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
11 changes: 8 additions & 3 deletions harmonizer/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
ProviderPreferences,
ProviderReleaseErrorMap,
ProviderReleaseMap,
ReleaseGroupType,
ResolvableEntity,
} from './types.ts';

Expand Down Expand Up @@ -85,6 +86,9 @@ export function mergeRelease(
const availableRegions = new Set<CountryCode>();
const excludedRegions = new Set<CountryCode>();

// temporary list of all release group types
const releaseGroupTypes = new Array<Iterable<ReleaseGroupType>>();

orderByPreference(availableProviders, preferredProviders);

// Phase 1: Clone properties without specific provider preferences
Expand Down Expand Up @@ -127,7 +131,7 @@ export function mergeRelease(

// Merge release group types
if (sourceRelease.types) {
mergedRelease.types = mergeTypes(mergedRelease.types || [], sourceRelease.types);
releaseGroupTypes.push(sourceRelease.types);
}

// combine availabilities
Expand All @@ -143,8 +147,9 @@ export function mergeRelease(
});
}

// Extend the types with types guessed from titles
guessTypesForRelease(mergedRelease);
// guess types from titles and merge all release types
releaseGroupTypes.push(guessTypesForRelease(mergedRelease));
mergedRelease.types = mergeTypes(...releaseGroupTypes);

// assign temporary sets to the merge target
if (availableRegions.size) {
Expand Down
4 changes: 2 additions & 2 deletions harmonizer/release_types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ describe('release types', () => {

passingCases.forEach(([description, release, expected]) => {
it(description, () => {
guessTypesForRelease(release);
assertEquals(release.types, expected);
const guessedTypes = guessTypesForRelease(release);
assertEquals(new Set(guessedTypes), new Set(expected));
});
});
});
Expand Down
14 changes: 7 additions & 7 deletions harmonizer/release_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { HarmonyRelease, HarmonyTrack, ReleaseGroupType } from './types.ts';
import { primaryTypeIds } from '@kellnerd/musicbrainz/data/release-group';

/** Guess the types for a release from release and track titles. */
export function guessTypesForRelease(release: HarmonyRelease) {
export function guessTypesForRelease(release: HarmonyRelease): Iterable<ReleaseGroupType> {
let types = new Set(release.types);
types = types.union(guessTypesFromTitle(release.title));
if (!types.has('Live') && guessLiveRelease(release.media.flatMap((media) => media.tracklist))) {
types.add('Live');
}
release.types = sortTypes(types);
return types;
}

const detectTypesPatterns = [
Expand Down Expand Up @@ -79,18 +79,18 @@ export function sortTypes(types: Iterable<ReleaseGroupType>): ReleaseGroupType[]
*
* The result is reduced to unique elements with only a single primary type.
*/
export function mergeTypes(...typeLists: Array<ReleaseGroupType>[]): ReleaseGroupType[] {
export function mergeTypes(...typeLists: Iterable<ReleaseGroupType>[]): ReleaseGroupType[] {
const primaryTypes = new Set<ReleaseGroupType>();
const resultTypes = new Set<ReleaseGroupType>();
typeLists.forEach((types) => {
types.forEach((type) => {
for (const types of typeLists) {
for (const type of types) {
if (isPrimaryType(type)) {
primaryTypes.add(type);
} else {
resultTypes.add(type);
}
});
});
}
}
if (primaryTypes.size) {
resultTypes.add(reducePrimaryTypes(Array.from(primaryTypes)));
}
Expand Down

0 comments on commit db4e0ef

Please sign in to comment.