// 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(); // Fallback link when we have no direct Spotify page: a Google search biased at Bandcamp, // which surfaces the artist's Bandcamp page (when it exists) plus general results. export const googleSearch = (n) => `https://www.google.com/search?q=${encodeURIComponent(`${n} bandcamp`)}`; // The precision lever: return the item whose name matches the candidate // case-insensitively, else null. Tolerates null/garbage items. Used for both // Spotify artist items and album items — the exact-name match is the same gate. export function pickArtist(name, items) { const want = norm(name); return (items ?? []).find((a) => a && norm(a.name) === want) ?? null; } // Same exact-name gate, named for the album path's readability. export const pickAlbum = pickArtist; // A "/search" url means we don't have a direct artist page (just a search fallback). export const isSearchUrl = (url) => typeof url === "string" && url.includes("/search"); // True for http/https URLs only — reject javascript:, ftp:, garbage. The Spotify // external_url flows into an injected link's href, so validate it defensively. export function isHttpUrl(u) { try { const p = new URL(u).protocol; return p === "https:" || p === "http:"; } catch { return false; } } // Build the single link to show for a confirmed name. Prefer the direct Spotify // url (validated http(s), not a /search url); otherwise fall back to a Google→Bandcamp // search. `primary` is what the extension renders ({ platform, url }); `kind` // ("artist" | "album") tells it which affordance to show. `kind` is additive — the // extension defaults to "artist" when absent, so older clients keep working. export function linkResult(name, spotifyUrl, kind = "artist") { const k = kind === "album" ? "album" : "artist"; const direct = spotifyUrl && isHttpUrl(spotifyUrl) && !isSearchUrl(spotifyUrl) ? spotifyUrl : null; if (direct) { return { name, kind: k, spotify: direct, primary: { platform: "spotify", url: direct } }; } const url = googleSearch(name); return { name, kind: k, google: url, primary: { platform: "google", url } }; }