From 6d9740e994435b66e8eaf3619298ec0c803b248e Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Wed, 16 Oct 2024 18:18:28 +0200 Subject: [PATCH] Extract duplicate by title and alternative title logic into function --- .../library/screens/LibraryDuplicates.tsx | 83 ++++++++++++------- 1 file changed, 53 insertions(+), 30 deletions(-) diff --git a/src/modules/library/screens/LibraryDuplicates.tsx b/src/modules/library/screens/LibraryDuplicates.tsx index d0f0e032..ab25642b 100644 --- a/src/modules/library/screens/LibraryDuplicates.tsx +++ b/src/modules/library/screens/LibraryDuplicates.tsx @@ -51,50 +51,73 @@ const findDuplicatesByTitle = >( }; type TMangaDuplicate = Pick; + +const findDuplicatesByTitleAndAlternativeTitlesSingleManga = ( + manga: Manga, + mangas: Manga[], +): { byTitle: Manga[]; byAlternativeTitle: Manga[] } => { + const titleToCheck = enhancedCleanup(manga.title); + + const result: ReturnType> = { + byTitle: [manga], + byAlternativeTitle: [manga], + }; + + mangas.forEach((libraryManga) => { + const isDifferentManga = manga.id !== libraryManga.id; + if (!isDifferentManga) { + return; + } + + const doesTitleMatch = enhancedCleanup(libraryManga.title) === titleToCheck; + const doesAlternativeTitleMatch = enhancedCleanup(libraryManga?.description ?? '').includes(titleToCheck); + + const isDuplicate = doesTitleMatch || doesAlternativeTitleMatch; + if (!isDuplicate) { + return; + } + + if (doesTitleMatch) { + result.byTitle.push(libraryManga); + } + + if (doesAlternativeTitleMatch) { + result.byAlternativeTitle.push(libraryManga); + } + }); + + return result; +}; + const findDuplicatesByTitleAndAlternativeTitles = ( libraryMangas: Manga[], ): Record => { - const titleToMangas: Record> = {}; - const titleToAlternativeTitleMatches: Record> = {}; + const titleToMangas: Record = {}; + const titleToAlternativeTitleMatches: Record = {}; libraryMangas.forEach((mangaToCheck) => { const titleToCheck = enhancedCleanup(mangaToCheck.title); - titleToMangas[titleToCheck] ??= new Set(); - titleToMangas[titleToCheck].add(mangaToCheck); + titleToMangas[titleToCheck] ??= []; + titleToAlternativeTitleMatches[titleToCheck] ??= []; - titleToAlternativeTitleMatches[titleToCheck] ??= new Set(); - titleToAlternativeTitleMatches[titleToCheck].add(mangaToCheck); + const { byTitle, byAlternativeTitle } = findDuplicatesByTitleAndAlternativeTitlesSingleManga( + mangaToCheck, + libraryMangas, + ); - libraryMangas.forEach((libraryManga) => { - const isDifferentManga = mangaToCheck.id !== libraryManga.id; - if (!isDifferentManga) { - return; - } - - const doesTitleMatch = enhancedCleanup(libraryManga.title) === titleToCheck; - const doesAlternativeTitleMatch = enhancedCleanup(libraryManga?.description ?? '').includes(titleToCheck); - - const isDuplicate = doesTitleMatch || doesAlternativeTitleMatch; - if (!isDuplicate) { - return; - } - - if (doesTitleMatch) { - titleToMangas[titleToCheck].add(libraryManga); - } - - if (doesAlternativeTitleMatch) { - titleToAlternativeTitleMatches[titleToCheck].add(libraryManga); - } - }); + titleToMangas[titleToCheck].push(...byTitle); + titleToAlternativeTitleMatches[titleToCheck].push(...byAlternativeTitle); }); const titleToDuplicatesEntries = Object.entries(titleToMangas) .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 noDuplicatesFound = duplicates.length === 1;