// The handful of things BOTH ledger kinds need, in one place. // // It exists because there are now two arithmetics over the same JSON shape -- // `headcount` (ledger-totals.mjs) and `measure` (measure-totals.mjs) -- and the // alternative to this file is a cycle between them or a second copy of dateKey. // A second dateKey is the worse outcome by a distance: the month-only entries it // pads are exactly the rows a copy would get subtly wrong, and nothing would say // so until a chart drew a claim in the wrong year. // // Nothing here knows what a claim MEANS. Vocabulary, predicates and totals all // live in the kind's own module. export const isStr = (v) => typeof v === "string" && v.trim().length > 0; /** The confidence a scope adjudication carries. Kind-independent. */ export const SCOPE_CONFIDENCE = ["clear", "read", "unresolved"]; /** Whose number it is. Kind-independent: a midpoint is ours in any unit. */ export const VALUE_KINDS = ["uttered", "derived", "synthetic"]; // --------------------------------------------------------------------------- // Dates. Some entries are month-only ("2024-02") because that is all the source // supports. Sorting pads them to the first of the month, which puts them before // any dated claim in the same month rather than guessing a day. // --------------------------------------------------------------------------- export function dateKey(d) { const s = String(d ?? ""); const m = /^(\d{4})(?:-(\d{2}))?(?:-(\d{2}))?$/.exec(s); if (!m) return "9999-99-99"; return `${m[1]}-${m[2] ?? "01"}-${m[3] ?? "01"}`; } /** Days since the epoch, so an x scale is linear in real time, not in claims. */ export const dayOf = (d) => Date.parse(`${dateKey(d)}T00:00:00Z`) / 86400000; /** Whole days from `a` to `b`, NaN-safe callers only. */ export const daysBetween = (a, b) => dayOf(b) - dayOf(a); /** `2026-01-09` plus n days, back as an ISO date. */ export function addDays(d, n) { const t = new Date(`${dateKey(d)}T00:00:00Z`); t.setUTCDate(t.getUTCDate() + n); return t.toISOString().slice(0, 10); } /** Chronological, ties broken by ledger id so the order is total and stable. */ export const chronological = (ledger) => [...(ledger ?? [])].sort((a, b) => { const d = dateKey(a.date).localeCompare(dateKey(b.date)); return d !== 0 ? d : String(a.id).localeCompare(String(b.id)); }); /** * "6 days earlier", "3 months earlier" — how a predicate says WHEN in prose. * * Every predicate's `text` is read aloud off a card, so the gap is words and * never a date arithmetic the viewer has to perform. */ export function gapWords(from, to) { const days = Math.round(daysBetween(from, to)); if (!Number.isFinite(days)) return "earlier"; if (days <= 0) return "the same day"; if (days === 1) return "a day earlier"; if (days < 31) return `${days} days earlier`; const months = Math.round(days / 30.44); if (months < 24) return `${months} month${months === 1 ? "" : "s"} earlier`; return `${Math.round(days / 365.25)} years earlier`; } // --------------------------------------------------------------------------- // What a ledger row is allowed to carry without declaring itself // --------------------------------------------------------------------------- /** * Every key a ledger row may hold that the code already knows the meaning of. * * It exists so that `ledgerSchema.fields[]` can be the ONLY other way a key * gets onto a row. A key in neither set is a field somebody added by hand or by * script and never declared, which reads on the board as nothing at all: the * arithmetic does not see it, the panel does not draw it, and the operator who * wrote it has no way of knowing that. `umtool check` raises it by name. * * Kind-independent on purpose. Both arithmetics walk the same JSON shape, and a * per-kind list would mean a `headcount` row and a `measure` row disagreeing * about whether `event` is a real key -- which is a distinction neither the * bench nor the inbox makes. */ export const LEDGER_BUILTIN_KEYS = new Set([ // identity, and where it came from "id", "date", "asOf", "src", "channel", "video", "cite", "citeFrom", "quote", // what it says "scope", "population", "value", "event", "display", "label", "deadline", // the adjudication "scopeBasis", "scopeConfidence", "valueKind", "flags", "hedged", "unsourced", // the cut "entryId", "plotted", "hidden", "authorNote", // the measure instruments (measure-slice.mjs): a loss claim's run (his // words placing it), a loss told in the past with no asOf to give it // (`retro`), a promise check's promise, whether it was kept, and a day a // goal or a promise is due "run", "retro", "promise", "kept", "due", // who said it, when it is not the subject (a promise_claim's counterparty; // such a row never moves the subject's numbers), and the day she says she paid "speaker", "paidOn", // how a promise ended (a promise_outcome's: kept, refunded, withdrawn), and the words under its stamp // (`stampNote`: the promise clock's second line in place of "by · no video") "outcome", "stampNote", ]); /** * THE WORDS UNDER AN OUTCOME'S STAMP (a promise_outcome row's `stampNote`, 12 Nov): the promise clock wrote * "by · no video" under REFUNDED whatever the record said. A row's own note is that line instead; the strip * (one line over a post's card) has room for its first clause only. `fit`: the characters the stamp's line holds * beside the count and the as-of at the card's design width -- the longest the clock's own words come to ("by 26 Oct * 2025 · no video" is 25) -- past it the lint warns; `max`: what a row may carry at all. */ export const STAMP_NOTE = Object.freeze({ fit: 26, max: 80 });