// 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), }); // Link object for a confirmed MusicBrainz hit (search urls — MB gives no platform ids). export const searchResult = (hit) => ({ name: hit.name, spotify: spotifySearch(hit.name), bandcamp: bandcampSearch(hit.name), });