Cotton code
93 lines · 4.3 KB · TypeScriptRaw
/** * Home's fan deals a tool whose tables hold no rows after Settings, in the * kit's order; a tool with rows keeps its place (owner, 26 Oct 2026). The * object decides which are empty — it holds every row, and a phone's copy * keeps only a window — and says so on the home board it serves: the board's * `tools` part names them as `empty`. The phone deals what it is served. * * A tool's tables are the ones it names and the ones its views read * (Web pages reads Documents' document, Birthdays reads People's person); a * tool that reads none is never empty. The package's own tool is no card. */type Doc = Record<string, unknown>;/** Every table `tool` names or a view of it reads. */export function tablesOf(tool: Doc): string[] {  const named = Array.isArray(tool.tables) ? tool.tables.filter((t): t is string => typeof t === "string") : [];  const views = Array.isArray(tool.views) ? (tool.views as Doc[]) : [];  const read = views.map((v) => (v.read as Doc | undefined)?.table).filter((t): t is string => typeof t === "string");  return [...new Set([...named, ...read])];}/** * The ids of `tools` whose every table holds no row, in `order` (the kit's), * a tool the kit does not ship after them by id; `cards` says which tools are * cards on the fan at all. */export function emptyTools(tools: Doc[], order: string[], cards: (tool: Doc) => boolean, holdsRows: (table: string) => boolean): string[] {  const rank = (id: string): number => {    const at = order.indexOf(id);    return at < 0 ? order.length : at;  };  return tools    .filter(cards)    .filter((tool) => {      const tables = tablesOf(tool);      return tables.length > 0 && tables.every((t) => !holdsRows(t));    })    .map((tool) => String(tool.id))    .sort((a, b) => rank(a) - rank(b) || a.localeCompare(b));}/** * The home board as served, its `tools` part naming `empty`: on a whole board * the part itself; on one kept as changes against the shipped board * (`board_overlay`), a change to that part, which the phone's board read lays * over the shipped Home. A Home the person dropped the tools part from is left * as it is. */export function withEmpty(board: Doc, empty: string[]): Doc {  if (board.board_overlay !== undefined) {    const dropped = Array.isArray(board.dropped) ? board.dropped : [];    if (dropped.includes("tools")) return board;    const changes = { fields: {}, removed: [], ...((board.changes as Doc | undefined) ?? {}) } as Doc;    const fields = { ...(changes.fields as Doc) };    const parts = { items: {}, removed: [], ...((fields.parts as Doc | undefined) ?? {}) } as Doc;    const items = { ...(parts.items as Doc) };    const tools = { fields: {}, removed: [], ...((items.tools as Doc | undefined) ?? {}) } as Doc;    items.tools = { ...tools, fields: { ...(tools.fields as Doc), empty: { value: empty } } };    fields.parts = { ...parts, items };    return { ...board, changes: { ...changes, fields } };  }  if (!Array.isArray(board.parts)) return board;  return { ...board, parts: (board.parts as Doc[]).map((p) => (p.kind === "tools" ? { ...p, empty } : p)) };}/** * A board as it is kept: its `tools` part without the `empty` the object * works out and serves (`withEmpty`), so a board a phone sends back as it was * served stores what the person said and nothing derived. On a board kept as * changes against the shipped one, the change naming `empty` goes. */export function withoutEmpty(board: Doc): Doc {  if (board.board_overlay !== undefined) {    const changes = board.changes as Doc | undefined;    const fields = changes?.fields as Doc | undefined;    const parts = fields?.parts as Doc | undefined;    const items = parts?.items as Doc | undefined;    const tools = items?.tools as Doc | undefined;    const toolFields = tools?.fields as Doc | undefined;    if (!toolFields || !("empty" in toolFields)) return board;    const { empty: _empty, ...kept } = toolFields;    return { ...board, changes: { ...changes, fields: { ...fields, parts: { ...parts, items: { ...items, tools: { ...tools, fields: kept } } } } } };  }  if (!Array.isArray(board.parts)) return board;  return {    ...board,    parts: (board.parts as Doc[]).map((p) => {      if (p.kind !== "tools" || !("empty" in p)) return p;      const { empty: _empty, ...kept } = p;      return kept;    }),  };}