// The measure panel's CELLS, as data: what each cell says after a row. No // HTML, no I/O, no browser. // // Ported from the diet-series branch's panel-model.mjs (scripts/report-to-video // there), which modelled the whole table panel -- geometry, row heights, the // log, the finale's two rolls. Only its cell rules are wanted on main: the // instrument modules (dial, odometer, goals, diet badge …) draw from ONE // slice (measure-slice.mjs), and the slice's per-event state is these rules // applied in the cut's order. The table panel's drawing is not ported. // // The three rules, each the branch's own: // // reading freshestReading(): a `current` always wins; a `past` wins only // when nothing is on the board or its asOf is LATER than what is. // goal goalAbsolute(): a `goal_loss` is a weight only against a reading // (the board's, else nothing) -- never one invented from nothing. // regime an opening row (start/on) puts its regime -- in HIS words, the // row's display -- in the cell; a closing one (stop/off) hands the // cell to THIS track's off regime, and only when it names what is // open (offCloses: "off GLP-1" said three weeks into keto leaves // Keto in the cell; the off-scope fix, eff32c8d). import { boundAgrees, displayRange, freshestReading, goalAbsolute, measureDisplay, measureQualifier, measureSchema, offCloses, plotDate } from "./measure-totals.mjs"; /** The events that OPEN or sustain a regime segment, against those that close. */ export const OPENING = new Set(["start", "on"]); export const CLOSING = new Set(["stop", "off"]); /** A regime of the schema, by key, or null. */ export function regimeOf(schema, key) { const s = measureSchema(schema); return s.regimes.find((r) => r.key === key) ?? null; } /** The regime a `stop`/`off` on THIS track hands over to, or null for none. */ export function offOf(schema, trackKey) { const s = measureSchema(schema); const k = s.tracks.find((t) => t.key === trackKey)?.offKey ?? null; return k ? regimeOf(s, k) : null; } /** * The reading cell after `row`: `prev` (or null) unless the row is the * freshest reading of its track by the board's rule. * * @returns {{ value: number, at: string|null, display: string, qualifier: string|null, row: string } | null} */ export function nextReading(prev, row, schema) { const at = plotDate(row); if (!freshestReading(row, at, prev)) return prev; // A bound the reading in force already satisfies (">300" at 342) leaves it as it is (boundAgrees). if (row.population === "current" && boundAgrees({ value: row.value, qualifier: measureQualifier(row, schema), range: displayRange(row.display) }, prev && { ...prev, range: displayRange(prev.display) })) return prev; return { value: Number(row.value), at, display: measureDisplay(row, schema), qualifier: measureQualifier(row, schema), row: row.id, }; } /** * A goal row as the cell holds it: a weight when it is one (a `goal`, or a * `goal_loss` against `base`, a reading `{ value }`), else his words. * * @returns {{ value: number|null, loss: number|null, display: string, deadline: string|null, row: string }} */ export function goalCell(row, base, schema) { const s = measureSchema(schema); const abs = goalAbsolute(row, base); const loss = row.population === "goal_loss" && Number.isFinite(Number(row.value)) ? Math.abs(Number(row.value)) : null; return { value: Number.isFinite(abs) ? abs : null, loss, display: Number.isFinite(abs) ? `${abs}${s.unit ? ` ${s.unit}` : ""}` : measureDisplay(row, s), deadline: row.deadline ?? null, row: row.id, }; } /** * The regime cell of `row`'s track after it: `{ key, label, display, at, * since, row }`, the off regime's word after a close that closes (null when * the track has none), or `open` unchanged (a row that is not a regime claim, * or an off for a regime that is not the one open). * * `display` is HIS words for it (the opening row's display), refreshed on * every opening row, `on` included: a report that the rule is holding is the * freshest thing he has said about what the rule is. `since` is when the * regime in the cell opened: an `on` for the open regime keeps it, a `start` opens it again. */ export function nextRegime(open, row, schema) { const s = measureSchema(schema); const R = regimeOf(s, row?.value); if (!R) return open; const at = plotDate(row); if (OPENING.has(row.event)) { // `on` is a report that the rule holds: it keeps when the regime opened. // `start` is the act -- a fresh start of the same regime opens it again. const same = open && open.key === R.key && row.event === "on"; return { key: R.key, label: R.label, display: measureDisplay(row, s), at, since: same ? open.since : at, row: row.id }; } if (CLOSING.has(row.event)) { const off = offOf(s, row.scope); if (!offCloses(open?.key ?? null, R.key, off?.key ?? null)) return open; return off ? { key: off.key, label: off.label, display: off.label, at, since: at, row: row.id } : null; } return open; }