From 877df50ff0996559fe70dc4a309ab32e31cd2fa0 Mon Sep 17 00:00:00 2001
From: Fuwn <contact@fuwn.me>
Date: Sun, 3 Dec 2023 15:49:02 -0800
Subject: fix(birthdays): remove duplicates

---
 src/lib/Tools/CharacterBirthdays.svelte | 52 +++++++++++++++++++++------------
 1 file changed, 33 insertions(+), 19 deletions(-)

(limited to 'src/lib/Tools/CharacterBirthdays.svelte')

diff --git a/src/lib/Tools/CharacterBirthdays.svelte b/src/lib/Tools/CharacterBirthdays.svelte
index 2a14bbbb..2f37fc1b 100644
--- a/src/lib/Tools/CharacterBirthdays.svelte
+++ b/src/lib/Tools/CharacterBirthdays.svelte
@@ -19,25 +19,39 @@
 		anisearchBirthdays = aniSearchBirthdays(month, day);
 	});
 
-	const combineBirthdaySources = (
-		acdbArray: ACDBBirthday[],
-		aniSearchArray: aniSearchBirthday[]
-	): Birthday[] =>
-		Array.from(
-			new Map(
-				[
-					...aniSearchArray.map((entry) => ({
-						name: entry.name,
-						image: entry.image
-					})),
-					...acdbArray.map((entry) => ({
-						name: entry.name,
-						image: entry.character_image,
-						origin: entry.origin
-					}))
-				].map((entry) => [entry.name, entry])
-			).values()
-		);
+	function normalizeName(name: string): string {
+		return name.toLowerCase().split(' ').sort().join(' ');
+	}
+
+	function combineBirthdaySources(
+		acdb: ACDBBirthday[],
+		aniSearch: aniSearchBirthday[]
+	): Birthday[] {
+		const nameMap = new Map<string, Birthday>();
+
+		for (const entry of aniSearch.map((entry) => ({
+			...entry,
+			normalized_name: normalizeName(entry.name)
+		}))) {
+			nameMap.set(entry.normalized_name, {
+				name: entry.name,
+				image: entry.image
+			});
+		}
+
+		for (const entry of acdb) {
+			const normalized_name = normalizeName(entry.name);
+			if (!nameMap.has(normalized_name)) {
+				nameMap.set(normalized_name, {
+					name: entry.name,
+					image: entry.character_image,
+					origin: entry.origin
+				});
+			}
+		}
+
+		return Array.from(nameMap.values());
+	}
 </script>
 
 {#await ACDBBirthdays(month, day)}
-- 
cgit v1.2.3

