// Pure, side-effect-free helpers for the resolver. Kept separate from resolver.js // (which has the HTTP-server side effect) so they can be unit-tested with `node --test` // without starting a server. The artist-match precision lever lives here. export const norm = (s) => String(s).normalize("NFKC").trim().toLowerCase(); export const bandcampSearch = (n) => `https://bandcamp.com/search?q=${encodeURIComponent(n)}&item_type=b`; export const spotifySearch = (n) => `https://open.spotify.com/search/${encodeURIComponent(n)}`; // The precision lever: return the result whose name matches the candidate // case-insensitively, else null. `minScore` (MusicBrainz) additionally requires a // confidence score. Tolerates null/garbage items. export function pickArtist(name, items, { minScore = null } = {}) { const want = norm(name); return ( (items ?? []).find( (a) => a && norm(a.name) === want && (minScore == null || (a.score ?? 0) >= minScore), ) ?? null ); } // Link object for a confirmed Spotify hit (direct artist url when present, else search). export const spotifyResult = (hit) => ({ name: hit.name, spotify: hit.external_urls?.spotify ?? spotifySearch(hit.name), bandcamp: bandcampSearch(hit.name), }); // Extract direct links from a MusicBrainz artist's url-relations (zero-cred: MB stores // official Bandcamp / streaming URLs). Either field may be null. export function relUrls(relations) { const urls = (relations ?? []).map((r) => r && r.url && r.url.resource).filter(Boolean); const find = (host) => urls.find((u) => u.includes(host)) ?? null; return { bandcamp: find("bandcamp.com"), spotify: find("open.spotify.com") }; } // Link object for a confirmed MusicBrainz hit: prefer the direct url-rel links, fall back // to search urls when MB has no relation for that platform. export function mbResult(hit, links) { return { name: hit.name, spotify: links?.spotify ?? spotifySearch(hit.name), bandcamp: links?.bandcamp ?? bandcampSearch(hit.name), }; }