import { default as romanisedArtists } from "~/third-party/lotus/romanised_artists.json" with { type: "json" }; import { default as artists } from "~/third-party/lotus/artist.json" with { type: "json" }; import { default as combinedArtists } from "~/third-party/lotus/combined_artists.json" with { type: "json" }; function normalise(input: Record): typeof lookup & ((key: string) => string) { const lookup = Object.fromEntries( Object.entries(input).map(([k, v]) => [k.toLowerCase(), v]), ); const fn = (key: string) => lookup[key.toLowerCase()] ?? key; return Object.assign(fn, lookup); } const lotus = { normaliseArtist: normalise(artists), romanise: normalise(romanisedArtists), }; export function normaliseArtistMetadata(rawArtist: string) { const removeUnwantedTokens = (raw: string, regex: RegExp): string => raw.split(regex) .map((s) => s.trim()) .filter(Boolean) .join(";"); const separators = /\s(?:&|feat\.?|ft\.?)\s|[\u3001]|\s*,\s*/i; let artist = removeUnwantedTokens(rawArtist, separators); for (const [key, transform] of Object.entries(combinedArtists)) { if (artist.toLowerCase().includes(key.toLowerCase())) { const pattern = new RegExp(key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "gi"); artist = artist.replace(pattern, transform); } } const index = artist.indexOf(";"); artist = index !== -1 ? artist.substring(0, index) : artist; return lotus.romanise(lotus.normaliseArtist(artist)); }