Extract duplicate by title and alternative title logic into function
This commit is contained in:
@@ -51,23 +51,20 @@ const findDuplicatesByTitle = <Manga extends Pick<MangaType, 'title'>>(
|
|||||||
};
|
};
|
||||||
|
|
||||||
type TMangaDuplicate = Pick<MangaType, 'id' | 'title' | 'description'>;
|
type TMangaDuplicate = Pick<MangaType, 'id' | 'title' | 'description'>;
|
||||||
const findDuplicatesByTitleAndAlternativeTitles = <Manga extends TMangaDuplicate>(
|
|
||||||
libraryMangas: Manga[],
|
|
||||||
): Record<string, Manga[]> => {
|
|
||||||
const titleToMangas: Record<string, Set<Manga>> = {};
|
|
||||||
const titleToAlternativeTitleMatches: Record<string, Set<Manga>> = {};
|
|
||||||
|
|
||||||
libraryMangas.forEach((mangaToCheck) => {
|
const findDuplicatesByTitleAndAlternativeTitlesSingleManga = <Manga extends TMangaDuplicate>(
|
||||||
const titleToCheck = enhancedCleanup(mangaToCheck.title);
|
manga: Manga,
|
||||||
|
mangas: Manga[],
|
||||||
|
): { byTitle: Manga[]; byAlternativeTitle: Manga[] } => {
|
||||||
|
const titleToCheck = enhancedCleanup(manga.title);
|
||||||
|
|
||||||
titleToMangas[titleToCheck] ??= new Set();
|
const result: ReturnType<typeof findDuplicatesByTitleAndAlternativeTitlesSingleManga<Manga>> = {
|
||||||
titleToMangas[titleToCheck].add(mangaToCheck);
|
byTitle: [manga],
|
||||||
|
byAlternativeTitle: [manga],
|
||||||
|
};
|
||||||
|
|
||||||
titleToAlternativeTitleMatches[titleToCheck] ??= new Set();
|
mangas.forEach((libraryManga) => {
|
||||||
titleToAlternativeTitleMatches[titleToCheck].add(mangaToCheck);
|
const isDifferentManga = manga.id !== libraryManga.id;
|
||||||
|
|
||||||
libraryMangas.forEach((libraryManga) => {
|
|
||||||
const isDifferentManga = mangaToCheck.id !== libraryManga.id;
|
|
||||||
if (!isDifferentManga) {
|
if (!isDifferentManga) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -81,20 +78,46 @@ const findDuplicatesByTitleAndAlternativeTitles = <Manga extends TMangaDuplicate
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (doesTitleMatch) {
|
if (doesTitleMatch) {
|
||||||
titleToMangas[titleToCheck].add(libraryManga);
|
result.byTitle.push(libraryManga);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (doesAlternativeTitleMatch) {
|
if (doesAlternativeTitleMatch) {
|
||||||
titleToAlternativeTitleMatches[titleToCheck].add(libraryManga);
|
result.byAlternativeTitle.push(libraryManga);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
const findDuplicatesByTitleAndAlternativeTitles = <Manga extends TMangaDuplicate>(
|
||||||
|
libraryMangas: Manga[],
|
||||||
|
): Record<string, Manga[]> => {
|
||||||
|
const titleToMangas: Record<string, Manga[]> = {};
|
||||||
|
const titleToAlternativeTitleMatches: Record<string, Manga[]> = {};
|
||||||
|
|
||||||
|
libraryMangas.forEach((mangaToCheck) => {
|
||||||
|
const titleToCheck = enhancedCleanup(mangaToCheck.title);
|
||||||
|
|
||||||
|
titleToMangas[titleToCheck] ??= [];
|
||||||
|
titleToAlternativeTitleMatches[titleToCheck] ??= [];
|
||||||
|
|
||||||
|
const { byTitle, byAlternativeTitle } = findDuplicatesByTitleAndAlternativeTitlesSingleManga(
|
||||||
|
mangaToCheck,
|
||||||
|
libraryMangas,
|
||||||
|
);
|
||||||
|
|
||||||
|
titleToMangas[titleToCheck].push(...byTitle);
|
||||||
|
titleToAlternativeTitleMatches[titleToCheck].push(...byAlternativeTitle);
|
||||||
});
|
});
|
||||||
|
|
||||||
const titleToDuplicatesEntries = Object.entries(titleToMangas)
|
const titleToDuplicatesEntries = Object.entries(titleToMangas)
|
||||||
.map(([title, titleMatches]) => {
|
.map(([title, titleMatches]) => {
|
||||||
const originalTitle = [...titleMatches][0].title;
|
const uniqueTitleMatches = new Set(titleMatches);
|
||||||
|
const uniqueAlternativeTitleMatches = new Set(titleToAlternativeTitleMatches[title] ?? []);
|
||||||
|
|
||||||
const combinedDuplicates = [...titleMatches, ...(titleToAlternativeTitleMatches[title] ?? [])];
|
const originalTitle = [...uniqueTitleMatches][0].title;
|
||||||
|
|
||||||
|
const combinedDuplicates = [...uniqueTitleMatches, ...uniqueAlternativeTitleMatches];
|
||||||
const duplicates = [...new Set([...combinedDuplicates])];
|
const duplicates = [...new Set([...combinedDuplicates])];
|
||||||
|
|
||||||
const noDuplicatesFound = duplicates.length === 1;
|
const noDuplicatesFound = duplicates.length === 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user