/** * What you can narrow a list by. * * A survey of twelve repositories on 2026-09-11 found twelve incompatible * filter-state shapes and no two alike. The thing they actually disagree about * is not the data — it is what KIND of narrowing each field supports, and each * repo rediscovered the kinds one at a time. This is the closed set, taken from * what those repos already do: * * one pick a single value "kind = product" * many pick several "status in (live, validating)" * flag a boolean switch "has no site" * range numeric between "price 10..50" * ordinal a threshold on a scale "tier B or better" * * The sentinel for "no filter" is the EMPTY SELECTION, never a magic string. * One repo used the translated label ("Alle") as its all-value, which makes * filter identity depend on the reader's language — a German user and an * English user filtering the same list got different results. */ function asList(v) { if (v === null || v === undefined) return []; if (Array.isArray(v)) return v.map(String); return [String(v)]; } /** * Does one record pass one facet's selection? * * An empty selection always passes. That is the whole reason this returns true * early: "no filter chosen" and "filter chosen that nothing matches" are * different states, and conflating them is how a list silently empties itself. */ export function facetMatches(facet, row, selected) { if (selected.length === 0) return true; const raw = facet.value(row); switch (facet.kind) { case "flag": { // Present in the selection means "the flag must be true". A flag is the // only kind whose value is about the RECORD, not about a match. const want = selected[0] !== "0" && selected[0] !== "false"; return Boolean(raw) === want; } case "one": return asList(raw).some((v) => v === selected[0]); case "many": { const have = new Set(asList(raw)); return (facet.logic ?? "any") === "all" ? selected.every((s) => have.has(s)) : selected.some((s) => have.has(s)); } case "range": { // selected is [min, max]; either end may be blank, meaning unbounded. const n = Number(raw); if (!Number.isFinite(n)) return false; const lo = selected[0] === "" || selected[0] === undefined ? -Infinity : Number(selected[0]); const hi = selected[1] === "" || selected[1] === undefined ? Infinity : Number(selected[1]); if (!Number.isFinite(lo) && lo !== -Infinity) return true; if (!Number.isFinite(hi) && hi !== Infinity) return true; return n >= lo && n <= hi; } case "ordinal": { const scale = facet.scale ?? []; const threshold = selected[0]; if (threshold === undefined) return true; const floor = scale.indexOf(threshold); if (floor < 0) return true; // an unknown threshold filters nothing const at = scale.indexOf(asList(raw)[0] ?? ""); return at >= 0 && at >= floor; } } }