@supa-media/tex-contract 0.0.0-stage → 0.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +11 -2
- package/package.json +16 -4
- package/texContract/aboutLines.cjs +117 -0
- package/texContract/aboutLines.d.cts +14 -0
- package/texContract/aboutNoteLimits.cjs +41 -0
- package/texContract/aboutNoteLimits.d.cts +5 -0
- package/texContract/actionManual.cjs +104 -0
- package/texContract/activityHint.cjs +40 -0
- package/texContract/activityHint.d.cts +4 -0
- package/texContract/asks.cjs +16 -0
- package/texContract/asks.d.cts +6 -0
- package/texContract/asksToFile.cjs +20 -0
- package/texContract/asksToFile.d.cts +2 -0
- package/texContract/attention.cjs +138 -0
- package/texContract/attention.d.cts +20 -0
- package/texContract/browserHandback.cjs +13 -0
- package/texContract/browserHandback.d.cts +3 -0
- package/texContract/browserOwner.cjs +31 -0
- package/texContract/browserOwner.d.cts +2 -0
- package/texContract/bytes.cjs +12 -0
- package/texContract/chatHistoryPath.cjs +38 -0
- package/texContract/chatHistoryPath.d.cts +5 -0
- package/texContract/chatLedger.cjs +91 -0
- package/texContract/chatLedger.d.cts +21 -0
- package/texContract/computerTool.cjs +102 -0
- package/texContract/computerTool.d.cts +16 -0
- package/texContract/consentYes.cjs +22 -0
- package/texContract/consentYes.d.cts +5 -0
- package/texContract/contextGateway.cjs +52 -0
- package/texContract/contextGateway.d.cts +19 -0
- package/texContract/deskEligible.cjs +20 -0
- package/texContract/deskEligible.d.cts +3 -0
- package/texContract/downloads.cjs +129 -0
- package/texContract/downloads.d.cts +15 -0
- package/texContract/egressLedger.cjs +114 -0
- package/texContract/egressLedger.d.cts +14 -0
- package/texContract/emailRead.cjs +38 -0
- package/texContract/emailRead.d.cts +5 -0
- package/texContract/eventWords.cjs +35 -0
- package/texContract/eventWords.d.cts +4 -0
- package/texContract/fileParts.cjs +43 -0
- package/texContract/fileParts.d.cts +6 -0
- package/texContract/fileTables.cjs +243 -0
- package/texContract/fileWords.cjs +169 -0
- package/texContract/folded.cjs +16 -0
- package/texContract/folded.d.cts +3 -0
- package/texContract/googleAiAccess.cjs +12 -0
- package/texContract/googleAiAccess.d.cts +2 -0
- package/texContract/graphWires.cjs +18 -0
- package/texContract/graphWires.d.cts +5 -0
- package/texContract/groupSession.cjs +28 -0
- package/texContract/groupSession.d.cts +7 -0
- package/texContract/guestNames.cjs +136 -0
- package/texContract/guestNames.d.cts +35 -0
- package/texContract/heard.cjs +19 -0
- package/texContract/heard.d.cts +4 -0
- package/texContract/heldOffer.cjs +57 -0
- package/texContract/heldOffer.d.cts +8 -0
- package/texContract/helperCallers.cjs +29 -0
- package/texContract/helperCallers.d.cts +4 -0
- package/texContract/identity.cjs +179 -0
- package/texContract/identity.d.cts +8 -0
- package/texContract/jobLogEntries.cjs +93 -0
- package/texContract/jobLogEntries.d.cts +12 -0
- package/texContract/judgedActions.cjs +20 -0
- package/texContract/judgedActions.d.cts +4 -0
- package/texContract/labSandboxGate.cjs +63 -0
- package/texContract/labSandboxGate.d.cts +9 -0
- package/texContract/ledgerReadChars.cjs +4 -0
- package/texContract/ledgerReadChars.d.cts +3 -0
- package/texContract/mailCheck.cjs +90 -0
- package/texContract/mailCheck.d.cts +12 -0
- package/texContract/mailEntries.cjs +64 -0
- package/texContract/mailEntries.d.cts +13 -0
- package/texContract/mailHealth.cjs +60 -0
- package/texContract/mailHealth.d.cts +10 -0
- package/texContract/notePath.cjs +99 -0
- package/texContract/notePath.d.cts +4 -0
- package/texContract/noteRunFacts.cjs +136 -0
- package/texContract/noteRunFacts.d.cts +32 -0
- package/texContract/objectEtag.cjs +84 -0
- package/texContract/objectEtag.d.cts +4 -0
- package/texContract/objectPlace.cjs +32 -0
- package/texContract/objectPlace.d.cts +5 -0
- package/texContract/pageJobs.cjs +226 -0
- package/texContract/pageJobs.d.cts +13 -0
- package/texContract/pdfLinks.cjs +135 -0
- package/texContract/pdfLinks.d.cts +4 -0
- package/texContract/personHub.cjs +38 -0
- package/texContract/personHub.d.cts +5 -0
- package/texContract/personNameText.cjs +18 -0
- package/texContract/personNameText.d.cts +3 -0
- package/texContract/pinnedContext.cjs +12 -0
- package/texContract/pinnedContext.d.cts +3 -0
- package/texContract/quietHoursWindow.cjs +36 -0
- package/texContract/quietHoursWindow.d.cts +3 -0
- package/texContract/scopeClamp.cjs +117 -0
- package/texContract/scopeClamp.d.cts +5 -0
- package/texContract/scopes.cjs +29 -0
- package/texContract/scopes.d.cts +7 -0
- package/texContract/searchSettings.cjs +54 -0
- package/texContract/searchSettings.d.cts +6 -0
- package/texContract/settingHabits.cjs +87 -0
- package/texContract/settingHabits.d.cts +20 -0
- package/texContract/settingsNotes.cjs +217 -0
- package/texContract/settingsNotes.d.cts +26 -0
- package/texContract/shownEmail.cjs +181 -0
- package/texContract/texClients.cjs +53 -0
- package/texContract/texClients.d.cts +12 -0
- package/texContract/texGraph.cjs +593 -0
- package/texContract/texGraph.d.cts +55 -0
- package/texContract/texKeys.cjs +100 -0
- package/texContract/texKeys.d.cts +13 -0
- package/texContract/texOwnSources.cjs +9 -0
- package/texContract/texOwnSources.d.cts +3 -0
- package/texContract/texStoreNames.cjs +26 -0
- package/texContract/texStoreNames.d.cts +12 -0
- package/texContract/texStoreWire.cjs +306 -0
- package/texContract/texStoreWire.d.cts +32 -0
- package/texContract/threadsFold.cjs +75 -0
- package/texContract/threadsFold.d.cts +22 -0
- package/texContract/toolActions.cjs +253 -0
- package/texContract/toolCatalog.cjs +49 -0
- package/texContract/toolCatalog.generated.json +90 -0
- package/texContract/toolContract.cjs +52 -0
- package/texContract/toolContract.d.cts +12 -0
- package/texContract/toolShapes.cjs +14 -0
- package/texContract/transcription.cjs +101 -0
- package/texContract/transcription.d.cts +3 -0
- package/texContract/treeJobs.cjs +31 -0
- package/texContract/treeJobs.d.cts +3 -0
- package/texContract/turnSession.cjs +90 -0
- package/texContract/turnSession.d.cts +19 -0
- package/texContract/turnWords.cjs +90 -0
- package/texContract/turnWords.d.cts +9 -0
- package/texContract/uploadImages.cjs +131 -0
- package/texContract/uploadImages.d.cts +13 -0
- package/texContract/v2Names.cjs +41 -0
- package/texContract/v2SearchFetch.cjs +316 -0
- package/texContract/v2Translate.cjs +123 -0
- package/texContract/vaultTools.cjs +112 -0
- package/texContract/vaultTools.d.cts +15 -0
- package/texContract/yesWords.cjs +91 -0
- package/texShared/argumentCoercion.cjs +93 -0
- package/texShared/cellComments.cjs +122 -0
- package/texShared/cellComments.d.cts +21 -0
- package/texShared/comments.cjs +586 -0
- package/texShared/comments.d.cts +115 -0
- package/texShared/contentTypes.cjs +85 -0
- package/texShared/fileKinds.cjs +123 -0
- package/texShared/flags.cjs +124 -0
- package/texShared/flags.d.cts +21 -0
- package/texShared/folderJobInfo.cjs +301 -0
- package/texShared/folderJobInfo.d.cts +40 -0
- package/texShared/folderRoles.cjs +512 -0
- package/texShared/folderRoles.d.cts +73 -0
- package/texShared/githubOidc.ts +98 -0
- package/texShared/groupRunQuestions.cjs +98 -0
- package/texShared/groupRunQuestions.d.cts +4 -0
- package/texShared/inboxFolders.cjs +49 -0
- package/texShared/inboxFolders.d.cts +5 -0
- package/texShared/ink/drawing.cjs +474 -0
- package/texShared/ink/texFigure.cjs +252 -0
- package/texShared/ink/texFigure.d.cts +36 -0
- package/texShared/ink/texPoses.cjs +180 -0
- package/texShared/ink/texPoses.d.cts +20 -0
- package/texShared/jobPartEnd.cjs +133 -0
- package/texShared/jobPartEnd.d.cts +13 -0
- package/texShared/jobResume.cjs +31 -0
- package/texShared/jobResume.d.cts +5 -0
- package/texShared/jobSplit.cjs +90 -0
- package/texShared/jobSplit.d.cts +6 -0
- package/texShared/jobStart.cjs +56 -0
- package/texShared/jobStart.d.cts +9 -0
- package/texShared/mail.cjs +400 -0
- package/texShared/mail.d.cts +56 -0
- package/texShared/messageAnchors.cjs +38 -0
- package/texShared/models/aiKeys.cjs +137 -0
- package/texShared/models/aiKeys.d.cts +42 -0
- package/texShared/models/aiKeys.json +56 -0
- package/texShared/models/creditRest.cjs +269 -0
- package/texShared/models/promptBudget.cjs +295 -0
- package/texShared/models/promptSections.cjs +29 -0
- package/texShared/noteAppLink.cjs +23 -0
- package/texShared/notePreview.cjs +219 -0
- package/texShared/notePreview.d.cts +21 -0
- package/texShared/pages/blocks.cjs +215 -0
- package/texShared/pages/cover.cjs +164 -0
- package/texShared/pages/css.cjs +181 -0
- package/texShared/pages/cssBlocks.cjs +264 -0
- package/texShared/pages/draw.cjs +212 -0
- package/texShared/pages/drawRich.cjs +232 -0
- package/texShared/pages/html.cjs +168 -0
- package/texShared/pages/inline.cjs +251 -0
- package/texShared/pages/layout.cjs +76 -0
- package/texShared/pages/parse.cjs +418 -0
- package/texShared/pages/render.cjs +200 -0
- package/texShared/pages/skill.cjs +147 -0
- package/texShared/pages/skill.d.cts +5 -0
- package/texShared/pages/templates.cjs +336 -0
- package/texShared/r2Store.cjs +163 -0
- package/texShared/remoteImage.cjs +177 -0
- package/texShared/routines.cjs +656 -0
- package/texShared/routines.d.cts +86 -0
- package/texShared/servedListing.cjs +250 -0
- package/texShared/servedListing.d.cts +17 -0
- package/texShared/sheetCsv.cjs +566 -0
- package/texShared/sheetCsv.d.cts +91 -0
- package/texShared/storageLayout.cjs +88 -0
- package/texShared/storeKeys.cjs +144 -0
- package/texShared/toolArguments.cjs +323 -0
|
@@ -0,0 +1,566 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sheets: a `.csv` file kept in a workspace like a note.
|
|
3
|
+
*
|
|
4
|
+
* The file is the whole truth. A column's type sits in its header after a
|
|
5
|
+
* colon (`renews:date`), and a header with no known type after a colon is
|
|
6
|
+
* plain text, so `time: 10:30` stays a column called `time: 10:30`. There is
|
|
7
|
+
* no sidecar file: the owner rejected a YAML beside every CSV (2026-11-23).
|
|
8
|
+
* Select options are the values already in the column. A multiselect cell
|
|
9
|
+
* holds several options joined with ", " (as Notion and Airtable export them),
|
|
10
|
+
* so one option never contains a comma.
|
|
11
|
+
*
|
|
12
|
+
* Pure and dependency-free: the gateway's fetch and write tools and the app's
|
|
13
|
+
* grid import the same functions, so every surface reads a sheet the same way.
|
|
14
|
+
* See docs/decisions/sheets.md. Which paths are sheets is the file-kinds
|
|
15
|
+
* list's to say (`packages/shared/src/fileKinds.cjs`).
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const { isSheetPath: isSheetKind } = require("./fileKinds.cjs");
|
|
19
|
+
const cellComments = require("./cellComments.cjs");
|
|
20
|
+
|
|
21
|
+
const SHEET_EXTENSION = ".csv";
|
|
22
|
+
/**
|
|
23
|
+
* `comments` is not a type a person picks: it marks the one column a sheet keeps
|
|
24
|
+
* its cell comments in (`packages/shared/src/cellComments.cjs`), which the grid
|
|
25
|
+
* never draws and a read leaves out of the table.
|
|
26
|
+
*/
|
|
27
|
+
const COLUMN_TYPES = Object.freeze(["text", "number", "usd", "date", "person", "select", "multiselect", "checkbox", "link", "comments"]);
|
|
28
|
+
/** A sheet past either bound is held back and named, never cut short. */
|
|
29
|
+
const MAX_SHEET_BYTES = 5 * 1024 * 1024;
|
|
30
|
+
const MAX_SHEET_ROWS = 50_000;
|
|
31
|
+
|
|
32
|
+
const TYPE_SET = new Set(COLUMN_TYPES);
|
|
33
|
+
// A day (`2026-11-24`) or a UTC instant (`2026-11-24T09:30Z`, seconds and milliseconds optional).
|
|
34
|
+
const DATE = /^(\d{4})-(\d{2})-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3}))?)?Z)?$/;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A date cell's moment in milliseconds since the epoch, UTC, or NaN when it is
|
|
38
|
+
* not a date. A day means its midnight. Parsed by hand rather than with
|
|
39
|
+
* `Date.parse`, which rolls `2026-02-30` over into March. Instants written to
|
|
40
|
+
* different precision (`T09:00Z`, `T09:00:30Z`) do not sort by their text, so
|
|
41
|
+
* anything that orders dates orders by this.
|
|
42
|
+
*/
|
|
43
|
+
function dateMs(value) {
|
|
44
|
+
const m = DATE.exec(cellText(value).trim());
|
|
45
|
+
if (!m) return Number.NaN;
|
|
46
|
+
const [y, mo, d, h = "0", mi = "0", sec = "0", frac = "0"] = m.slice(1);
|
|
47
|
+
const hour = Number(h);
|
|
48
|
+
const minute = Number(mi);
|
|
49
|
+
const second = Number(sec);
|
|
50
|
+
if (hour > 23 || minute > 59 || second > 59) return Number.NaN;
|
|
51
|
+
const ms = Date.UTC(Number(y), Number(mo) - 1, Number(d), hour, minute, second, Number(frac.padEnd(3, "0")));
|
|
52
|
+
const back = new Date(ms);
|
|
53
|
+
if (back.getUTCFullYear() !== Number(y) || back.getUTCMonth() !== Number(mo) - 1 || back.getUTCDate() !== Number(d)) return Number.NaN;
|
|
54
|
+
return ms;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function isSheetPath(path) {
|
|
58
|
+
return isSheetKind(path);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A column's layout after its type: `nowrap` and a width in pixels, `w320`.
|
|
63
|
+
* Wrapping is the default (Dev, 2026-11-26: "make wrap text the default for all
|
|
64
|
+
* csvs"), so only a column turned off says so; `wrap`, written before that, still
|
|
65
|
+
* reads as on.
|
|
66
|
+
*/
|
|
67
|
+
const LAYOUT_WRAP = "wrap";
|
|
68
|
+
const LAYOUT_NOWRAP = "nowrap";
|
|
69
|
+
const LAYOUT_WIDTH = /^w(\d{1,4})$/;
|
|
70
|
+
const MIN_COLUMN_WIDTH = 48;
|
|
71
|
+
const MAX_COLUMN_WIDTH = 2000;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* `renews:date` → { name: "renews", type: "date" }; `notes` → text.
|
|
75
|
+
* A column's layout rides after its type, so every reader sees the same
|
|
76
|
+
* sheet: `notes:text:nowrap:w320` stays on one line and is 320 wide. Layout without a
|
|
77
|
+
* known type before it is not layout: the whole cell stays the name.
|
|
78
|
+
*/
|
|
79
|
+
function parseHeaderCell(cell) {
|
|
80
|
+
const raw = String(cell ?? "").trim();
|
|
81
|
+
const parts = raw.split(":");
|
|
82
|
+
const layout = {};
|
|
83
|
+
let end = parts.length;
|
|
84
|
+
while (end > 2) {
|
|
85
|
+
const token = parts[end - 1].trim().toLowerCase();
|
|
86
|
+
const width = LAYOUT_WIDTH.exec(token);
|
|
87
|
+
if ((token === LAYOUT_WRAP || token === LAYOUT_NOWRAP) && layout.wrap === undefined) layout.wrap = token === LAYOUT_WRAP;
|
|
88
|
+
else if (width && layout.width === undefined) layout.width = Math.max(MIN_COLUMN_WIDTH, Math.min(MAX_COLUMN_WIDTH, Number(width[1])));
|
|
89
|
+
else break;
|
|
90
|
+
end -= 1;
|
|
91
|
+
}
|
|
92
|
+
if (end < parts.length) {
|
|
93
|
+
const type = parts[end - 1].trim().toLowerCase();
|
|
94
|
+
const name = parts.slice(0, end - 1).join(":").trim();
|
|
95
|
+
if (TYPE_SET.has(type) && name) return { name, type, ...layout };
|
|
96
|
+
}
|
|
97
|
+
const at = raw.lastIndexOf(":");
|
|
98
|
+
if (at > 0) {
|
|
99
|
+
const type = raw.slice(at + 1).trim().toLowerCase();
|
|
100
|
+
const name = raw.slice(0, at).trim();
|
|
101
|
+
if (TYPE_SET.has(type) && name) return { name, type };
|
|
102
|
+
}
|
|
103
|
+
return { name: raw, type: "text" };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function renderHeaderCell(column) {
|
|
107
|
+
const wrap = column.wrap === false ? LAYOUT_NOWRAP : column.wrap === true ? LAYOUT_WRAP : "";
|
|
108
|
+
const layout = [wrap, Number.isInteger(column.width) ? `w${column.width}` : ""].filter(Boolean);
|
|
109
|
+
const type = column.type || "text";
|
|
110
|
+
if (layout.length) return [column.name, type, ...layout].join(":");
|
|
111
|
+
return type !== "text" ? `${column.name}:${type}` : column.name;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** A column's wrap and width changed; every value and the rest of the header stay as they were. */
|
|
115
|
+
function setColumnLayout(sheet, name, { wrap, width } = {}) {
|
|
116
|
+
const column = findColumn(sheet, name);
|
|
117
|
+
if (!column) return sheet;
|
|
118
|
+
const next = { ...column };
|
|
119
|
+
// On is the default, so turning a column on takes its `nowrap` off rather than writing `wrap`.
|
|
120
|
+
if (wrap !== undefined) { if (wrap) delete next.wrap; else next.wrap = false; }
|
|
121
|
+
if (width !== undefined) {
|
|
122
|
+
if (width === null) delete next.width;
|
|
123
|
+
else next.width = Math.max(MIN_COLUMN_WIDTH, Math.min(MAX_COLUMN_WIDTH, Math.round(width)));
|
|
124
|
+
}
|
|
125
|
+
return { columns: sheet.columns.map((c) => (c === column ? next : c)), rows: sheet.rows };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** RFC 4180 records: quoted fields, doubled quotes, CRLF or LF, newlines inside quotes. */
|
|
129
|
+
function parseCsvRecords(text) {
|
|
130
|
+
const records = [];
|
|
131
|
+
let field = "";
|
|
132
|
+
let record = [];
|
|
133
|
+
let quoted = false;
|
|
134
|
+
let line = 1;
|
|
135
|
+
let recordLine = 1;
|
|
136
|
+
let unterminated = false;
|
|
137
|
+
const src = String(text ?? "").replace(/^\uFEFF/, "");
|
|
138
|
+
for (let i = 0; i < src.length; i += 1) {
|
|
139
|
+
const ch = src[i];
|
|
140
|
+
if (quoted) {
|
|
141
|
+
if (ch === '"') {
|
|
142
|
+
if (src[i + 1] === '"') { field += '"'; i += 1; } else quoted = false;
|
|
143
|
+
} else {
|
|
144
|
+
if (ch === "\n") line += 1;
|
|
145
|
+
field += ch;
|
|
146
|
+
}
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (ch === '"' && field === "") { quoted = true; continue; }
|
|
150
|
+
if (ch === ",") { record.push(field); field = ""; continue; }
|
|
151
|
+
if (ch === "\r" && src[i + 1] === "\n") continue;
|
|
152
|
+
if (ch === "\n" || ch === "\r") {
|
|
153
|
+
record.push(field);
|
|
154
|
+
records.push({ line: recordLine, cells: record });
|
|
155
|
+
record = []; field = ""; line += 1; recordLine = line;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
field += ch;
|
|
159
|
+
}
|
|
160
|
+
if (quoted) unterminated = true;
|
|
161
|
+
if (field !== "" || record.length) {
|
|
162
|
+
record.push(field);
|
|
163
|
+
records.push({ line: recordLine, cells: record });
|
|
164
|
+
}
|
|
165
|
+
return { records, unterminated };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function quote(value) {
|
|
169
|
+
const s = value == null ? "" : String(value);
|
|
170
|
+
return /[",\r\n]/.test(s) || /^\s|\s$/.test(s) ? `"${s.replace(/"/g, '""')}"` : s;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** The value as stored text: numbers plain, checkboxes `true`/`false`. */
|
|
174
|
+
function cellText(value) {
|
|
175
|
+
if (value === true) return "true";
|
|
176
|
+
if (value === false) return "false";
|
|
177
|
+
if (value == null) return "";
|
|
178
|
+
return String(value);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Why a value does not fit its column's type, or "" when it does. An empty cell always fits. */
|
|
182
|
+
function cellProblem(type, value) {
|
|
183
|
+
const s = cellText(value).trim();
|
|
184
|
+
if (s === "") return "";
|
|
185
|
+
switch (type) {
|
|
186
|
+
case "number":
|
|
187
|
+
case "usd":
|
|
188
|
+
return Number.isFinite(Number(s.replace(/[$,]/g, ""))) ? "" : "not a number";
|
|
189
|
+
case "date":
|
|
190
|
+
return Number.isNaN(dateMs(s)) ? "not a date (YYYY-MM-DD, or YYYY-MM-DDTHH:MMZ in UTC)" : "";
|
|
191
|
+
case "checkbox":
|
|
192
|
+
return s === "true" || s === "false" ? "" : "not true or false";
|
|
193
|
+
case "link":
|
|
194
|
+
return /^https?:\/\/\S+$/i.test(s) ? "" : "not a link (https://…)";
|
|
195
|
+
default:
|
|
196
|
+
return "";
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** A cell's text as the value its type means: a number, a boolean, or the text. */
|
|
201
|
+
function typedValue(type, value) {
|
|
202
|
+
const s = cellText(value).trim();
|
|
203
|
+
if (s === "" || cellProblem(type, s)) return s;
|
|
204
|
+
if (type === "number" || type === "usd") return Number(s.replace(/[$,]/g, ""));
|
|
205
|
+
if (type === "checkbox") return s === "true";
|
|
206
|
+
return s;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Text → { columns, rows, errors, tooLarge }. Rows are objects keyed by column
|
|
211
|
+
* name with the cell's text. A row with the wrong number of cells is kept and
|
|
212
|
+
* reported: the reader decides, and nothing is dropped.
|
|
213
|
+
*/
|
|
214
|
+
function parseSheet(text) {
|
|
215
|
+
const src = String(text ?? "");
|
|
216
|
+
const errors = [];
|
|
217
|
+
if (new TextEncoder().encode(src).byteLength > MAX_SHEET_BYTES) {
|
|
218
|
+
return { columns: [], rows: [], errors: [{ line: 0, message: `sheet is over ${MAX_SHEET_BYTES} bytes` }], tooLarge: true };
|
|
219
|
+
}
|
|
220
|
+
const { records, unterminated } = parseCsvRecords(src);
|
|
221
|
+
if (unterminated) errors.push({ line: records.at(-1)?.line ?? 1, message: "a quoted cell is never closed" });
|
|
222
|
+
const nonEmpty = records.filter((r) => !(r.cells.length === 1 && r.cells[0] === ""));
|
|
223
|
+
if (!nonEmpty.length) return { columns: [], rows: [], errors, tooLarge: false };
|
|
224
|
+
const [head, ...body] = nonEmpty;
|
|
225
|
+
if (body.length > MAX_SHEET_ROWS) {
|
|
226
|
+
return { columns: [], rows: [], errors: [{ line: 0, message: `sheet has over ${MAX_SHEET_ROWS} rows` }], tooLarge: true };
|
|
227
|
+
}
|
|
228
|
+
const columns = head.cells.map(parseHeaderCell);
|
|
229
|
+
const seen = new Set();
|
|
230
|
+
columns.forEach((c, i) => {
|
|
231
|
+
if (!c.name) { c.name = `column ${i + 1}`; errors.push({ line: head.line, message: `column ${i + 1} has no name` }); }
|
|
232
|
+
const key = c.name.toLowerCase();
|
|
233
|
+
// `lossy`: rows are keyed by name, so a rewrite of this text would drop one of the two columns.
|
|
234
|
+
if (seen.has(key)) errors.push({ line: head.line, message: `two columns are called "${c.name}"`, lossy: true });
|
|
235
|
+
seen.add(key);
|
|
236
|
+
});
|
|
237
|
+
const rows = body.map((r) => {
|
|
238
|
+
if (r.cells.length !== columns.length) {
|
|
239
|
+
// More cells than the header is `lossy`: a rewrite keeps only the header's width. Fewer is padded, and safe.
|
|
240
|
+
errors.push({ line: r.line, message: `has ${r.cells.length} cells, the header has ${columns.length}`, ...(r.cells.length > columns.length ? { lossy: true } : {}) });
|
|
241
|
+
}
|
|
242
|
+
const row = {};
|
|
243
|
+
columns.forEach((c, i) => { row[c.name] = r.cells[i] ?? ""; });
|
|
244
|
+
return row;
|
|
245
|
+
});
|
|
246
|
+
return { columns, rows, errors, tooLarge: false };
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function serializeSheet({ columns, rows }) {
|
|
250
|
+
const lines = [columns.map((c) => quote(renderHeaderCell(c))).join(",")];
|
|
251
|
+
for (const row of rows) lines.push(columns.map((c) => quote(cellText(row[c.name]))).join(","));
|
|
252
|
+
return `${lines.join("\n")}\n`;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** A multiselect cell's options, in the order written: split on commas, trimmed, blanks and repeats dropped. */
|
|
256
|
+
function splitOptions(value) {
|
|
257
|
+
const out = [];
|
|
258
|
+
for (const part of cellText(value).split(",")) {
|
|
259
|
+
const v = part.trim();
|
|
260
|
+
if (v && !out.includes(v)) out.push(v);
|
|
261
|
+
}
|
|
262
|
+
return out;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** Options as a multiselect cell stores them. */
|
|
266
|
+
function joinOptions(options) {
|
|
267
|
+
return splitOptions((options ?? []).join(",")).join(", ");
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** Values already in a select or multiselect column, each option once, in first-seen order. */
|
|
271
|
+
function selectOptions(sheet, name) {
|
|
272
|
+
const out = [];
|
|
273
|
+
const seen = new Set();
|
|
274
|
+
const multi = findColumn(sheet, name)?.type === "multiselect";
|
|
275
|
+
for (const row of sheet.rows) {
|
|
276
|
+
const values = multi ? splitOptions(row[name]) : [cellText(row[name]).trim()];
|
|
277
|
+
for (const v of values) if (v && !seen.has(v)) { seen.add(v); out.push(v); }
|
|
278
|
+
}
|
|
279
|
+
return out;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/* ------------------------------- querying -------------------------------- */
|
|
283
|
+
|
|
284
|
+
const FILTER_OPS = Object.freeze(["is", "is not", "has", "has no", "contains", ">", "<", ">=", "<=", "after", "before", "is empty", "is not empty"]);
|
|
285
|
+
|
|
286
|
+
function findColumn(sheet, name) {
|
|
287
|
+
const key = String(name ?? "").trim().toLowerCase();
|
|
288
|
+
return sheet.columns.find((c) => c.name.toLowerCase() === key) ?? null;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function compare(type, a, b) {
|
|
292
|
+
if (type === "number" || type === "usd") {
|
|
293
|
+
const x = typedValue(type, a); const y = typedValue(type, b);
|
|
294
|
+
if (typeof x === "number" && typeof y === "number") return x - y;
|
|
295
|
+
}
|
|
296
|
+
if (type === "date") {
|
|
297
|
+
const x = dateMs(a);
|
|
298
|
+
const y = dateMs(b);
|
|
299
|
+
if (!Number.isNaN(x) && !Number.isNaN(y)) return x - y;
|
|
300
|
+
}
|
|
301
|
+
return cellText(a).localeCompare(cellText(b), undefined, { numeric: true });
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function hasOption(column, value, want) {
|
|
305
|
+
const options = column.type === "multiselect" ? splitOptions(value) : [value];
|
|
306
|
+
return options.some((o) => o.toLowerCase() === want.toLowerCase());
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** A value as its column stores it: a number plain, a multiselect's options tidied, the rest trimmed. */
|
|
310
|
+
function storedText(type, text, problem) {
|
|
311
|
+
if (text === "" || problem) return text;
|
|
312
|
+
if (type === "usd" || type === "number") return String(typedValue(type, text));
|
|
313
|
+
if (type === "multiselect") return joinOptions([text]);
|
|
314
|
+
return text;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function rowMatches(sheet, row, filter) {
|
|
318
|
+
const column = findColumn(sheet, filter.column);
|
|
319
|
+
if (!column) return false;
|
|
320
|
+
const value = cellText(row[column.name]).trim();
|
|
321
|
+
const want = cellText(filter.value).trim();
|
|
322
|
+
switch (filter.op ?? "is") {
|
|
323
|
+
case "is": return value.toLowerCase() === want.toLowerCase();
|
|
324
|
+
case "is not": return value.toLowerCase() !== want.toLowerCase();
|
|
325
|
+
case "contains": return value.toLowerCase().includes(want.toLowerCase());
|
|
326
|
+
// One of the cell's options: a multiselect's, split on commas, or any other cell's whole value.
|
|
327
|
+
case "has": return hasOption(column, value, want);
|
|
328
|
+
case "has no": return !hasOption(column, value, want);
|
|
329
|
+
case ">": case "after": return value !== "" && compare(column.type, value, want) > 0;
|
|
330
|
+
case "<": case "before": return value !== "" && compare(column.type, value, want) < 0;
|
|
331
|
+
case ">=": return value !== "" && compare(column.type, value, want) >= 0;
|
|
332
|
+
case "<=": return value !== "" && compare(column.type, value, want) <= 0;
|
|
333
|
+
case "is empty": return value === "";
|
|
334
|
+
case "is not empty": return value !== "";
|
|
335
|
+
default: return false;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** Every problem with a query, so a bad one is refused rather than answered wrongly. */
|
|
340
|
+
function queryProblems(sheet, { where = [], sort, columns } = {}) {
|
|
341
|
+
const problems = [];
|
|
342
|
+
for (const f of where) {
|
|
343
|
+
if (!findColumn(sheet, f.column)) problems.push(`no column "${f.column}"`);
|
|
344
|
+
if (!FILTER_OPS.includes(f.op ?? "is")) problems.push(`unknown operator "${f.op}"; use one of ${FILTER_OPS.join(", ")}`);
|
|
345
|
+
}
|
|
346
|
+
if (sort && !findColumn(sheet, sort.column)) problems.push(`no column "${sort.column}" to sort by`);
|
|
347
|
+
for (const name of columns ?? []) if (!findColumn(sheet, name)) problems.push(`no column "${name}"`);
|
|
348
|
+
return problems;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Filter, sort and trim. `last: n` keeps the final n rows after sorting (the
|
|
353
|
+
* newest, in a sheet kept in date order); `limit` keeps the first n.
|
|
354
|
+
* Each returned row carries `line`, its line in the file.
|
|
355
|
+
*/
|
|
356
|
+
function querySheet(sheet, { where = [], sort, last, limit, columns } = {}) {
|
|
357
|
+
let rows = sheet.rows.map((row, index) => ({ row, line: index + 2 }));
|
|
358
|
+
for (const f of where) rows = rows.filter(({ row }) => rowMatches(sheet, row, f));
|
|
359
|
+
if (sort) {
|
|
360
|
+
const column = findColumn(sheet, sort.column);
|
|
361
|
+
const dir = sort.direction === "desc" ? -1 : 1;
|
|
362
|
+
if (column) rows.sort((a, b) => compare(column.type, a.row[column.name], b.row[column.name]) * dir || a.line - b.line);
|
|
363
|
+
}
|
|
364
|
+
const total = rows.length;
|
|
365
|
+
if (Number.isInteger(last) && last >= 0) rows = rows.slice(Math.max(0, rows.length - last));
|
|
366
|
+
if (Number.isInteger(limit) && limit >= 0) rows = rows.slice(0, limit);
|
|
367
|
+
// The comments column is conversation about the cells, not a value: shown only when asked for by name.
|
|
368
|
+
const keep = columns?.length ? columns.map((n) => findColumn(sheet, n)).filter(Boolean) : sheet.columns.filter((c) => !isCommentsColumn(c));
|
|
369
|
+
return {
|
|
370
|
+
columns: keep,
|
|
371
|
+
total,
|
|
372
|
+
rows: rows.map(({ row, line }) => ({ line, ...Object.fromEntries(keep.map((c) => [c.name, row[c.name] ?? ""])) })),
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** The newest row for each value of `groupBy`, by `dateColumn` (ties: later line wins). */
|
|
377
|
+
function latestByGroup(sheet, { groupBy, dateColumn }) {
|
|
378
|
+
const group = findColumn(sheet, groupBy);
|
|
379
|
+
const date = findColumn(sheet, dateColumn);
|
|
380
|
+
const out = new Map();
|
|
381
|
+
if (!group || !date) return out;
|
|
382
|
+
for (const row of sheet.rows) {
|
|
383
|
+
const key = cellText(row[group.name]).trim();
|
|
384
|
+
if (!key) continue;
|
|
385
|
+
const current = out.get(key);
|
|
386
|
+
if (!current || compare(date.type, row[date.name], current[date.name]) >= 0) out.set(key, row);
|
|
387
|
+
}
|
|
388
|
+
return out;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/* -------------------------------- changing -------------------------------- */
|
|
392
|
+
|
|
393
|
+
function normaliseRow(sheet, input, errors, where) {
|
|
394
|
+
const row = {};
|
|
395
|
+
const unknown = Object.keys(input ?? {}).filter((k) => !findColumn(sheet, k));
|
|
396
|
+
if (unknown.length) errors.push(`${where}: no column ${unknown.map((k) => `"${k}"`).join(", ")}`);
|
|
397
|
+
for (const c of sheet.columns) {
|
|
398
|
+
const key = Object.keys(input ?? {}).find((k) => k.toLowerCase() === c.name.toLowerCase());
|
|
399
|
+
const text = key === undefined ? "" : cellText(input[key]).trim();
|
|
400
|
+
const problem = cellProblem(c.type, text);
|
|
401
|
+
if (problem) errors.push(`${where}: ${c.name} "${text}" is ${problem}`);
|
|
402
|
+
row[c.name] = storedText(c.type, text, problem);
|
|
403
|
+
}
|
|
404
|
+
return row;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function matchesAll(sheet, row, match) {
|
|
408
|
+
return Object.entries(match ?? {}).every(([k, v]) => rowMatches(sheet, row, { column: k, op: "is", value: v }));
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Append, update and delete rows, all or nothing: any problem returns the
|
|
413
|
+
* errors and leaves the sheet as it was. An update or delete must name at
|
|
414
|
+
* least one column to match, so "every row" is never the accidental target.
|
|
415
|
+
*/
|
|
416
|
+
function changeRows(sheet, { append = [], update, delete: remove } = {}) {
|
|
417
|
+
const errors = [];
|
|
418
|
+
const next = { columns: sheet.columns, rows: sheet.rows.map((r) => ({ ...r })) };
|
|
419
|
+
let updated = 0;
|
|
420
|
+
let deleted = 0;
|
|
421
|
+
if (update) {
|
|
422
|
+
if (!update.match || !Object.keys(update.match).length) errors.push("update needs match: { column: value }");
|
|
423
|
+
else {
|
|
424
|
+
const set = normaliseRowPartial(next, update.set ?? {}, errors);
|
|
425
|
+
next.rows.forEach((row) => { if (matchesAll(next, row, update.match)) { Object.assign(row, set); updated += 1; } });
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
if (remove) {
|
|
429
|
+
if (!remove.match || !Object.keys(remove.match).length) errors.push("delete needs match: { column: value }");
|
|
430
|
+
else {
|
|
431
|
+
const before = next.rows.length;
|
|
432
|
+
next.rows = next.rows.filter((row) => !matchesAll(next, row, remove.match));
|
|
433
|
+
deleted = before - next.rows.length;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
append.forEach((input, i) => next.rows.push(normaliseRow(next, input, errors, `row ${i + 1}`)));
|
|
437
|
+
if (next.rows.length > MAX_SHEET_ROWS) errors.push(`a sheet holds at most ${MAX_SHEET_ROWS} rows`);
|
|
438
|
+
if (errors.length) return { ok: false, errors, sheet };
|
|
439
|
+
return { ok: true, errors: [], sheet: next, appended: append.length, updated, deleted };
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function normaliseRowPartial(sheet, set, errors) {
|
|
443
|
+
const out = {};
|
|
444
|
+
for (const [k, v] of Object.entries(set)) {
|
|
445
|
+
const c = findColumn(sheet, k);
|
|
446
|
+
if (!c) { errors.push(`set: no column "${k}"`); continue; }
|
|
447
|
+
const text = cellText(v).trim();
|
|
448
|
+
const problem = cellProblem(c.type, text);
|
|
449
|
+
if (problem) errors.push(`set: ${c.name} "${text}" is ${problem}`);
|
|
450
|
+
out[c.name] = c.type === "multiselect" ? storedText(c.type, text, problem) : text;
|
|
451
|
+
}
|
|
452
|
+
return out;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/* ------------------------------ column changes ----------------------------- */
|
|
456
|
+
|
|
457
|
+
function uniqueName(sheet, wanted) {
|
|
458
|
+
let name = wanted.trim() || "column";
|
|
459
|
+
let n = 2;
|
|
460
|
+
while (findColumn(sheet, name)) name = `${wanted.trim() || "column"} ${n++}`;
|
|
461
|
+
return name;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
function addColumn(sheet, { name = "column", type = "text", at } = {}) {
|
|
465
|
+
const column = { name: uniqueName(sheet, name.replace(/[,:\r\n"]/g, " ")), type: TYPE_SET.has(type) ? type : "text" };
|
|
466
|
+
const columns = [...sheet.columns];
|
|
467
|
+
columns.splice(Number.isInteger(at) ? Math.max(0, Math.min(at, columns.length)) : columns.length, 0, column);
|
|
468
|
+
return { columns, rows: sheet.rows.map((r) => ({ ...r, [column.name]: "" })), column };
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function renameColumn(sheet, from, to) {
|
|
472
|
+
const column = findColumn(sheet, from);
|
|
473
|
+
const clean = String(to ?? "").replace(/[,:\r\n"]/g, " ").trim();
|
|
474
|
+
if (!column || !clean || clean === column.name) return sheet;
|
|
475
|
+
if (findColumn(sheet, clean) && clean.toLowerCase() !== column.name.toLowerCase()) return sheet;
|
|
476
|
+
// A cell's threads name their column, so they follow it to its new name.
|
|
477
|
+
const notes = sheet.columns.filter(isCommentsColumn);
|
|
478
|
+
return {
|
|
479
|
+
columns: sheet.columns.map((c) => (c === column ? { ...c, name: clean } : c)),
|
|
480
|
+
rows: sheet.rows.map((r) => {
|
|
481
|
+
const out = {};
|
|
482
|
+
for (const c of sheet.columns) out[c === column ? clean : c.name] = r[c.name];
|
|
483
|
+
for (const c of notes) if (c !== column) out[c.name] = cellComments.renameCellColumn(out[c.name] ?? "", column.name, clean);
|
|
484
|
+
return out;
|
|
485
|
+
}),
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/** The column a sheet keeps its cell comments in. */
|
|
490
|
+
function isCommentsColumn(column) {
|
|
491
|
+
return column?.type === cellComments.COMMENTS_TYPE;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/** The sheet's comments column, or null when no cell has been commented on. */
|
|
495
|
+
function commentsColumnOf(sheet) {
|
|
496
|
+
return sheet.columns.find(isCommentsColumn) ?? null;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/** Changing a type never rewrites a value: one that no longer fits is kept and reported. */
|
|
500
|
+
function setColumnType(sheet, name, type) {
|
|
501
|
+
const column = findColumn(sheet, name);
|
|
502
|
+
if (!column || !TYPE_SET.has(type)) return { sheet, misfits: 0 };
|
|
503
|
+
const next = { columns: sheet.columns.map((c) => (c === column ? { ...c, type } : c)), rows: sheet.rows };
|
|
504
|
+
const misfits = sheet.rows.filter((r) => cellProblem(type, r[column.name])).length;
|
|
505
|
+
return { sheet: next, misfits };
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
function deleteColumn(sheet, name) {
|
|
509
|
+
const column = findColumn(sheet, name);
|
|
510
|
+
if (!column) return sheet;
|
|
511
|
+
return {
|
|
512
|
+
columns: sheet.columns.filter((c) => c !== column),
|
|
513
|
+
rows: sheet.rows.map((r) => { const out = { ...r }; delete out[column.name]; return out; }),
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
function moveColumn(sheet, name, to) {
|
|
518
|
+
const column = findColumn(sheet, name);
|
|
519
|
+
if (!column) return sheet;
|
|
520
|
+
const columns = sheet.columns.filter((c) => c !== column);
|
|
521
|
+
columns.splice(Math.max(0, Math.min(to, columns.length)), 0, column);
|
|
522
|
+
return { columns, rows: sheet.rows };
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/** A new, empty sheet's text. */
|
|
526
|
+
function newSheetText(columns = [{ name: "name", type: "text" }, { name: "notes", type: "text" }]) {
|
|
527
|
+
return serializeSheet({ columns, rows: [] });
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
module.exports = {
|
|
532
|
+
SHEET_EXTENSION,
|
|
533
|
+
COLUMN_TYPES,
|
|
534
|
+
MAX_SHEET_BYTES,
|
|
535
|
+
MAX_SHEET_ROWS,
|
|
536
|
+
dateMs,
|
|
537
|
+
isSheetPath,
|
|
538
|
+
MIN_COLUMN_WIDTH,
|
|
539
|
+
MAX_COLUMN_WIDTH,
|
|
540
|
+
parseHeaderCell,
|
|
541
|
+
renderHeaderCell,
|
|
542
|
+
setColumnLayout,
|
|
543
|
+
parseCsvRecords,
|
|
544
|
+
cellText,
|
|
545
|
+
cellProblem,
|
|
546
|
+
typedValue,
|
|
547
|
+
parseSheet,
|
|
548
|
+
serializeSheet,
|
|
549
|
+
splitOptions,
|
|
550
|
+
joinOptions,
|
|
551
|
+
selectOptions,
|
|
552
|
+
FILTER_OPS,
|
|
553
|
+
rowMatches,
|
|
554
|
+
queryProblems,
|
|
555
|
+
querySheet,
|
|
556
|
+
latestByGroup,
|
|
557
|
+
changeRows,
|
|
558
|
+
addColumn,
|
|
559
|
+
renameColumn,
|
|
560
|
+
isCommentsColumn,
|
|
561
|
+
commentsColumnOf,
|
|
562
|
+
setColumnType,
|
|
563
|
+
deleteColumn,
|
|
564
|
+
moveColumn,
|
|
565
|
+
newSheetText,
|
|
566
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/** Types for `sheetCsv.cjs` (the sheet functions); see that file for the format and the reasons. */
|
|
2
|
+
|
|
3
|
+
/** A column's type: one of `COLUMN_TYPES`, typed as a string because a header's type is read from text and may be any word. */
|
|
4
|
+
export type ColumnType = string;
|
|
5
|
+
|
|
6
|
+
export interface SheetColumn {
|
|
7
|
+
name: string;
|
|
8
|
+
type: ColumnType;
|
|
9
|
+
wrap?: boolean;
|
|
10
|
+
width?: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** One row: every cell as its stored text, keyed by column name. */
|
|
14
|
+
export type SheetRow = Record<string, string>;
|
|
15
|
+
|
|
16
|
+
export interface Sheet {
|
|
17
|
+
columns: SheetColumn[];
|
|
18
|
+
rows: SheetRow[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface SheetError {
|
|
22
|
+
line: number;
|
|
23
|
+
message: string;
|
|
24
|
+
lossy?: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ParsedSheet extends Sheet {
|
|
28
|
+
errors: SheetError[];
|
|
29
|
+
tooLarge: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface SheetFilter {
|
|
33
|
+
column: string;
|
|
34
|
+
op?: string;
|
|
35
|
+
value?: unknown;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type ChangeResult =
|
|
39
|
+
| { ok: false; errors: string[]; sheet: Sheet }
|
|
40
|
+
| { ok: true; errors: []; sheet: Sheet; appended: number; updated: number; deleted: number };
|
|
41
|
+
|
|
42
|
+
export declare const SHEET_EXTENSION: ".csv";
|
|
43
|
+
export declare const COLUMN_TYPES: readonly ColumnType[];
|
|
44
|
+
export declare const MAX_SHEET_BYTES: number;
|
|
45
|
+
export declare const MAX_SHEET_ROWS: number;
|
|
46
|
+
export declare const MIN_COLUMN_WIDTH: number;
|
|
47
|
+
export declare const MAX_COLUMN_WIDTH: number;
|
|
48
|
+
export declare const FILTER_OPS: readonly string[];
|
|
49
|
+
export declare function dateMs(value: unknown): number;
|
|
50
|
+
export declare function isSheetPath(path: unknown): boolean;
|
|
51
|
+
export declare function parseHeaderCell(cell: unknown): SheetColumn;
|
|
52
|
+
export declare function renderHeaderCell(column: Partial<SheetColumn>): string;
|
|
53
|
+
export declare function setColumnLayout(sheet: Sheet, name: string, options?: { wrap?: boolean; width?: number | null }): Sheet;
|
|
54
|
+
export declare function parseCsvRecords(text: unknown): { records: { line: number; cells: string[] }[]; unterminated: boolean };
|
|
55
|
+
export declare function cellText(value: unknown): string;
|
|
56
|
+
export declare function cellProblem(type: string, value: unknown): string;
|
|
57
|
+
export declare function typedValue(type: string, value: unknown): string | number | boolean;
|
|
58
|
+
export declare function parseSheet(text: unknown): ParsedSheet;
|
|
59
|
+
export declare function serializeSheet(sheet: Sheet): string;
|
|
60
|
+
export declare function splitOptions(value: unknown): string[];
|
|
61
|
+
export declare function joinOptions(options: unknown): string;
|
|
62
|
+
export declare function selectOptions(sheet: Sheet, name: string): string[];
|
|
63
|
+
export declare function rowMatches(sheet: Sheet, row: SheetRow, filter: SheetFilter): boolean;
|
|
64
|
+
export declare function queryProblems(
|
|
65
|
+
sheet: Sheet,
|
|
66
|
+
options?: { where?: SheetFilter[]; sort?: { column: string; direction?: string }; columns?: string[] },
|
|
67
|
+
): string[];
|
|
68
|
+
export declare function querySheet(
|
|
69
|
+
sheet: Sheet,
|
|
70
|
+
options?: { where?: SheetFilter[]; sort?: { column: string; direction?: string }; last?: number; limit?: number; columns?: string[] },
|
|
71
|
+
): { columns: SheetColumn[]; total: number; rows: ({ line: number } & SheetRow)[] };
|
|
72
|
+
export declare function latestByGroup(sheet: Sheet, options: { groupBy: string; dateColumn: string }): Map<string, SheetRow>;
|
|
73
|
+
export declare function changeRows(
|
|
74
|
+
sheet: Sheet,
|
|
75
|
+
options?: {
|
|
76
|
+
append?: Record<string, unknown>[];
|
|
77
|
+
update?: { match: Record<string, unknown>; set?: Record<string, unknown> };
|
|
78
|
+
delete?: { match: Record<string, unknown> };
|
|
79
|
+
},
|
|
80
|
+
): ChangeResult;
|
|
81
|
+
export declare function addColumn(
|
|
82
|
+
sheet: Sheet,
|
|
83
|
+
options?: { name?: string; type?: string; at?: number },
|
|
84
|
+
): { columns: SheetColumn[]; rows: SheetRow[]; column: SheetColumn };
|
|
85
|
+
export declare function renameColumn(sheet: Sheet, from: string, to: string): Sheet;
|
|
86
|
+
export declare function isCommentsColumn(column: unknown): boolean;
|
|
87
|
+
export declare function commentsColumnOf(sheet: Sheet): SheetColumn | null;
|
|
88
|
+
export declare function setColumnType(sheet: Sheet, name: string, type: string): { sheet: Sheet; misfits: number };
|
|
89
|
+
export declare function deleteColumn(sheet: Sheet, name: string): Sheet;
|
|
90
|
+
export declare function moveColumn(sheet: Sheet, name: string, to: number): Sheet;
|
|
91
|
+
export declare function newSheetText(columns?: { name: string; type: string }[]): string;
|