// THE DIET RECORD (the finale's detailed diet runs, `deck.finale.dietRuns`): // the WHOLE record of his diets in scope -- every regime row, on camera or // not, from the earliest -- as RUNS, with the rows that stopped one or named a // medication as marks, and his stated weights along it. measure-slice.mjs // writes it into the slice as `record` only when the finale asks for it, so a // cut without it writes the slice it always did; chrome-runs-finale.mjs draws it. // // The runs follow the state's own rule (panel-model.mjs nextRegime: "on" keeps // when a regime opened, "start" opens it again, an "off" closes only what it // names; the track's off key is no diet): a new run at each change of regime, // numbered as the state numbers its diets. A run is named by its opening row's // own words when they name it otherwise ("Weight Watchers" for `calories`). // // Pure: rows and the schema in, plain data out. import { chronological, dateKey } from "./ledger-shared.mjs"; import { measureDisplay, plotDate } from "./measure-totals.mjs"; import { CLOSING, nextRegime, offOf } from "./panel-model.mjs"; const GENERIC = /^(some diet|no diet|something unnamed)$/i; /** * A run's own name from its opening row's words, or null when they only * repeat the regime ("Keto", "Keto · 5 carbs a day", "off keto", "Some diet"). */ export function runName(display, regime) { const d = String(display ?? "").trim(); if (!d || GENERIC.test(d) || /^off\b/i.test(d) || /[·+]/.test(d)) return null; const names = [regime?.key, regime?.label].filter(Boolean).map((x) => String(x).toLowerCase()); if (names.some((n) => d.toLowerCase() === n || d.toLowerCase().startsWith(`${n} `))) return null; return d; } /** * The record: `{ from, to, runs: [{ key, n, since, end, ended, name, row }], * marks: [{ date, key, kind: "off" | "med", display, row }], weights: [{ date, * value }] }` -- `end` null for the run still open; `from`/`to` the earliest and * latest dates any of it has. * * @param {any[]} own his rows in scope * @param {any} schema the resolved measure schema * @param {string | null} regimeTrack the regime track's key * @param {any[]} readings his weight rows (current and past) */ export function dietRecord(own, schema, regimeTrack, readings = [], opts = {}) { if (!regimeTrack) return null; const off = offOf(schema, regimeTrack)?.key ?? null; const regimes = new Map((schema.regimes ?? []).map((r) => [r.key, r])); const runs = []; const marks = []; let open = null; let cur = null; let n = 0; for (const row of chronological(own.filter((r) => r.scope === regimeTrack))) { open = nextRegime(open, row, schema); const date = plotDate(row); const key = open && open.key !== off ? open.key : null; if (key !== (cur?.key ?? null)) { if (cur) { cur.end = date; cur.ended = key ? "new" : "off"; } cur = null; if (key) { n += 1; cur = { key, n, since: open.since ?? date, end: null, ended: null, name: runName(measureDisplay(row, schema), regimes.get(key)), row: row.id }; runs.push(cur); } } const value = String(row.value ?? ""); if (CLOSING.has(row.event)) marks.push({ date, key: value, kind: "off", display: measureDisplay(row, schema), row: row.id }); else if (regimes.get(value)?.key === "glp1" && !(cur && cur.row === row.id)) marks.push({ date, key: value, kind: "med", display: measureDisplay(row, schema), row: row.id }); } const weights = readings .filter((r) => r.population === "current" && Number.isFinite(Number(r.value))) .map((r) => ({ date: plotDate(r), value: Number(r.value) })) .filter((w) => w.date) .sort((a, b) => dateKey(a.date).localeCompare(dateKey(b.date))); const points = recordPoints(own, readings, opts); const { claims, leftOut } = weightClaims(own); const dates = [...runs.flatMap((r) => [r.since, r.end]), ...marks.map((m) => m.date), ...weights.map((w) => w.date), ...points.map((p) => p.date)] .filter(Boolean).map(dateKey); for (const c of claims) dates.push(dateKey(c.applied), dateKey(c.said)); dates.sort(); return { from: dates[0] ?? null, to: dates[dates.length - 1] ?? null, runs, marks, weights, points, claims, leftOut }; } /** A claim told at least this many days after the day it applied is drawn as told later (a retelling). */ export const RETOLD_DAYS = 30; /** * EVERY weight he gave, at the day it APPLIED: each weight-scope row of his * with a number that is a weight -- what he weighs now (`current`) and what he * weighed (`past`) -- `{ row, value, applied, said, retold, qualifier, range, * population }`, `applied` its asOf (else its date), `said` its date, `retold` * when he said it RETOLD_DAYS or more after it applied. A loss or gain * (`delta`) or "N over" (`overweight_by`) names no weight the ledger resolves, * so it is left out and counted (`leftOut`, by population); goals and promises * are not weights. */ export function weightClaims(own) { const claims = []; const leftOut = {}; for (const r of own) { if (r.scope !== "weight") continue; const value = Number(r.value); if (r.population === "current" || r.population === "past") { const said = r.date ?? null; const applied = r.asOf ?? r.date ?? null; if (!Number.isFinite(value) || !applied) { leftOut[r.population] = (leftOut[r.population] ?? 0) + 1; continue; } const late = said && applied && dateKey(applied) < dateKey(said) && (Date.parse(dateKey(said)) - Date.parse(dateKey(applied))) / 86400000 >= RETOLD_DAYS; claims.push({ row: r.id, value, applied: dateKey(applied), said: said ? dateKey(said) : dateKey(applied), retold: !!late, qualifier: typeof r.qualifier === "string" ? r.qualifier : null, range: Array.isArray(r.range) && r.range.length === 2 ? r.range.map(Number) : null, population: r.population, }); } else if ((r.population === "delta" || r.population === "overweight_by") && Number.isFinite(value)) { leftOut[r.population] = (leftOut[r.population] ?? 0) + 1; } } claims.sort((a, b) => a.applied.localeCompare(b.applied) || a.said.localeCompare(b.said)); return { claims, leftOut }; } /** * The annotated weights on the record's line (`deck.finale.dietRuns.marks`): * each named row at the day it was his weight (its asOf, else its date) and its * value, with the author's label -- his words -- `kind` "peak" for THE peak (the * one marked `peak`, else the heaviest marked), "mark" else; and, unless `low` * is false, the lowest weight he stated at the time, "low" (its label `low` * when it is words, else "his low · ") -- never a bound ("over 240"), * and of one weigh-in told twice the same day ("291." and "291.5") the fuller. * A row the ledger lacks, or with no number, is left out. * * @returns {Array<{ row: string, date: string, value: number, label: string, kind: "peak" | "mark" | "low" }>} */ export function recordPoints(own, readings, { marks = [], low = true } = {}) { const byId = new Map(own.map((r) => [r.id, r])); const points = []; for (const m of Array.isArray(marks) ? marks : []) { const row = byId.get(String(m?.row ?? "")); const value = Number(row?.value); if (!row || !Number.isFinite(value) || !plotDate(row)) continue; points.push({ row: row.id, date: plotDate(row), value, label: String(m.label ?? "").trim(), kind: m.peak === true ? "peak" : "mark" }); } if (points.length && !points.some((p) => p.kind === "peak")) points.reduce((a, b) => (b.value > a.value ? b : a)).kind = "peak"; if (low !== false) { // A weight he STATED, as measured: no bound ("over 240" is a floor, not a weigh-in). const stated = readings.filter((r) => r.population === "current" && Number.isFinite(Number(r.value)) && plotDate(r) && !["<", ">"].includes(r.qualifier)); let lo = stated.reduce((a, b) => (!a || Number(b.value) < Number(a.value) || (Number(b.value) === Number(a.value) && dateKey(plotDate(b)) > dateKey(plotDate(a))) ? b : a), null); // One weigh-in told twice ("291." on X, "291.5" on stream, the same day): the reading he gave in full. const fuller = lo && stated.find((r) => r !== lo && dateKey(plotDate(r)) === dateKey(plotDate(lo)) && Math.abs(Number(r.value) - Number(lo.value)) < 1 && !Number.isInteger(Number(r.value)) && Number.isInteger(Number(lo.value))); if (fuller) lo = fuller; if (lo && !points.some((p) => p.row === lo.id)) { points.push({ row: lo.id, date: plotDate(lo), value: Number(lo.value), label: typeof low === "string" && low.trim() ? low.trim() : `his low · ${Number(lo.value)}`, kind: "low" }); } } return points.sort((a, b) => dateKey(a.date).localeCompare(dateKey(b.date))); }