CatoandClaude Opus 5 69cb4f4ef1 feat: listkit — a list as a query
Filtering, sorting, searching and paging are the same problem everywhere
and were solved separately everywhere. A survey of twelve repositories in
this fleet on 2026-09-11 found twelve incompatible filter-state shapes and
no two alike, ~34 hand-built URL builders, ~25 copies of "toggle a value
in a set" in two encodings, ~11 debounces at five different delays, ~11
copies of Math.ceil(total / pageSize) with three in one repo, and three of
the seven repos that need to escape a SQL LIKE pattern doing so.

Two of the toggle copies are character-for-character identical in repos
that share no code. One repo writes the same three-line body eight times
for eight facets plus eleven more for its boolean flags — about a hundred
and twenty lines expressing one idea. This is well past the rule of three.

Four live bugs were among the URL builders, and the codec here exists so
they cannot be written again: a preset that rebuilt the params from
scratch and wiped the reader's search; a date range that did not clear
page and stranded the reader past the end; a builder that preserved three
params by name so every later param had to be remembered in six template
strings; and a debounced box using push rather than replace, so the back
button walked through every spelling of the word.

What it ships is the decisions. Five facet kinds — one, many, flag, range,
ordinal — with the empty selection as the only sentinel, because one repo
used its translated label as the all-value and made filter identity depend
on the reader's language. A comparator that puts missing values last in
BOTH directions, since a record with no date is not the oldest record. Page
arithmetic that cannot produce a negative offset and gives an empty list
one page rather than "page 1 of 0". Facet counts computed with that
facet's own selection lifted, so an unselected option never reads as a
dead end.

What it deliberately does not ship is markup, tokens, React, a search
engine or an HTTP client. Nine token vocabularies and six chip treatments
were found among the same repos, several different on purpose. The fleet
has already run the experiment where a shared renderer owns the markup: it
reached two consumers of twenty and shipped the same two defects to both,
unfixable downstream. Centralize the rule; render it locally.

43 tests, importing the package by name so a broken exports map fails here
rather than at the first consumer's install.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018Z7YRpgsEtNjRqr1usHMQY
2026-09-11 17:17:16 +02:00
2026-09-11 17:17:16 +02:00
2026-09-11 17:17:16 +02:00
2026-09-11 17:17:16 +02:00
2026-09-11 17:17:16 +02:00
2026-09-11 17:17:16 +02:00

listkit

A list as a query. Filtering, sorting, searching and paging are the same problem everywhere, and they were solved separately everywhere.

A survey of twelve repositories in this fleet on 2026-09-11 found:

what count
incompatible filter-state shapes 12, no two alike
hand-built URL builders ~34
copies of "toggle a value in a set" ~25, in two encodings
debounces, at five different delays ~11
copies of Math.ceil(total / pageSize) ~11, three in one repo
repos that escape % and _ before a SQL LIKE 3 of 7 that need to

Two of the toggle copies were character-for-character identical in repos that share no code. One repo wrote the same three-line body eight times for eight facets, plus eleven more for its boolean flags — about a hundred and twenty lines expressing one idea.

Four live bugs were among the URL builders, and this codec exists so they cannot be written again:

  • one built new URLSearchParams() from scratch, so applying a preset silently wiped the reader's active search and five other facets;
  • one changed a date range without clearing page, landing the reader on page 4 of a two-page result;
  • one preserved exactly three params by name, so every param added later had to be remembered in six separate template strings;
  • one used push rather than replace for a debounced search box, so the back button walked backwards through every intermediate spelling of the word.

What it ships

The decisions, and nothing else.

import { parseQuery, writeQuery, applyQuery, type ListSpec } from "listkit";

const spec: ListSpec<Project> = {
  facets: [
    { key: "kind", kind: "one", value: (p) => p.kind, options: ["product", "client", "demo"] },
    { key: "status", kind: "many", value: (p) => p.status, options: ["live", "prospect"] },
    { key: "nosite", kind: "flag", value: (p) => !p.site },
  ],
  search: { text: (p) => [p.name, p.description, p.host] },
  sorts: [
    { key: "name", by: [(p) => p.name] },
    { key: "newest", by: [(p) => p.since, (p) => p.name] },
  ],
  defaultSort: "name",
  defaultPageSize: 25,
};

const query = parseQuery(searchParams, spec); // validated, clamped
const { rows, matched, page, counts } = applyQuery(projects, spec, query);
const url = `?${writeQuery(searchParams, next, spec, query)}`;

Five facet kinds, and no magic sentinel

one · many · flag · range · ordinal. The value for "no filter" is the empty selection, never a magic string. One surveyed repo used the translated label ("Alle") as its all-value, which made filter identity depend on the reader's language.

Rules the tests pin

  • An empty selection filters nothing, not everything.
  • Missing values sort last in both directions. A record with no date is not the oldest record.
  • ?page=0 can never become a negative offset, and an empty list still has one page rather than "page 1 of 0".
  • Facet counts are computed with that facet's own selection lifted, so while filtering by kind=product the count beside kind=demo still says how many demos exist, instead of zero.
  • Writing copies the params you were given. A locale or a referrer survives.
  • page is dropped when the result set changes, kept when only the page moves.
  • A superseded async search resolves to undefined, so a slow "ab" cannot overwrite a fast "abc".

What it deliberately does not ship

Markup, tokens, React, a search engine, or an HTTP client.

Nine token vocabularies and six chip treatments were found among those same repos, several different on purpose — one marketplace is monochrome by design, one product reserves an accent colour for Bitcoin UI, one is the only RTL-aware surface in the fleet. The fleet has already run the experiment where a shared renderer owns the markup: it reached two consumers out of twenty and shipped the same two defects to both, unfixable downstream because a consumer cannot patch markup it does not own.

Centralize the rule; render it locally.

The execution engine is also yours. applyQuery is the in-memory engine, for bounded collections. The same ListSpec describes what a SQL builder should do with a large one — escapeLike and likeContains are exported for exactly that. Forcing one execution model would serve one repo and strand the rest.

Install

pnpm add github:bitbaum/listkit#v0.1.0

Develop

pnpm install
pnpm run verify   # format, lint, typecheck, build, test

Tests import the package by name, so a broken exports or files map fails here rather than at the first consumer's install.

MIT.

S
Description
No description provided
Readme MIT
160 KiB
0 Stars 2 Watchers 0 Forks
Languages
TypeScript 58.5%
JavaScript 41.5%