@ember-lattice/formula 0.0.0-stage → 1.1.0
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/CHANGELOG.md +15 -0
- package/LICENSE.md +21 -0
- package/NOTICE +30 -0
- package/README.md +47 -2
- package/dist/a1.d.ts +25 -0
- package/dist/conformance.d.ts +38 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +527 -0
- package/dist/index.js.map +1 -0
- package/dist/r1c1.d.ts +8 -0
- package/dist/refs.d.ts +57 -0
- package/dist/scan.d.ts +23 -0
- package/dist/types.d.ts +156 -0
- package/package.json +48 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @ember-lattice/formula
|
|
2
|
+
|
|
3
|
+
## 1.1.0
|
|
4
|
+
|
|
5
|
+
No changes in this release.
|
|
6
|
+
|
|
7
|
+
## 1.0.0
|
|
8
|
+
|
|
9
|
+
### Major Changes
|
|
10
|
+
|
|
11
|
+
- 7ce4af2: Lattice 1.0: one grid for every collection in Ember. Lists, feeds, tables, boards and
|
|
12
|
+
spreadsheets come from one headless engine, a port of TanStack Table. It has 80 of the 81
|
|
13
|
+
AG Grid features in the parity matrix; the external filter runs after grouping. The release has the client row models, the Lattice wire protocol with servers for Node,
|
|
14
|
+
Rails and Python, an IronCalc spreadsheet with live collaboration, themes, test harnesses, Jig
|
|
15
|
+
stories and Marquetry catalog entries.
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Joe Gaudet
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
Lattice
|
|
2
|
+
Copyright (c) 2026 Joe Gaudet. MIT licensed; see LICENSE.md.
|
|
3
|
+
|
|
4
|
+
Lattice ports code from two MIT-licensed TanStack projects into Ember-native code. Lattice does
|
|
5
|
+
not depend on either package. PORT_MAP.md names each ported file and its source.
|
|
6
|
+
|
|
7
|
+
TanStack Table (@tanstack/table-core 9.3.1)
|
|
8
|
+
https://github.com/TanStack/table
|
|
9
|
+
Copyright (c) 2016 Tanner Linsley
|
|
10
|
+
|
|
11
|
+
TanStack Virtual (@tanstack/virtual-core 3.19.16)
|
|
12
|
+
https://github.com/TanStack/virtual
|
|
13
|
+
Copyright (c) 2021-present Tanner Linsley
|
|
14
|
+
|
|
15
|
+
Both are licensed under the MIT License:
|
|
16
|
+
|
|
17
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
|
18
|
+
and associated documentation files (the "Software"), to deal in the Software without
|
|
19
|
+
restriction, including without limitation the rights to use, copy, modify, merge, publish,
|
|
20
|
+
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
|
|
21
|
+
Software is furnished to do so, subject to the following conditions:
|
|
22
|
+
|
|
23
|
+
The above copyright notice and this permission notice shall be included in all copies or
|
|
24
|
+
substantial portions of the Software.
|
|
25
|
+
|
|
26
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
|
|
27
|
+
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
28
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
29
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
30
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,48 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ember-lattice/formula
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Lattice formula seam. No framework.
|
|
4
|
+
|
|
5
|
+
- The `FormulaEngine` interface, the `SheetOp` ops and the `CellChange` type.
|
|
6
|
+
- A1 and R1C1 parsing for the formula editor: refs, ranges, `$` flags, whole columns and rows,
|
|
7
|
+
cross-sheet refs (`Sheet2!A1`, `'My Sheet'!A1`) and names (`formulaNames`).
|
|
8
|
+
- The stored ID form of a formula. Refs stay valid when rows and columns move.
|
|
9
|
+
- A shared conformance suite that each engine adapter runs.
|
|
10
|
+
|
|
11
|
+
Part of [Lattice](https://joegaudet.com/ember-lattice/). Not released yet.
|
|
12
|
+
|
|
13
|
+
## The stored form
|
|
14
|
+
|
|
15
|
+
A formula stores each ref by the row, column and sheet ID:
|
|
16
|
+
|
|
17
|
+
| A1 | Stored |
|
|
18
|
+
| ------------------ | -------------------- |
|
|
19
|
+
| `B2` | `{{c2,r2}}` |
|
|
20
|
+
| `$B$2` | `{{$c2,$r2}}` |
|
|
21
|
+
| `'My Sheet'!A1:B2` | `{{s2!c1,r1:c2,r2}}` |
|
|
22
|
+
| `B:C` | `{{c2,:c3,}}` |
|
|
23
|
+
| `$2:3` | `{{,$r2:,r3}}` |
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { createRefContext, toA1, toStored } from '@ember-lattice/formula'
|
|
27
|
+
|
|
28
|
+
const ctx = createRefContext({ sheets: [{ id: 's1', name: 'Sheet1', rows, cols }] })
|
|
29
|
+
const stored = toStored('=SUM(A1:A9)', 's1', ctx) // '=SUM({{c1,r1:c1,r9}})'
|
|
30
|
+
toA1(stored, 's1', createRefContext(nextDoc)) // the A1 text after rows move
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
- Make a new `RefContext` after the row or column order changes.
|
|
34
|
+
- Call `shrinkDeleted` before a delete, so that a range end moves inward as in Excel.
|
|
35
|
+
- See `docs/adr/0015-sheet-model/index.html` for the full rules.
|
|
36
|
+
|
|
37
|
+
## Conformance
|
|
38
|
+
|
|
39
|
+
`runConformance(createEngine)` runs `CONFORMANCE_CASES` on new engines and gives the failures.
|
|
40
|
+
|
|
41
|
+
## Tests
|
|
42
|
+
|
|
43
|
+
Build first, so that other packages can import this one from `dist`:
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
volta run --node 24.11.1 npx tsc --build packages/formula && (cd packages/formula && npx tsup)
|
|
47
|
+
volta run --node 24.11.1 npx vitest run packages/formula
|
|
48
|
+
```
|
package/dist/a1.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export declare const MAX_ROWS = 1048576;
|
|
2
|
+
export declare const MAX_COLS = 16384;
|
|
3
|
+
/** One end of a ref. A whole-column ref has no `row`; a whole-row ref has no `col`. */
|
|
4
|
+
export interface A1Part {
|
|
5
|
+
row?: number;
|
|
6
|
+
col?: number;
|
|
7
|
+
rowAbs: boolean;
|
|
8
|
+
colAbs: boolean;
|
|
9
|
+
}
|
|
10
|
+
/** A cell, a range, whole columns (`A:C`) or whole rows (`2:5`), on an optional sheet. */
|
|
11
|
+
export interface A1Ref {
|
|
12
|
+
sheet?: string;
|
|
13
|
+
start: A1Part;
|
|
14
|
+
end?: A1Part;
|
|
15
|
+
}
|
|
16
|
+
export declare function columnLabel(index: number): string;
|
|
17
|
+
/** The 0-based index of a column label, or -1 when it is not a column up to XFD. */
|
|
18
|
+
export declare function columnIndex(label: string): number;
|
|
19
|
+
/** The ref patterns. The scanner uses them with the `y` flag at each token start. */
|
|
20
|
+
export declare const REF_SOURCE: string;
|
|
21
|
+
/** Reads a match of `REF_SOURCE`. Gives undefined when an index is out of the sheet. */
|
|
22
|
+
export declare function refFromMatch(m: RegExpExecArray | RegExpMatchArray): A1Ref | undefined;
|
|
23
|
+
export declare function parseA1(text: string): A1Ref | undefined;
|
|
24
|
+
export declare function quoteSheet(name: string): string;
|
|
25
|
+
export declare function printA1(ref: A1Ref): string;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { CellValue, FormulaEngine, SheetDoc, SheetOp } from './types';
|
|
2
|
+
type Input = number | string | boolean;
|
|
3
|
+
/** A1 address (`B2` or `Other!B2`) to an input. A string that starts with `=` is a formula. */
|
|
4
|
+
type Cells = Record<string, Input>;
|
|
5
|
+
export interface ConformanceCase {
|
|
6
|
+
name: string;
|
|
7
|
+
/** Sheet name to cells. The first sheet is the current sheet. */
|
|
8
|
+
sheets: Record<string, Cells>;
|
|
9
|
+
names?: Record<string, string>;
|
|
10
|
+
expect: Record<string, CellValue>;
|
|
11
|
+
/** A second step: set these cells, then check `apply` gives these changes (and maybe more). */
|
|
12
|
+
then?: {
|
|
13
|
+
set: Cells;
|
|
14
|
+
changes: Record<string, CellValue>;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export interface ConformanceFailure {
|
|
18
|
+
name: string;
|
|
19
|
+
cell: string;
|
|
20
|
+
expected: CellValue | 'a change';
|
|
21
|
+
actual: CellValue | undefined;
|
|
22
|
+
}
|
|
23
|
+
export declare const CONFORMANCE_CASES: ConformanceCase[];
|
|
24
|
+
/** Builds the document of a case, with IDs `<sheet>r<n>` and `<sheet>c<n>`. */
|
|
25
|
+
export declare function conformanceDoc(c: ConformanceCase): {
|
|
26
|
+
doc: SheetDoc;
|
|
27
|
+
at: (key: string) => {
|
|
28
|
+
sheet: string;
|
|
29
|
+
row: string;
|
|
30
|
+
col: string;
|
|
31
|
+
};
|
|
32
|
+
ctx: import("./refs").RefContext;
|
|
33
|
+
set: (cells: Cells) => SheetOp[];
|
|
34
|
+
first: string;
|
|
35
|
+
};
|
|
36
|
+
/** Runs every case on a new engine from `create`. Gives the failures; empty means it passed. */
|
|
37
|
+
export declare function runConformance(create: () => Promise<FormulaEngine>, cases?: ConformanceCase[]): Promise<ConformanceFailure[]>;
|
|
38
|
+
export {};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
import is from '@sindresorhus/is';
|
|
2
|
+
|
|
3
|
+
// src/a1.ts
|
|
4
|
+
var MAX_ROWS = 1048576;
|
|
5
|
+
var MAX_COLS = 16384;
|
|
6
|
+
function columnLabel(index) {
|
|
7
|
+
let label = "";
|
|
8
|
+
for (let n = index + 1; n > 0; n = Math.floor((n - 1) / 26)) {
|
|
9
|
+
label = String.fromCharCode(65 + (n - 1) % 26) + label;
|
|
10
|
+
}
|
|
11
|
+
return label;
|
|
12
|
+
}
|
|
13
|
+
function columnIndex(label) {
|
|
14
|
+
if (!/^[A-Za-z]{1,3}$/.test(label)) return -1;
|
|
15
|
+
let n = 0;
|
|
16
|
+
for (const ch of label.toUpperCase()) n = n * 26 + ch.charCodeAt(0) - 64;
|
|
17
|
+
return n <= MAX_COLS ? n - 1 : -1;
|
|
18
|
+
}
|
|
19
|
+
var SHEET = String.raw`(?:'((?:[^']|'')+)'|([A-Za-z_][\w.]*))!`;
|
|
20
|
+
var CELL = String.raw`(\$?)([A-Za-z]{1,3})(\$?)(\d+)`;
|
|
21
|
+
var COL = String.raw`(\$?)([A-Za-z]{1,3})`;
|
|
22
|
+
var ROW = String.raw`(\$?)(\d+)`;
|
|
23
|
+
var REF_SOURCE = `(?:${SHEET})?(?:${CELL}(?::${CELL})?|${COL}:${COL}|${ROW}:${ROW})` + String.raw`(?![\w.(!\[])`;
|
|
24
|
+
var EXACT = new RegExp(`^${REF_SOURCE}$`);
|
|
25
|
+
function cellPart(abs1, col, abs2, row) {
|
|
26
|
+
const c = columnIndex(col);
|
|
27
|
+
const r = Number(row) - 1;
|
|
28
|
+
if (c < 0 || r < 0 || r >= MAX_ROWS) return void 0;
|
|
29
|
+
return { col: c, row: r, colAbs: abs1 === "$", rowAbs: abs2 === "$" };
|
|
30
|
+
}
|
|
31
|
+
function refFromMatch(m) {
|
|
32
|
+
const g = m.slice(1).map((v) => v ?? "");
|
|
33
|
+
const sheet = g[0] ? g[0].replace(/''/g, "'") : g[1] || void 0;
|
|
34
|
+
let start;
|
|
35
|
+
let end;
|
|
36
|
+
if (g[3]) {
|
|
37
|
+
start = cellPart(g[2], g[3], g[4], g[5]);
|
|
38
|
+
end = g[7] ? cellPart(g[6], g[7], g[8], g[9]) : void 0;
|
|
39
|
+
if (g[7] && !end) return void 0;
|
|
40
|
+
} else if (g[11]) {
|
|
41
|
+
const a = columnIndex(g[11]);
|
|
42
|
+
const b = columnIndex(g[13]);
|
|
43
|
+
if (a < 0 || b < 0) return void 0;
|
|
44
|
+
start = { col: a, colAbs: g[10] === "$", rowAbs: false };
|
|
45
|
+
end = { col: b, colAbs: g[12] === "$", rowAbs: false };
|
|
46
|
+
} else {
|
|
47
|
+
const a = Number(g[15]) - 1;
|
|
48
|
+
const b = Number(g[17]) - 1;
|
|
49
|
+
if (a < 0 || b < 0 || a >= MAX_ROWS || b >= MAX_ROWS) return void 0;
|
|
50
|
+
start = { row: a, rowAbs: g[14] === "$", colAbs: false };
|
|
51
|
+
end = { row: b, rowAbs: g[16] === "$", colAbs: false };
|
|
52
|
+
}
|
|
53
|
+
if (!start) return void 0;
|
|
54
|
+
const ref = { start };
|
|
55
|
+
if (sheet !== void 0) ref.sheet = sheet;
|
|
56
|
+
if (end) ref.end = end;
|
|
57
|
+
return ref;
|
|
58
|
+
}
|
|
59
|
+
function parseA1(text) {
|
|
60
|
+
const m = EXACT.exec(text);
|
|
61
|
+
return m ? refFromMatch(m) : void 0;
|
|
62
|
+
}
|
|
63
|
+
function quoteSheet(name) {
|
|
64
|
+
return /^[A-Za-z_][\w.]*$/.test(name) && !/^[A-Za-z]{1,3}\d+$|^R\d*C\d*$/i.test(name) ? name : `'${name.replace(/'/g, "''")}'`;
|
|
65
|
+
}
|
|
66
|
+
function printPart(part) {
|
|
67
|
+
const col = part.col === void 0 ? "" : (part.colAbs ? "$" : "") + columnLabel(part.col);
|
|
68
|
+
const row = part.row === void 0 ? "" : (part.rowAbs ? "$" : "") + (part.row + 1);
|
|
69
|
+
return col + row;
|
|
70
|
+
}
|
|
71
|
+
function printA1(ref) {
|
|
72
|
+
const sheet = ref.sheet === void 0 ? "" : `${quoteSheet(ref.sheet)}!`;
|
|
73
|
+
return sheet + printPart(ref.start) + (ref.end ? `:${printPart(ref.end)}` : "");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/scan.ts
|
|
77
|
+
var WORD = /[\w.$]/;
|
|
78
|
+
function scanWith(formula, pattern, read) {
|
|
79
|
+
const tokens = [];
|
|
80
|
+
let text = "";
|
|
81
|
+
let i = 0;
|
|
82
|
+
const flush = () => {
|
|
83
|
+
if (text) tokens.push({ kind: "text", text });
|
|
84
|
+
text = "";
|
|
85
|
+
};
|
|
86
|
+
while (i < formula.length) {
|
|
87
|
+
const ch = formula[i];
|
|
88
|
+
if (ch === '"') {
|
|
89
|
+
let j2 = i + 1;
|
|
90
|
+
while (j2 < formula.length && !(formula[j2] === '"' && formula[j2 + 1] !== '"')) {
|
|
91
|
+
j2 += formula[j2] === '"' ? 2 : 1;
|
|
92
|
+
}
|
|
93
|
+
text += formula.slice(i, j2 + 1);
|
|
94
|
+
i = j2 + 1;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const prev = formula[i - 1] ?? "";
|
|
98
|
+
if (!WORD.test(prev) && prev !== "'" && prev !== "]" && prev !== "}") {
|
|
99
|
+
pattern.lastIndex = i;
|
|
100
|
+
const m = pattern.exec(formula);
|
|
101
|
+
const ref = m ? read(m) : void 0;
|
|
102
|
+
if (m && ref !== void 0) {
|
|
103
|
+
flush();
|
|
104
|
+
tokens.push({ kind: "ref", text: m[0], ref });
|
|
105
|
+
i += m[0].length;
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
let j = i + 1;
|
|
110
|
+
if (WORD.test(ch)) while (j < formula.length && WORD.test(formula[j])) j++;
|
|
111
|
+
text += formula.slice(i, j);
|
|
112
|
+
i = j;
|
|
113
|
+
}
|
|
114
|
+
flush();
|
|
115
|
+
return tokens;
|
|
116
|
+
}
|
|
117
|
+
var A1_PATTERN = new RegExp(REF_SOURCE, "y");
|
|
118
|
+
function scanFormula(formula) {
|
|
119
|
+
return scanWith(formula, A1_PATTERN, refFromMatch);
|
|
120
|
+
}
|
|
121
|
+
function formulaNames(formula) {
|
|
122
|
+
const seen = /* @__PURE__ */ new Map();
|
|
123
|
+
for (const token of scanFormula(formula)) {
|
|
124
|
+
if (token.kind === "ref") continue;
|
|
125
|
+
const text = token.text.replace(/"(?:[^"]|"")*"?/g, '""');
|
|
126
|
+
for (const m of text.matchAll(/(?<![\w.$'!])[A-Za-z_\\][\w.]*(?![\w.!]|\s*\()/g)) {
|
|
127
|
+
const key = m[0].toUpperCase();
|
|
128
|
+
if (key !== "TRUE" && key !== "FALSE" && !seen.has(key)) seen.set(key, m[0]);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return [...seen.values()];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// src/refs.ts
|
|
135
|
+
function createAxis(ids) {
|
|
136
|
+
let map;
|
|
137
|
+
return {
|
|
138
|
+
id: (index) => ids[index],
|
|
139
|
+
index: (id) => (map ??= new Map(ids.map((v, i) => [v, i]))).get(id) ?? -1
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
function createRefContext(doc) {
|
|
143
|
+
const byId = new Map(doc.sheets.map((s) => [s.id, s]));
|
|
144
|
+
const byName = new Map(doc.sheets.map((s) => [s.name.toUpperCase(), s.id]));
|
|
145
|
+
const axes = /* @__PURE__ */ new Map();
|
|
146
|
+
const get = (id) => {
|
|
147
|
+
const sheet = byId.get(id);
|
|
148
|
+
if (!sheet) return void 0;
|
|
149
|
+
let a = axes.get(id);
|
|
150
|
+
if (!a) axes.set(id, a = { rows: createAxis(sheet.rows), cols: createAxis(sheet.cols) });
|
|
151
|
+
return a;
|
|
152
|
+
};
|
|
153
|
+
return {
|
|
154
|
+
sheetId: (name) => byName.get(name.toUpperCase()),
|
|
155
|
+
sheetName: (id) => byId.get(id)?.name,
|
|
156
|
+
rows: (id) => get(id)?.rows,
|
|
157
|
+
cols: (id) => get(id)?.cols
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
var ID = /^[\w.-]+$/;
|
|
161
|
+
var ID_SRC = String.raw`(\$?)([\w.-]*),(\$?)([\w.-]*)`;
|
|
162
|
+
var STORED = new RegExp(String.raw`\{\{(?:([\w.-]+)!)?${ID_SRC}(?::${ID_SRC})?\}\}`, "y");
|
|
163
|
+
function idPart(colAbs, col, rowAbs, row) {
|
|
164
|
+
const part = { rowAbs: rowAbs === "$", colAbs: colAbs === "$" };
|
|
165
|
+
if (row) part.row = row;
|
|
166
|
+
if (col) part.col = col;
|
|
167
|
+
return part;
|
|
168
|
+
}
|
|
169
|
+
function readStored(m) {
|
|
170
|
+
const g = m.slice(1).map((v) => v ?? "");
|
|
171
|
+
const ref = { start: idPart(g[1], g[2], g[3], g[4]) };
|
|
172
|
+
if (g[0]) ref.sheet = g[0];
|
|
173
|
+
if (m[6] !== void 0) ref.end = idPart(g[5], g[6], g[7], g[8]);
|
|
174
|
+
return ref;
|
|
175
|
+
}
|
|
176
|
+
function checkId(id) {
|
|
177
|
+
if (!ID.test(id)) throw new Error(`Lattice: the ID "${id}" must match ${ID}`);
|
|
178
|
+
return id;
|
|
179
|
+
}
|
|
180
|
+
function printStored(ref) {
|
|
181
|
+
const part = (p) => (p.col === void 0 ? "" : (p.colAbs ? "$" : "") + checkId(p.col)) + "," + (p.row === void 0 ? "" : (p.rowAbs ? "$" : "") + checkId(p.row));
|
|
182
|
+
const sheet = ref.sheet === void 0 ? "" : `${checkId(ref.sheet)}!`;
|
|
183
|
+
return `{{${sheet}${part(ref.start)}${ref.end ? `:${part(ref.end)}` : ""}}}`;
|
|
184
|
+
}
|
|
185
|
+
function scanStored(stored) {
|
|
186
|
+
return scanWith(stored, STORED, readStored);
|
|
187
|
+
}
|
|
188
|
+
function storedRefs(stored) {
|
|
189
|
+
return scanStored(stored).flatMap((t) => t.kind === "ref" ? [t.ref] : []);
|
|
190
|
+
}
|
|
191
|
+
var REF_ERROR = "#REF!";
|
|
192
|
+
function toIdPart(part, rows, cols) {
|
|
193
|
+
const out = { rowAbs: part.rowAbs, colAbs: part.colAbs };
|
|
194
|
+
if (part.row !== void 0) {
|
|
195
|
+
const id = rows.id(part.row);
|
|
196
|
+
if (id === void 0) return void 0;
|
|
197
|
+
out.row = id;
|
|
198
|
+
}
|
|
199
|
+
if (part.col !== void 0) {
|
|
200
|
+
const id = cols.id(part.col);
|
|
201
|
+
if (id === void 0) return void 0;
|
|
202
|
+
out.col = id;
|
|
203
|
+
}
|
|
204
|
+
return out;
|
|
205
|
+
}
|
|
206
|
+
function toStored(formula, sheetId, ctx) {
|
|
207
|
+
return scanFormula(formula).map((token) => {
|
|
208
|
+
if (token.kind === "text") return token.text;
|
|
209
|
+
const ref = token.ref;
|
|
210
|
+
const sid = ref.sheet === void 0 ? sheetId : ctx.sheetId(ref.sheet);
|
|
211
|
+
const rows = sid === void 0 ? void 0 : ctx.rows(sid);
|
|
212
|
+
const cols = sid === void 0 ? void 0 : ctx.cols(sid);
|
|
213
|
+
if (!rows || !cols || sid === void 0) return REF_ERROR;
|
|
214
|
+
const start = toIdPart(ref.start, rows, cols);
|
|
215
|
+
const end = ref.end && toIdPart(ref.end, rows, cols);
|
|
216
|
+
if (!start || ref.end && !end) return REF_ERROR;
|
|
217
|
+
const out = { start };
|
|
218
|
+
if (ref.sheet !== void 0) out.sheet = sid;
|
|
219
|
+
if (end) out.end = end;
|
|
220
|
+
return printStored(out);
|
|
221
|
+
}).join("");
|
|
222
|
+
}
|
|
223
|
+
function toA1Part(part, rows, cols) {
|
|
224
|
+
const out = { rowAbs: part.rowAbs, colAbs: part.colAbs };
|
|
225
|
+
if (part.row !== void 0) {
|
|
226
|
+
const i = rows.index(part.row);
|
|
227
|
+
if (i < 0) return void 0;
|
|
228
|
+
out.row = i;
|
|
229
|
+
}
|
|
230
|
+
if (part.col !== void 0) {
|
|
231
|
+
const i = cols.index(part.col);
|
|
232
|
+
if (i < 0) return void 0;
|
|
233
|
+
out.col = i;
|
|
234
|
+
}
|
|
235
|
+
return out;
|
|
236
|
+
}
|
|
237
|
+
function resolveRef(ref, sheetId, ctx) {
|
|
238
|
+
const sid = ref.sheet ?? sheetId;
|
|
239
|
+
const rows = ctx.rows(sid);
|
|
240
|
+
const cols = ctx.cols(sid);
|
|
241
|
+
const name = ctx.sheetName(sid);
|
|
242
|
+
if (!rows || !cols || name === void 0) return void 0;
|
|
243
|
+
const start = toA1Part(ref.start, rows, cols);
|
|
244
|
+
const end = ref.end && toA1Part(ref.end, rows, cols);
|
|
245
|
+
if (!start || ref.end && !end) return void 0;
|
|
246
|
+
const out = { start };
|
|
247
|
+
if (ref.sheet !== void 0) out.sheet = name;
|
|
248
|
+
if (end) out.end = end;
|
|
249
|
+
return out;
|
|
250
|
+
}
|
|
251
|
+
function toA1(stored, sheetId, ctx) {
|
|
252
|
+
return scanStored(stored).map((token) => {
|
|
253
|
+
if (token.kind === "text") return token.text;
|
|
254
|
+
const ref = resolveRef(token.ref, sheetId, ctx);
|
|
255
|
+
return ref ? printA1(ref) : REF_ERROR;
|
|
256
|
+
}).join("");
|
|
257
|
+
}
|
|
258
|
+
function shrinkDeleted(stored, sheetId, ctx, deletion) {
|
|
259
|
+
return scanStored(stored).map((token) => {
|
|
260
|
+
if (token.kind === "text") return token.text;
|
|
261
|
+
const ref = token.ref;
|
|
262
|
+
if ((ref.sheet ?? sheetId) !== deletion.sheet || !ref.end) return token.text;
|
|
263
|
+
const out = { ...ref, start: { ...ref.start }, end: { ...ref.end } };
|
|
264
|
+
for (const [key, ids] of [
|
|
265
|
+
["row", deletion.rows],
|
|
266
|
+
["col", deletion.cols]
|
|
267
|
+
]) {
|
|
268
|
+
if (!ids?.length) continue;
|
|
269
|
+
const gone = new Set(ids);
|
|
270
|
+
const axis2 = key === "row" ? ctx.rows(deletion.sheet) : ctx.cols(deletion.sheet);
|
|
271
|
+
const a = out.start[key];
|
|
272
|
+
const b = out.end[key];
|
|
273
|
+
if (!axis2 || a === void 0 || b === void 0) continue;
|
|
274
|
+
const from = axis2.index(a);
|
|
275
|
+
const to = axis2.index(b);
|
|
276
|
+
const pick = (i, step) => {
|
|
277
|
+
for (; step > 0 ? i <= to : i >= from; i += step) {
|
|
278
|
+
const id = axis2.id(i);
|
|
279
|
+
if (id !== void 0 && !gone.has(id)) return id;
|
|
280
|
+
}
|
|
281
|
+
return void 0;
|
|
282
|
+
};
|
|
283
|
+
if (gone.has(a)) {
|
|
284
|
+
const id = pick(from, 1);
|
|
285
|
+
if (id !== void 0) out.start[key] = id;
|
|
286
|
+
}
|
|
287
|
+
if (gone.has(b)) {
|
|
288
|
+
const id = pick(to, -1);
|
|
289
|
+
if (id !== void 0) out.end[key] = id;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return printStored(out);
|
|
293
|
+
}).join("");
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// src/r1c1.ts
|
|
297
|
+
function axis(letter, abs, value, base) {
|
|
298
|
+
if (abs) return `${letter}${value + 1}`;
|
|
299
|
+
const offset = value - base;
|
|
300
|
+
return offset === 0 ? letter : `${letter}[${offset}]`;
|
|
301
|
+
}
|
|
302
|
+
function partR1C1(part, base) {
|
|
303
|
+
const r = part.row === void 0 ? "" : axis("R", part.rowAbs, part.row, base.row);
|
|
304
|
+
const c = part.col === void 0 ? "" : axis("C", part.colAbs, part.col, base.col);
|
|
305
|
+
return r + c;
|
|
306
|
+
}
|
|
307
|
+
function toR1C1(formula, base) {
|
|
308
|
+
return scanFormula(formula).map((token) => {
|
|
309
|
+
if (token.kind === "text") return token.text;
|
|
310
|
+
const { sheet, start, end } = token.ref;
|
|
311
|
+
const prefix = sheet === void 0 ? "" : `${quoteSheet(sheet)}!`;
|
|
312
|
+
return prefix + partR1C1(start, base) + (end ? `:${partR1C1(end, base)}` : "");
|
|
313
|
+
}).join("");
|
|
314
|
+
}
|
|
315
|
+
var SHEET2 = String.raw`(?:'((?:[^']|'')+)'|([A-Za-z_][\w.]*))!`;
|
|
316
|
+
var AXIS = (l) => String.raw`${l}(?:\[(-?\d+)\]|(\d+))?`;
|
|
317
|
+
var PART = `(?:${AXIS("R")}${AXIS("C")}|${AXIS("R")}(?![\\w.])|${AXIS("C")})`;
|
|
318
|
+
var R1C1 = new RegExp(`(?:${SHEET2})?${PART}(?::${PART})?(?![\\w.(!\\[])`, "y");
|
|
319
|
+
var PART_GROUPS = 8;
|
|
320
|
+
function readValue(rel, abs, base) {
|
|
321
|
+
if (abs !== void 0) return { value: Number(abs) - 1, abs: true };
|
|
322
|
+
return { value: base + Number(rel ?? 0), abs: false };
|
|
323
|
+
}
|
|
324
|
+
function readPart(g, base) {
|
|
325
|
+
const part = { rowAbs: false, colAbs: false };
|
|
326
|
+
const setRow = (rel, abs) => {
|
|
327
|
+
const v = readValue(rel, abs, base.row);
|
|
328
|
+
part.row = v.value;
|
|
329
|
+
part.rowAbs = v.abs;
|
|
330
|
+
};
|
|
331
|
+
const setCol = (rel, abs) => {
|
|
332
|
+
const v = readValue(rel, abs, base.col);
|
|
333
|
+
part.col = v.value;
|
|
334
|
+
part.colAbs = v.abs;
|
|
335
|
+
};
|
|
336
|
+
if (g[8] === "cell") {
|
|
337
|
+
setRow(g[0], g[1]);
|
|
338
|
+
setCol(g[2], g[3]);
|
|
339
|
+
} else if (g[8] === "row") setRow(g[4], g[5]);
|
|
340
|
+
else setCol(g[6], g[7]);
|
|
341
|
+
const bad = (v, max) => v !== void 0 && (v < 0 || v >= max);
|
|
342
|
+
return bad(part.row, MAX_ROWS) || bad(part.col, MAX_COLS) ? void 0 : part;
|
|
343
|
+
}
|
|
344
|
+
function kind(text) {
|
|
345
|
+
if (/^R(\[-?\d+\]|\d+)?C/.test(text)) return "cell";
|
|
346
|
+
return text.startsWith("R") ? "row" : "col";
|
|
347
|
+
}
|
|
348
|
+
function fromR1C1(formula, base) {
|
|
349
|
+
return scanWith(formula, R1C1, (m) => {
|
|
350
|
+
const sheet = m[1] !== void 0 ? m[1].replace(/''/g, "'") : m[2];
|
|
351
|
+
const body = m[0].slice(m[0].lastIndexOf("!") + 1);
|
|
352
|
+
const [first, second] = body.split(":");
|
|
353
|
+
const start = readPart([...m.slice(3, 3 + PART_GROUPS), kind(first)], base);
|
|
354
|
+
const end = second === void 0 ? kind(first) === "cell" ? void 0 : start : readPart([...m.slice(3 + PART_GROUPS, 3 + 2 * PART_GROUPS), kind(second)], base);
|
|
355
|
+
const ref = start && (second === void 0 || end) ? { start, ...end ? { end } : {} } : void 0;
|
|
356
|
+
if (ref && sheet !== void 0) ref.sheet = sheet;
|
|
357
|
+
return ref ? printA1(ref) : "#REF!";
|
|
358
|
+
}).map((token) => token.kind === "ref" ? token.ref : token.text).join("");
|
|
359
|
+
}
|
|
360
|
+
var err = (error) => ({ error });
|
|
361
|
+
var CONFORMANCE_CASES = [
|
|
362
|
+
{
|
|
363
|
+
name: "arithmetic and precedence",
|
|
364
|
+
sheets: { Sheet1: { A1: 2, A2: 3, B1: "=A1+A2*2", B2: "=-A1^2", B3: "=(A1+A2)/4" } },
|
|
365
|
+
expect: { B1: 8, B2: 4, B3: 1.25 }
|
|
366
|
+
},
|
|
367
|
+
{
|
|
368
|
+
name: "aggregates",
|
|
369
|
+
sheets: { Sheet1: { A1: 1, A2: 2, A3: 3, A4: "x", B1: "=SUM(A1:A4)", B2: "=AVERAGE(A1:A3)" } },
|
|
370
|
+
expect: { B1: 6, B2: 2 }
|
|
371
|
+
},
|
|
372
|
+
{
|
|
373
|
+
name: "counts and conditions",
|
|
374
|
+
sheets: {
|
|
375
|
+
Sheet1: { A1: 5, A2: 15, A3: 25, B1: '=COUNTIF(A1:A3,">10")', B2: '=SUMIF(A1:A3,">10")' }
|
|
376
|
+
},
|
|
377
|
+
expect: { B1: 2, B2: 40 }
|
|
378
|
+
},
|
|
379
|
+
{
|
|
380
|
+
name: "logic and text",
|
|
381
|
+
sheets: {
|
|
382
|
+
Sheet1: {
|
|
383
|
+
A1: "Lattice",
|
|
384
|
+
B1: '=IF(LEN(A1)>5,UPPER(A1),"short")',
|
|
385
|
+
B2: '=LEFT(A1,3)&"-"&RIGHT(A1,2)',
|
|
386
|
+
B3: "=AND(TRUE,NOT(FALSE))",
|
|
387
|
+
B4: '=TEXTJOIN(",",TRUE,"a","","b")'
|
|
388
|
+
}
|
|
389
|
+
},
|
|
390
|
+
expect: { B1: "LATTICE", B2: "Lat-ce", B3: true, B4: "a,b" }
|
|
391
|
+
},
|
|
392
|
+
{
|
|
393
|
+
name: "lookups",
|
|
394
|
+
sheets: {
|
|
395
|
+
Sheet1: {
|
|
396
|
+
A1: "a",
|
|
397
|
+
A2: "b",
|
|
398
|
+
A3: "c",
|
|
399
|
+
B1: 10,
|
|
400
|
+
B2: 20,
|
|
401
|
+
B3: 30,
|
|
402
|
+
C1: '=VLOOKUP("b",A1:B3,2,FALSE)',
|
|
403
|
+
C2: '=XLOOKUP("c",A1:A3,B1:B3)',
|
|
404
|
+
C3: '=INDEX(B1:B3,MATCH("a",A1:A3,0))'
|
|
405
|
+
}
|
|
406
|
+
},
|
|
407
|
+
expect: { C1: 20, C2: 30, C3: 10 }
|
|
408
|
+
},
|
|
409
|
+
{
|
|
410
|
+
name: "dates are serial numbers",
|
|
411
|
+
sheets: { Sheet1: { A1: "=DATE(2026,3,8)", A2: "=EDATE(A1,1)", A3: "=YEAR(A1)" } },
|
|
412
|
+
expect: { A1: 46089, A2: 46120, A3: 2026 }
|
|
413
|
+
},
|
|
414
|
+
{
|
|
415
|
+
name: "errors",
|
|
416
|
+
sheets: { Sheet1: { A1: "=1/0", A2: "=NOPE(1)", A3: "=IFERROR(A1,-1)", A4: "=NA()" } },
|
|
417
|
+
expect: { A1: err("#DIV/0!"), A2: err("#NAME?"), A3: -1, A4: err("#N/A") }
|
|
418
|
+
},
|
|
419
|
+
{
|
|
420
|
+
name: "large and small numbers",
|
|
421
|
+
sheets: { Sheet1: { A1: "=1E+300", A2: "=1E-300", A3: "=-1E+100", A4: "=2^40" } },
|
|
422
|
+
expect: { A1: 1e300, A2: 1e-300, A3: -1e100, A4: 1099511627776 }
|
|
423
|
+
},
|
|
424
|
+
{
|
|
425
|
+
name: "cross-sheet refs and names",
|
|
426
|
+
sheets: {
|
|
427
|
+
Sheet1: { A1: "=Data!A1*Rate", A2: "='Q 2'!B2+1" },
|
|
428
|
+
Data: { A1: 50 },
|
|
429
|
+
"Q 2": { B2: 9 }
|
|
430
|
+
},
|
|
431
|
+
names: { Rate: "Data!$A$1" },
|
|
432
|
+
expect: { A1: 2500, A2: 10 }
|
|
433
|
+
},
|
|
434
|
+
{
|
|
435
|
+
name: "LET and LAMBDA names",
|
|
436
|
+
sheets: { Sheet1: { A1: "=LET(x,4,x*x)", A2: "=DOUBLE(21)" } },
|
|
437
|
+
names: { DOUBLE: "LAMBDA(n,n*2)" },
|
|
438
|
+
expect: { A1: 16, A2: 42 }
|
|
439
|
+
},
|
|
440
|
+
{
|
|
441
|
+
name: "a recalc reaches every dependent",
|
|
442
|
+
sheets: {
|
|
443
|
+
Sheet1: { A1: 1, A2: "=A1+1", A3: "=A2+1", B1: "=SUM(A1:A3)" },
|
|
444
|
+
Other: { A1: "=Sheet1!A3*10" }
|
|
445
|
+
},
|
|
446
|
+
expect: { A3: 3, B1: 6, "Other!A1": 30 },
|
|
447
|
+
then: { set: { A1: 10 }, changes: { A1: 10, A2: 11, A3: 12, B1: 33, "Other!A1": 120 } }
|
|
448
|
+
}
|
|
449
|
+
];
|
|
450
|
+
var ROWS = 20;
|
|
451
|
+
var COLS = 10;
|
|
452
|
+
function address(key, first) {
|
|
453
|
+
const ref = parseA1(key);
|
|
454
|
+
if (!ref || ref.start.row === void 0 || ref.start.col === void 0) {
|
|
455
|
+
throw new Error(`Lattice conformance: "${key}" is not a cell`);
|
|
456
|
+
}
|
|
457
|
+
return { sheet: ref.sheet ?? first, row: ref.start.row, col: ref.start.col };
|
|
458
|
+
}
|
|
459
|
+
function conformanceDoc(c) {
|
|
460
|
+
const names = Object.keys(c.sheets);
|
|
461
|
+
const sid = (name) => `s${names.indexOf(name) + 1}`;
|
|
462
|
+
const axes = names.map((name) => ({
|
|
463
|
+
id: sid(name),
|
|
464
|
+
name,
|
|
465
|
+
rows: Array.from({ length: ROWS }, (_, i) => `${sid(name)}r${i + 1}`),
|
|
466
|
+
cols: Array.from({ length: COLS }, (_, i) => `${sid(name)}c${i + 1}`)
|
|
467
|
+
}));
|
|
468
|
+
const ctx = createRefContext({ sheets: axes });
|
|
469
|
+
const first = names[0];
|
|
470
|
+
const at = (key) => {
|
|
471
|
+
const a = address(key, first);
|
|
472
|
+
const axis2 = axes[names.indexOf(a.sheet)];
|
|
473
|
+
return { sheet: axis2.id, row: axis2.rows[a.row], col: axis2.cols[a.col] };
|
|
474
|
+
};
|
|
475
|
+
const cell = (sheet, input) => is.string(input) && input.startsWith("=") ? { formula: toStored(input, sheet, ctx) } : { value: input };
|
|
476
|
+
const doc = {
|
|
477
|
+
sheets: axes.map((axis2, i) => ({
|
|
478
|
+
...axis2,
|
|
479
|
+
cells: Object.entries(c.sheets[names[i]]).map(([key, input]) => {
|
|
480
|
+
const { row, col } = at(`${quoteless(names[i])}!${key}`);
|
|
481
|
+
return { row, col, ...cell(axis2.id, input) };
|
|
482
|
+
})
|
|
483
|
+
})),
|
|
484
|
+
names: Object.entries(c.names ?? {}).map(([name, ref]) => ({
|
|
485
|
+
name,
|
|
486
|
+
ref: toStored(ref, sid(first), ctx)
|
|
487
|
+
}))
|
|
488
|
+
};
|
|
489
|
+
const set = (cells) => Object.entries(cells).map(([key, input]) => {
|
|
490
|
+
const pos = at(key);
|
|
491
|
+
return { type: "setCell", ...pos, ...cell(pos.sheet, input) };
|
|
492
|
+
});
|
|
493
|
+
return { doc, at, ctx, set, first: sid(first) };
|
|
494
|
+
}
|
|
495
|
+
var quoteless = (name) => `'${name.replace(/'/g, "''")}'`;
|
|
496
|
+
async function runConformance(create, cases = CONFORMANCE_CASES) {
|
|
497
|
+
const failures = [];
|
|
498
|
+
const same = (a, b) => JSON.stringify(a) === JSON.stringify(b) || is.number(a) && is.number(b) && Math.abs(a - b) <= Math.abs(b) * 1e-14;
|
|
499
|
+
for (const c of cases) {
|
|
500
|
+
const engine = await create();
|
|
501
|
+
const { doc, at, set, first } = conformanceDoc(c);
|
|
502
|
+
await engine.load(doc);
|
|
503
|
+
for (const [key, expected] of Object.entries(c.expect)) {
|
|
504
|
+
const { sheet, row, col } = at(key);
|
|
505
|
+
const actual = await engine.evaluate(first, `={{${sheet}!${col},${row}}}`);
|
|
506
|
+
if (!same(actual, expected)) failures.push({ name: c.name, cell: key, expected, actual });
|
|
507
|
+
}
|
|
508
|
+
if (c.then) {
|
|
509
|
+
const changes = await engine.apply(set(c.then.set));
|
|
510
|
+
for (const [key, expected] of Object.entries(c.then.changes)) {
|
|
511
|
+
const pos = at(key);
|
|
512
|
+
const change = changes.find(
|
|
513
|
+
(ch) => ch.sheet === pos.sheet && ch.row === pos.row && ch.col === pos.col
|
|
514
|
+
);
|
|
515
|
+
if (!change || !same(change.value, expected)) {
|
|
516
|
+
failures.push({ name: `${c.name} (then)`, cell: key, expected, actual: change?.value });
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
engine.dispose?.();
|
|
521
|
+
}
|
|
522
|
+
return failures;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
export { CONFORMANCE_CASES, MAX_COLS, MAX_ROWS, REF_SOURCE, columnIndex, columnLabel, conformanceDoc, createAxis, createRefContext, formulaNames, fromR1C1, parseA1, printA1, printStored, quoteSheet, refFromMatch, resolveRef, runConformance, scanFormula, scanStored, scanWith, shrinkDeleted, storedRefs, toA1, toR1C1, toStored };
|
|
526
|
+
//# sourceMappingURL=index.js.map
|
|
527
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/a1.ts","../src/scan.ts","../src/refs.ts","../src/r1c1.ts","../src/conformance.ts"],"names":["j","axis","SHEET"],"mappings":";;;AAEO,IAAM,QAAA,GAAW;AACjB,IAAM,QAAA,GAAW;AAiBjB,SAAS,YAAY,KAAA,EAAuB;AACjD,EAAA,IAAI,KAAA,GAAQ,EAAA;AACZ,EAAA,KAAA,IAAS,CAAA,GAAI,KAAA,GAAQ,CAAA,EAAG,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,IAAA,CAAK,KAAA,CAAA,CAAO,CAAA,GAAI,CAAA,IAAK,EAAE,CAAA,EAAG;AAC3D,IAAA,KAAA,GAAQ,OAAO,YAAA,CAAa,EAAA,GAAA,CAAO,CAAA,GAAI,CAAA,IAAK,EAAG,CAAA,GAAI,KAAA;AAAA,EACrD;AACA,EAAA,OAAO,KAAA;AACT;AAGO,SAAS,YAAY,KAAA,EAAuB;AACjD,EAAA,IAAI,CAAC,iBAAA,CAAkB,IAAA,CA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,EAAM,CAAA,EAAG,CAAA,CAAE,IAAI,CAAA,OAAA,CAAA,EAAW,IAAA,EAAM,GAAA,EAAK,QAAA,EAAU,MAAA,EAAQ,MAAA,EAAQ,OAAO,CAAA;AAAA,QACxF;AAAA,MACF;AAAA,IACF;AACA,IAAA,MAAA,CAAO,OAAA,IAAU;AAAA,EACnB;AACA,EAAA,OAAO,QAAA;AACT","file":"index.js","sourcesContent":["// A1 refs for the editor and for import and export. Indices are 0-based.\n\nexport const MAX_ROWS = 1_048_576\nexport const MAX_COLS = 16_384\n\n/** One end of a ref. A whole-column ref has no `row`; a whole-row ref has no `col`. */\nexport interface A1Part {\n row?: number\n col?: number\n rowAbs: boolean\n colAbs: boolean\n}\n\n/** A cell, a range, whole columns (`A:C`) or whole rows (`2:5`), on an optional sheet. */\nexport interface A1Ref {\n sheet?: string\n start: A1Part\n end?: A1Part\n}\n\nexport function columnLabel(index: number): string {\n let label = ''\n for (let n = index + 1; n > 0; n = Math.floor((n - 1) / 26)) {\n label = String.fromCharCode(65 + ((n - 1) % 26)) + label\n }\n return label\n}\n\n/** The 0-based index of a column label, or -1 when it is not a column up to XFD. */\nexport function columnIndex(label: string): number {\n if (!/^[A-Za-z]{1,3}$/.test(label)) return -1\n let n = 0\n for (const ch of label.toUpperCase()) n = n * 26 + ch.charCodeAt(0) - 64\n return n <= MAX_COLS ? n - 1 : -1\n}\n\nconst SHEET = String.raw`(?:'((?:[^']|'')+)'|([A-Za-z_][\\w.]*))!`\nconst CELL = String.raw`(\\$?)([A-Za-z]{1,3})(\\$?)(\\d+)`\nconst COL = String.raw`(\\$?)([A-Za-z]{1,3})`\nconst ROW = String.raw`(\\$?)(\\d+)`\n\n/** The ref patterns. The scanner uses them with the `y` flag at each token start. */\nexport const REF_SOURCE =\n `(?:${SHEET})?(?:${CELL}(?::${CELL})?|${COL}:${COL}|${ROW}:${ROW})` + String.raw`(?![\\w.(!\\[])`\n\nconst EXACT = new RegExp(`^${REF_SOURCE}$`)\n\nfunction cellPart(abs1: string, col: string, abs2: string, row: string): A1Part | undefined {\n const c = columnIndex(col)\n const r = Number(row) - 1\n if (c < 0 || r < 0 || r >= MAX_ROWS) return undefined\n return { col: c, row: r, colAbs: abs1 === '$', rowAbs: abs2 === '$' }\n}\n\n/** Reads a match of `REF_SOURCE`. Gives undefined when an index is out of the sheet. */\nexport function refFromMatch(m: RegExpExecArray | RegExpMatchArray): A1Ref | undefined {\n const g = m.slice(1).map(v => v ?? '')\n const sheet = g[0] ? g[0].replace(/''/g, \"'\") : g[1] || undefined\n let start: A1Part | undefined\n let end: A1Part | undefined\n if (g[3]) {\n start = cellPart(g[2]!, g[3], g[4]!, g[5]!)\n end = g[7] ? cellPart(g[6]!, g[7], g[8]!, g[9]!) : undefined\n if (g[7] && !end) return undefined\n } else if (g[11]) {\n const a = columnIndex(g[11])\n const b = columnIndex(g[13]!)\n if (a < 0 || b < 0) return undefined\n start = { col: a, colAbs: g[10] === '$', rowAbs: false }\n end = { col: b, colAbs: g[12] === '$', rowAbs: false }\n } else {\n const a = Number(g[15]) - 1\n const b = Number(g[17]) - 1\n if (a < 0 || b < 0 || a >= MAX_ROWS || b >= MAX_ROWS) return undefined\n start = { row: a, rowAbs: g[14] === '$', colAbs: false }\n end = { row: b, rowAbs: g[16] === '$', colAbs: false }\n }\n if (!start) return undefined\n const ref: A1Ref = { start }\n if (sheet !== undefined) ref.sheet = sheet\n if (end) ref.end = end\n return ref\n}\n\nexport function parseA1(text: string): A1Ref | undefined {\n const m = EXACT.exec(text)\n return m ? refFromMatch(m) : undefined\n}\n\nexport function quoteSheet(name: string): string {\n return /^[A-Za-z_][\\w.]*$/.test(name) && !/^[A-Za-z]{1,3}\\d+$|^R\\d*C\\d*$/i.test(name)\n ? name\n : `'${name.replace(/'/g, \"''\")}'`\n}\n\nfunction printPart(part: A1Part): string {\n const col = part.col === undefined ? '' : (part.colAbs ? '$' : '') + columnLabel(part.col)\n const row = part.row === undefined ? '' : (part.rowAbs ? '$' : '') + (part.row + 1)\n return col + row\n}\n\nexport function printA1(ref: A1Ref): string {\n const sheet = ref.sheet === undefined ? '' : `${quoteSheet(ref.sheet)}!`\n return sheet + printPart(ref.start) + (ref.end ? `:${printPart(ref.end)}` : '')\n}\n","import { REF_SOURCE, refFromMatch } from './a1'\nimport type { A1Ref } from './a1'\n\nexport type Token<T> = { kind: 'text'; text: string } | { kind: 'ref'; text: string; ref: T }\n\nconst WORD = /[\\w.$]/\n\n/**\n * Splits a formula into text and ref tokens. The tokens join back to the input. Refs are found\n * only at a token start and never in a string literal. `read` turns a match of `pattern` into a\n * ref, or gives undefined when the match is not a ref (for example a column after XFD).\n */\nexport function scanWith<T>(\n formula: string,\n pattern: RegExp,\n read: (m: RegExpExecArray) => T | undefined,\n): Token<T>[] {\n const tokens: Token<T>[] = []\n let text = ''\n let i = 0\n const flush = () => {\n if (text) tokens.push({ kind: 'text', text })\n text = ''\n }\n while (i < formula.length) {\n const ch = formula[i]!\n if (ch === '\"') {\n let j = i + 1\n while (j < formula.length && !(formula[j] === '\"' && formula[j + 1] !== '\"')) {\n j += formula[j] === '\"' ? 2 : 1\n }\n text += formula.slice(i, j + 1)\n i = j + 1\n continue\n }\n const prev = formula[i - 1] ?? ''\n if (!WORD.test(prev) && prev !== \"'\" && prev !== ']' && prev !== '}') {\n pattern.lastIndex = i\n const m = pattern.exec(formula)\n const ref = m ? read(m) : undefined\n if (m && ref !== undefined) {\n flush()\n tokens.push({ kind: 'ref', text: m[0], ref })\n i += m[0].length\n continue\n }\n }\n // Skip the rest of a name, so that `ABCD1` or `Tax2` is never read in part.\n let j = i + 1\n if (WORD.test(ch)) while (j < formula.length && WORD.test(formula[j]!)) j++\n text += formula.slice(i, j)\n i = j\n }\n flush()\n return tokens\n}\n\nconst A1_PATTERN = new RegExp(REF_SOURCE, 'y')\n\n/** Splits an A1 formula into text and ref tokens (for the editor and for conversion). */\nexport function scanFormula(formula: string): Token<A1Ref>[] {\n return scanWith(formula, A1_PATTERN, refFromMatch)\n}\n\n/**\n * The names (named ranges, `LET` and `LAMBDA` names) in an A1 formula, for the editor. A name\n * is an identifier that is not a ref, not a function call and not `TRUE` or `FALSE`. Each name\n * shows once, in its first spelling (names do not care about case).\n */\nexport function formulaNames(formula: string): string[] {\n const seen = new Map<string, string>()\n for (const token of scanFormula(formula)) {\n if (token.kind === 'ref') continue\n const text = token.text.replace(/\"(?:[^\"]|\"\")*\"?/g, '\"\"')\n for (const m of text.matchAll(/(?<![\\w.$'!])[A-Za-z_\\\\][\\w.]*(?![\\w.!]|\\s*\\()/g)) {\n const key = m[0].toUpperCase()\n if (key !== 'TRUE' && key !== 'FALSE' && !seen.has(key)) seen.set(key, m[0])\n }\n }\n return [...seen.values()]\n}\n","// The stored form of a formula: refs by row and column ID, so they stay valid when rows and\n// columns move. See docs/adr/0015-sheet-model/index.html.\n//\n// {{c7,r42}} one cell (A1: a relative cell)\n// {{$c7,$r42}} one cell with both `$` flags\n// {{s2!c1,r1:c3,r9}} a range on the sheet with ID `s2`\n// {{c1,:c3,}} whole columns (A1: `A:C`)\n// {{,r1:,r3}} whole rows (A1: `1:3`)\n//\n// `{{` cannot occur in an Excel formula outside a string, so the form is unambiguous.\nimport { printA1 } from './a1'\nimport type { A1Part, A1Ref } from './a1'\nimport { scanFormula, scanWith } from './scan'\n\nexport interface IdPart {\n row?: string\n col?: string\n rowAbs: boolean\n colAbs: boolean\n}\n\nexport interface IdRef {\n sheet?: string\n start: IdPart\n end?: IdPart\n}\n\n/** The order of the rows or the columns of one sheet. */\nexport interface Axis {\n id(index: number): string | undefined\n /** The index of an ID, or -1 when the ID is not on the axis (for example, deleted). */\n index(id: string): number\n}\n\nexport interface RefContext {\n sheetId(name: string): string | undefined\n sheetName(id: string): string | undefined\n rows(sheetId: string): Axis | undefined\n cols(sheetId: string): Axis | undefined\n}\n\nexport interface SheetAxes {\n id: string\n name: string\n rows: readonly string[]\n cols: readonly string[]\n}\n\nexport function createAxis(ids: readonly string[]): Axis {\n let map: Map<string, number> | undefined\n return {\n id: index => ids[index],\n index: id => (map ??= new Map(ids.map((v, i) => [v, i]))).get(id) ?? -1,\n }\n}\n\n/** A `RefContext` on a snapshot of the sheet axes. Make a new one after the axes change. */\nexport function createRefContext(doc: { sheets: readonly SheetAxes[] }): RefContext {\n const byId = new Map(doc.sheets.map(s => [s.id, s]))\n const byName = new Map(doc.sheets.map(s => [s.name.toUpperCase(), s.id]))\n const axes = new Map<string, { rows: Axis; cols: Axis }>()\n const get = (id: string) => {\n const sheet = byId.get(id)\n if (!sheet) return undefined\n let a = axes.get(id)\n if (!a) axes.set(id, (a = { rows: createAxis(sheet.rows), cols: createAxis(sheet.cols) }))\n return a\n }\n return {\n sheetId: name => byName.get(name.toUpperCase()),\n sheetName: id => byId.get(id)?.name,\n rows: id => get(id)?.rows,\n cols: id => get(id)?.cols,\n }\n}\n\nconst ID = /^[\\w.-]+$/\nconst ID_SRC = String.raw`(\\$?)([\\w.-]*),(\\$?)([\\w.-]*)`\nconst STORED = new RegExp(String.raw`\\{\\{(?:([\\w.-]+)!)?${ID_SRC}(?::${ID_SRC})?\\}\\}`, 'y')\n\nfunction idPart(colAbs: string, col: string, rowAbs: string, row: string): IdPart {\n const part: IdPart = { rowAbs: rowAbs === '$', colAbs: colAbs === '$' }\n if (row) part.row = row\n if (col) part.col = col\n return part\n}\n\nfunction readStored(m: RegExpExecArray): IdRef {\n const g = m.slice(1).map(v => v ?? '')\n const ref: IdRef = { start: idPart(g[1]!, g[2]!, g[3]!, g[4]!) }\n if (g[0]) ref.sheet = g[0]\n if (m[6] !== undefined) ref.end = idPart(g[5]!, g[6]!, g[7]!, g[8]!)\n return ref\n}\n\nfunction checkId(id: string): string {\n if (!ID.test(id)) throw new Error(`Lattice: the ID \"${id}\" must match ${ID}`)\n return id\n}\n\nexport function printStored(ref: IdRef): string {\n const part = (p: IdPart) =>\n (p.col === undefined ? '' : (p.colAbs ? '$' : '') + checkId(p.col)) +\n ',' +\n (p.row === undefined ? '' : (p.rowAbs ? '$' : '') + checkId(p.row))\n const sheet = ref.sheet === undefined ? '' : `${checkId(ref.sheet)}!`\n return `{{${sheet}${part(ref.start)}${ref.end ? `:${part(ref.end)}` : ''}}}`\n}\n\n/** Splits a stored formula into text and ID-ref tokens. */\nexport function scanStored(stored: string) {\n return scanWith(stored, STORED, readStored)\n}\n\n/** The ID refs in a stored formula, in order. */\nexport function storedRefs(stored: string): IdRef[] {\n return scanStored(stored).flatMap(t => (t.kind === 'ref' ? [t.ref] : []))\n}\n\nconst REF_ERROR = '#REF!'\n\nfunction toIdPart(part: A1Part, rows: Axis, cols: Axis): IdPart | undefined {\n const out: IdPart = { rowAbs: part.rowAbs, colAbs: part.colAbs }\n if (part.row !== undefined) {\n const id = rows.id(part.row)\n if (id === undefined) return undefined\n out.row = id\n }\n if (part.col !== undefined) {\n const id = cols.id(part.col)\n if (id === undefined) return undefined\n out.col = id\n }\n return out\n}\n\n/** Converts the A1 refs of a formula on sheet `sheetId` to the stored ID form. */\nexport function toStored(formula: string, sheetId: string, ctx: RefContext): string {\n return scanFormula(formula)\n .map(token => {\n if (token.kind === 'text') return token.text\n const ref = token.ref\n const sid = ref.sheet === undefined ? sheetId : ctx.sheetId(ref.sheet)\n const rows = sid === undefined ? undefined : ctx.rows(sid)\n const cols = sid === undefined ? undefined : ctx.cols(sid)\n if (!rows || !cols || sid === undefined) return REF_ERROR\n const start = toIdPart(ref.start, rows, cols)\n const end = ref.end && toIdPart(ref.end, rows, cols)\n if (!start || (ref.end && !end)) return REF_ERROR\n const out: IdRef = { start }\n if (ref.sheet !== undefined) out.sheet = sid\n if (end) out.end = end\n return printStored(out)\n })\n .join('')\n}\n\nfunction toA1Part(part: IdPart, rows: Axis, cols: Axis): A1Part | undefined {\n const out: A1Part = { rowAbs: part.rowAbs, colAbs: part.colAbs }\n if (part.row !== undefined) {\n const i = rows.index(part.row)\n if (i < 0) return undefined\n out.row = i\n }\n if (part.col !== undefined) {\n const i = cols.index(part.col)\n if (i < 0) return undefined\n out.col = i\n }\n return out\n}\n\n/** Resolves a stored ref to A1 indices, or undefined when a row, column or sheet is gone. */\nexport function resolveRef(ref: IdRef, sheetId: string, ctx: RefContext): A1Ref | undefined {\n const sid = ref.sheet ?? sheetId\n const rows = ctx.rows(sid)\n const cols = ctx.cols(sid)\n const name = ctx.sheetName(sid)\n if (!rows || !cols || name === undefined) return undefined\n const start = toA1Part(ref.start, rows, cols)\n const end = ref.end && toA1Part(ref.end, rows, cols)\n if (!start || (ref.end && !end)) return undefined\n const out: A1Ref = { start }\n if (ref.sheet !== undefined) out.sheet = name\n if (end) out.end = end\n return out\n}\n\n/** Converts a stored formula on sheet `sheetId` to A1 text. A lost ref prints as `#REF!`. */\nexport function toA1(stored: string, sheetId: string, ctx: RefContext): string {\n return scanStored(stored)\n .map(token => {\n if (token.kind === 'text') return token.text\n const ref = resolveRef(token.ref, sheetId, ctx)\n return ref ? printA1(ref) : REF_ERROR\n })\n .join('')\n}\n\nexport interface Deletion {\n sheet: string\n rows?: readonly string[]\n cols?: readonly string[]\n}\n\n/**\n * Moves each range end that a delete removes to the nearest row or column that stays in the\n * range, as Excel does. Call it with the context from before the delete. A single-cell ref\n * and a range that loses all its rows stay as they are, and print as `#REF!` after the delete.\n */\nexport function shrinkDeleted(\n stored: string,\n sheetId: string,\n ctx: RefContext,\n deletion: Deletion,\n): string {\n return scanStored(stored)\n .map(token => {\n if (token.kind === 'text') return token.text\n const ref = token.ref\n if ((ref.sheet ?? sheetId) !== deletion.sheet || !ref.end) return token.text\n const out: IdRef = { ...ref, start: { ...ref.start }, end: { ...ref.end } }\n for (const [key, ids] of [\n ['row', deletion.rows],\n ['col', deletion.cols],\n ] as const) {\n if (!ids?.length) continue\n const gone = new Set(ids)\n const axis = key === 'row' ? ctx.rows(deletion.sheet) : ctx.cols(deletion.sheet)\n const a = out.start[key]\n const b = out.end![key]\n if (!axis || a === undefined || b === undefined) continue\n const from = axis.index(a)\n const to = axis.index(b)\n const pick = (i: number, step: number) => {\n for (; step > 0 ? i <= to : i >= from; i += step) {\n const id = axis.id(i)\n if (id !== undefined && !gone.has(id)) return id\n }\n return undefined\n }\n if (gone.has(a)) {\n const id = pick(from, 1)\n if (id !== undefined) out.start[key] = id\n }\n if (gone.has(b)) {\n const id = pick(to, -1)\n if (id !== undefined) out.end![key] = id\n }\n }\n return printStored(out)\n })\n .join('')\n}\n","// R1C1 text for the editor: relative refs are offsets from a base cell (0-based indices).\nimport { MAX_COLS, MAX_ROWS, printA1, quoteSheet } from './a1'\nimport type { A1Part, A1Ref } from './a1'\nimport { scanFormula, scanWith } from './scan'\n\nexport interface CellIndex {\n row: number\n col: number\n}\n\nfunction axis(letter: 'R' | 'C', abs: boolean, value: number, base: number): string {\n if (abs) return `${letter}${value + 1}`\n const offset = value - base\n return offset === 0 ? letter : `${letter}[${offset}]`\n}\n\nfunction partR1C1(part: A1Part, base: CellIndex): string {\n const r = part.row === undefined ? '' : axis('R', part.rowAbs, part.row, base.row)\n const c = part.col === undefined ? '' : axis('C', part.colAbs, part.col, base.col)\n return r + c\n}\n\n/** Converts the A1 refs of a formula to R1C1, relative to the cell at `base`. */\nexport function toR1C1(formula: string, base: CellIndex): string {\n return scanFormula(formula)\n .map(token => {\n if (token.kind === 'text') return token.text\n const { sheet, start, end } = token.ref\n const prefix = sheet === undefined ? '' : `${quoteSheet(sheet)}!`\n return prefix + partR1C1(start, base) + (end ? `:${partR1C1(end, base)}` : '')\n })\n .join('')\n}\n\nconst SHEET = String.raw`(?:'((?:[^']|'')+)'|([A-Za-z_][\\w.]*))!`\nconst AXIS = (l: string) => String.raw`${l}(?:\\[(-?\\d+)\\]|(\\d+))?`\nconst PART = `(?:${AXIS('R')}${AXIS('C')}|${AXIS('R')}(?![\\\\w.])|${AXIS('C')})`\nconst R1C1 = new RegExp(`(?:${SHEET})?${PART}(?::${PART})?(?![\\\\w.(!\\\\[])`, 'y')\n\n// Groups in one PART: [rRel, rAbs, cRel, cAbs] (cell), [rRel, rAbs] (row), [cRel, cAbs] (col).\nconst PART_GROUPS = 8\n\nfunction readValue(rel: string | undefined, abs: string | undefined, base: number) {\n if (abs !== undefined) return { value: Number(abs) - 1, abs: true }\n return { value: base + Number(rel ?? 0), abs: false }\n}\n\nfunction readPart(g: (string | undefined)[], base: CellIndex): A1Part | undefined {\n const part: A1Part = { rowAbs: false, colAbs: false }\n const setRow = (rel?: string, abs?: string) => {\n const v = readValue(rel, abs, base.row)\n part.row = v.value\n part.rowAbs = v.abs\n }\n const setCol = (rel?: string, abs?: string) => {\n const v = readValue(rel, abs, base.col)\n part.col = v.value\n part.colAbs = v.abs\n }\n if (g[8] === 'cell') {\n setRow(g[0], g[1])\n setCol(g[2], g[3])\n } else if (g[8] === 'row') setRow(g[4], g[5])\n else setCol(g[6], g[7])\n const bad = (v: number | undefined, max: number) => v !== undefined && (v < 0 || v >= max)\n return bad(part.row, MAX_ROWS) || bad(part.col, MAX_COLS) ? undefined : part\n}\n\n// Which branch of PART matched: the text decides, since a bare `R` or `C` has no groups.\nfunction kind(text: string): 'cell' | 'row' | 'col' {\n if (/^R(\\[-?\\d+\\]|\\d+)?C/.test(text)) return 'cell'\n return text.startsWith('R') ? 'row' : 'col'\n}\n\n/** Converts the R1C1 refs of a formula to A1, relative to the cell at `base`. */\nexport function fromR1C1(formula: string, base: CellIndex): string {\n return scanWith(formula, R1C1, m => {\n const sheet = m[1] !== undefined ? m[1].replace(/''/g, \"'\") : m[2]\n const body = m[0].slice(m[0].lastIndexOf('!') + 1)\n const [first, second] = body.split(':')\n const start = readPart([...m.slice(3, 3 + PART_GROUPS), kind(first!)], base)\n // A lone `R2` or `C3` is a whole row or column: A1 writes it as `2:2` or `C:C`.\n const end =\n second === undefined\n ? kind(first!) === 'cell'\n ? undefined\n : start\n : readPart([...m.slice(3 + PART_GROUPS, 3 + 2 * PART_GROUPS), kind(second)], base)\n const ref: A1Ref | undefined =\n start && (second === undefined || end) ? { start, ...(end ? { end } : {}) } : undefined\n if (ref && sheet !== undefined) ref.sheet = sheet\n return ref ? printA1(ref) : '#REF!'\n })\n .map(token => (token.kind === 'ref' ? token.ref : token.text))\n .join('')\n}\n","// The shared formula conformance suite. Each engine adapter runs the same cases, so all\n// adapters give the same results (plan 8.3). Cases are in A1 for the reader; the runner\n// converts them to the stored ID form.\nimport is from '@sindresorhus/is'\nimport { parseA1 } from './a1'\nimport { createRefContext, toStored } from './refs'\nimport type { CellChange, CellData, CellValue, FormulaEngine, SheetDoc, SheetOp } from './types'\n\ntype Input = number | string | boolean\n/** A1 address (`B2` or `Other!B2`) to an input. A string that starts with `=` is a formula. */\ntype Cells = Record<string, Input>\n\nexport interface ConformanceCase {\n name: string\n /** Sheet name to cells. The first sheet is the current sheet. */\n sheets: Record<string, Cells>\n names?: Record<string, string>\n expect: Record<string, CellValue>\n /** A second step: set these cells, then check `apply` gives these changes (and maybe more). */\n then?: { set: Cells; changes: Record<string, CellValue> }\n}\n\nexport interface ConformanceFailure {\n name: string\n cell: string\n expected: CellValue | 'a change'\n actual: CellValue | undefined\n}\n\nconst err = (error: string) => ({ error })\n\nexport const CONFORMANCE_CASES: ConformanceCase[] = [\n {\n name: 'arithmetic and precedence',\n sheets: { Sheet1: { A1: 2, A2: 3, B1: '=A1+A2*2', B2: '=-A1^2', B3: '=(A1+A2)/4' } },\n expect: { B1: 8, B2: 4, B3: 1.25 },\n },\n {\n name: 'aggregates',\n sheets: { Sheet1: { A1: 1, A2: 2, A3: 3, A4: 'x', B1: '=SUM(A1:A4)', B2: '=AVERAGE(A1:A3)' } },\n expect: { B1: 6, B2: 2 },\n },\n {\n name: 'counts and conditions',\n sheets: {\n Sheet1: { A1: 5, A2: 15, A3: 25, B1: '=COUNTIF(A1:A3,\">10\")', B2: '=SUMIF(A1:A3,\">10\")' },\n },\n expect: { B1: 2, B2: 40 },\n },\n {\n name: 'logic and text',\n sheets: {\n Sheet1: {\n A1: 'Lattice',\n B1: '=IF(LEN(A1)>5,UPPER(A1),\"short\")',\n B2: '=LEFT(A1,3)&\"-\"&RIGHT(A1,2)',\n B3: '=AND(TRUE,NOT(FALSE))',\n B4: '=TEXTJOIN(\",\",TRUE,\"a\",\"\",\"b\")',\n },\n },\n expect: { B1: 'LATTICE', B2: 'Lat-ce', B3: true, B4: 'a,b' },\n },\n {\n name: 'lookups',\n sheets: {\n Sheet1: {\n A1: 'a',\n A2: 'b',\n A3: 'c',\n B1: 10,\n B2: 20,\n B3: 30,\n C1: '=VLOOKUP(\"b\",A1:B3,2,FALSE)',\n C2: '=XLOOKUP(\"c\",A1:A3,B1:B3)',\n C3: '=INDEX(B1:B3,MATCH(\"a\",A1:A3,0))',\n },\n },\n expect: { C1: 20, C2: 30, C3: 10 },\n },\n {\n name: 'dates are serial numbers',\n sheets: { Sheet1: { A1: '=DATE(2026,3,8)', A2: '=EDATE(A1,1)', A3: '=YEAR(A1)' } },\n expect: { A1: 46089, A2: 46120, A3: 2026 },\n },\n {\n name: 'errors',\n sheets: { Sheet1: { A1: '=1/0', A2: '=NOPE(1)', A3: '=IFERROR(A1,-1)', A4: '=NA()' } },\n expect: { A1: err('#DIV/0!'), A2: err('#NAME?'), A3: -1, A4: err('#N/A') },\n },\n {\n name: 'large and small numbers',\n sheets: { Sheet1: { A1: '=1E+300', A2: '=1E-300', A3: '=-1E+100', A4: '=2^40' } },\n expect: { A1: 1e300, A2: 1e-300, A3: -1e100, A4: 1099511627776 },\n },\n {\n name: 'cross-sheet refs and names',\n sheets: {\n Sheet1: { A1: '=Data!A1*Rate', A2: \"='Q 2'!B2+1\" },\n Data: { A1: 50 },\n 'Q 2': { B2: 9 },\n },\n names: { Rate: 'Data!$A$1' },\n expect: { A1: 2500, A2: 10 },\n },\n {\n name: 'LET and LAMBDA names',\n sheets: { Sheet1: { A1: '=LET(x,4,x*x)', A2: '=DOUBLE(21)' } },\n names: { DOUBLE: 'LAMBDA(n,n*2)' },\n expect: { A1: 16, A2: 42 },\n },\n {\n name: 'a recalc reaches every dependent',\n sheets: {\n Sheet1: { A1: 1, A2: '=A1+1', A3: '=A2+1', B1: '=SUM(A1:A3)' },\n Other: { A1: '=Sheet1!A3*10' },\n },\n expect: { A3: 3, B1: 6, 'Other!A1': 30 },\n then: { set: { A1: 10 }, changes: { A1: 10, A2: 11, A3: 12, B1: 33, 'Other!A1': 120 } },\n },\n]\n\nconst ROWS = 20\nconst COLS = 10\n\nfunction address(key: string, first: string) {\n const ref = parseA1(key)\n if (!ref || ref.start.row === undefined || ref.start.col === undefined) {\n throw new Error(`Lattice conformance: \"${key}\" is not a cell`)\n }\n return { sheet: ref.sheet ?? first, row: ref.start.row, col: ref.start.col }\n}\n\n/** Builds the document of a case, with IDs `<sheet>r<n>` and `<sheet>c<n>`. */\nexport function conformanceDoc(c: ConformanceCase) {\n const names = Object.keys(c.sheets)\n const sid = (name: string) => `s${names.indexOf(name) + 1}`\n const axes = names.map(name => ({\n id: sid(name),\n name,\n rows: Array.from({ length: ROWS }, (_, i) => `${sid(name)}r${i + 1}`),\n cols: Array.from({ length: COLS }, (_, i) => `${sid(name)}c${i + 1}`),\n }))\n const ctx = createRefContext({ sheets: axes })\n const first = names[0]!\n const at = (key: string) => {\n const a = address(key, first)\n const axis = axes[names.indexOf(a.sheet)]!\n return { sheet: axis.id, row: axis.rows[a.row]!, col: axis.cols[a.col]! }\n }\n const cell = (sheet: string, input: Input): Omit<CellData, 'row' | 'col'> =>\n is.string(input) && input.startsWith('=')\n ? { formula: toStored(input, sheet, ctx) }\n : { value: input }\n const doc: SheetDoc = {\n sheets: axes.map((axis, i) => ({\n ...axis,\n cells: Object.entries(c.sheets[names[i]!]!).map(([key, input]) => {\n const { row, col } = at(`${quoteless(names[i]!)}!${key}`)\n return { row, col, ...cell(axis.id, input) }\n }),\n })),\n names: Object.entries(c.names ?? {}).map(([name, ref]) => ({\n name,\n ref: toStored(ref, sid(first), ctx),\n })),\n }\n const set = (cells: Cells): SheetOp[] =>\n Object.entries(cells).map(([key, input]) => {\n const pos = at(key)\n return { type: 'setCell', ...pos, ...cell(pos.sheet, input) }\n })\n return { doc, at, ctx, set, first: sid(first) }\n}\n\nconst quoteless = (name: string) => `'${name.replace(/'/g, \"''\")}'`\n\n/** Runs every case on a new engine from `create`. Gives the failures; empty means it passed. */\nexport async function runConformance(\n create: () => Promise<FormulaEngine>,\n cases: ConformanceCase[] = CONFORMANCE_CASES,\n): Promise<ConformanceFailure[]> {\n const failures: ConformanceFailure[] = []\n const same = (a: CellValue | undefined, b: CellValue) =>\n JSON.stringify(a) === JSON.stringify(b) ||\n (is.number(a) && is.number(b) && Math.abs(a - b) <= Math.abs(b) * 1e-14)\n for (const c of cases) {\n const engine = await create()\n const { doc, at, set, first } = conformanceDoc(c)\n await engine.load(doc)\n for (const [key, expected] of Object.entries(c.expect)) {\n const { sheet, row, col } = at(key)\n const actual = await engine.evaluate(first, `={{${sheet}!${col},${row}}}`)\n if (!same(actual, expected)) failures.push({ name: c.name, cell: key, expected, actual })\n }\n if (c.then) {\n const changes: CellChange[] = await engine.apply(set(c.then.set))\n for (const [key, expected] of Object.entries(c.then.changes)) {\n const pos = at(key)\n const change = changes.find(\n ch => ch.sheet === pos.sheet && ch.row === pos.row && ch.col === pos.col,\n )\n if (!change || !same(change.value, expected)) {\n failures.push({ name: `${c.name} (then)`, cell: key, expected, actual: change?.value })\n }\n }\n }\n engine.dispose?.()\n }\n return failures\n}\n"]}
|
package/dist/r1c1.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface CellIndex {
|
|
2
|
+
row: number;
|
|
3
|
+
col: number;
|
|
4
|
+
}
|
|
5
|
+
/** Converts the A1 refs of a formula to R1C1, relative to the cell at `base`. */
|
|
6
|
+
export declare function toR1C1(formula: string, base: CellIndex): string;
|
|
7
|
+
/** Converts the R1C1 refs of a formula to A1, relative to the cell at `base`. */
|
|
8
|
+
export declare function fromR1C1(formula: string, base: CellIndex): string;
|
package/dist/refs.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { A1Ref } from './a1';
|
|
2
|
+
export interface IdPart {
|
|
3
|
+
row?: string;
|
|
4
|
+
col?: string;
|
|
5
|
+
rowAbs: boolean;
|
|
6
|
+
colAbs: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface IdRef {
|
|
9
|
+
sheet?: string;
|
|
10
|
+
start: IdPart;
|
|
11
|
+
end?: IdPart;
|
|
12
|
+
}
|
|
13
|
+
/** The order of the rows or the columns of one sheet. */
|
|
14
|
+
export interface Axis {
|
|
15
|
+
id(index: number): string | undefined;
|
|
16
|
+
/** The index of an ID, or -1 when the ID is not on the axis (for example, deleted). */
|
|
17
|
+
index(id: string): number;
|
|
18
|
+
}
|
|
19
|
+
export interface RefContext {
|
|
20
|
+
sheetId(name: string): string | undefined;
|
|
21
|
+
sheetName(id: string): string | undefined;
|
|
22
|
+
rows(sheetId: string): Axis | undefined;
|
|
23
|
+
cols(sheetId: string): Axis | undefined;
|
|
24
|
+
}
|
|
25
|
+
export interface SheetAxes {
|
|
26
|
+
id: string;
|
|
27
|
+
name: string;
|
|
28
|
+
rows: readonly string[];
|
|
29
|
+
cols: readonly string[];
|
|
30
|
+
}
|
|
31
|
+
export declare function createAxis(ids: readonly string[]): Axis;
|
|
32
|
+
/** A `RefContext` on a snapshot of the sheet axes. Make a new one after the axes change. */
|
|
33
|
+
export declare function createRefContext(doc: {
|
|
34
|
+
sheets: readonly SheetAxes[];
|
|
35
|
+
}): RefContext;
|
|
36
|
+
export declare function printStored(ref: IdRef): string;
|
|
37
|
+
/** Splits a stored formula into text and ID-ref tokens. */
|
|
38
|
+
export declare function scanStored(stored: string): import("./scan").Token<IdRef>[];
|
|
39
|
+
/** The ID refs in a stored formula, in order. */
|
|
40
|
+
export declare function storedRefs(stored: string): IdRef[];
|
|
41
|
+
/** Converts the A1 refs of a formula on sheet `sheetId` to the stored ID form. */
|
|
42
|
+
export declare function toStored(formula: string, sheetId: string, ctx: RefContext): string;
|
|
43
|
+
/** Resolves a stored ref to A1 indices, or undefined when a row, column or sheet is gone. */
|
|
44
|
+
export declare function resolveRef(ref: IdRef, sheetId: string, ctx: RefContext): A1Ref | undefined;
|
|
45
|
+
/** Converts a stored formula on sheet `sheetId` to A1 text. A lost ref prints as `#REF!`. */
|
|
46
|
+
export declare function toA1(stored: string, sheetId: string, ctx: RefContext): string;
|
|
47
|
+
export interface Deletion {
|
|
48
|
+
sheet: string;
|
|
49
|
+
rows?: readonly string[];
|
|
50
|
+
cols?: readonly string[];
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Moves each range end that a delete removes to the nearest row or column that stays in the
|
|
54
|
+
* range, as Excel does. Call it with the context from before the delete. A single-cell ref
|
|
55
|
+
* and a range that loses all its rows stay as they are, and print as `#REF!` after the delete.
|
|
56
|
+
*/
|
|
57
|
+
export declare function shrinkDeleted(stored: string, sheetId: string, ctx: RefContext, deletion: Deletion): string;
|
package/dist/scan.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { A1Ref } from './a1';
|
|
2
|
+
export type Token<T> = {
|
|
3
|
+
kind: 'text';
|
|
4
|
+
text: string;
|
|
5
|
+
} | {
|
|
6
|
+
kind: 'ref';
|
|
7
|
+
text: string;
|
|
8
|
+
ref: T;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Splits a formula into text and ref tokens. The tokens join back to the input. Refs are found
|
|
12
|
+
* only at a token start and never in a string literal. `read` turns a match of `pattern` into a
|
|
13
|
+
* ref, or gives undefined when the match is not a ref (for example a column after XFD).
|
|
14
|
+
*/
|
|
15
|
+
export declare function scanWith<T>(formula: string, pattern: RegExp, read: (m: RegExpExecArray) => T | undefined): Token<T>[];
|
|
16
|
+
/** Splits an A1 formula into text and ref tokens (for the editor and for conversion). */
|
|
17
|
+
export declare function scanFormula(formula: string): Token<A1Ref>[];
|
|
18
|
+
/**
|
|
19
|
+
* The names (named ranges, `LET` and `LAMBDA` names) in an A1 formula, for the editor. A name
|
|
20
|
+
* is an identifier that is not a ref, not a function call and not `TRUE` or `FALSE`. Each name
|
|
21
|
+
* shows once, in its first spelling (names do not care about case).
|
|
22
|
+
*/
|
|
23
|
+
export declare function formulaNames(formula: string): string[];
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/** A computed or raw cell value. An error is its own variant, for example `{ error: '#DIV/0!' }`. */
|
|
2
|
+
export type CellValue = number | string | boolean | null | {
|
|
3
|
+
error: string;
|
|
4
|
+
};
|
|
5
|
+
/** A cell position by IDs. */
|
|
6
|
+
export interface CellRef {
|
|
7
|
+
sheet: string;
|
|
8
|
+
row: string;
|
|
9
|
+
col: string;
|
|
10
|
+
}
|
|
11
|
+
/** A rectangle by the IDs of its top-left and bottom-right cells. */
|
|
12
|
+
export interface RangeRef {
|
|
13
|
+
sheet: string;
|
|
14
|
+
start: {
|
|
15
|
+
row: string;
|
|
16
|
+
col: string;
|
|
17
|
+
};
|
|
18
|
+
end: {
|
|
19
|
+
row: string;
|
|
20
|
+
col: string;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export interface CellStyle {
|
|
24
|
+
bold?: boolean;
|
|
25
|
+
italic?: boolean;
|
|
26
|
+
underline?: boolean;
|
|
27
|
+
strike?: boolean;
|
|
28
|
+
color?: string;
|
|
29
|
+
fill?: string;
|
|
30
|
+
align?: 'left' | 'center' | 'right';
|
|
31
|
+
valign?: 'top' | 'middle' | 'bottom';
|
|
32
|
+
wrap?: boolean;
|
|
33
|
+
border?: {
|
|
34
|
+
top?: string;
|
|
35
|
+
right?: string;
|
|
36
|
+
bottom?: string;
|
|
37
|
+
left?: string;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export type SheetOp =
|
|
41
|
+
/** `formula` is in the stored ID form and starts with `=`. Without both, the cell is cleared. */
|
|
42
|
+
{
|
|
43
|
+
type: 'setCell';
|
|
44
|
+
sheet: string;
|
|
45
|
+
row: string;
|
|
46
|
+
col: string;
|
|
47
|
+
value?: CellValue;
|
|
48
|
+
formula?: string;
|
|
49
|
+
}
|
|
50
|
+
/** Inserts new row IDs before the row `before`, or at the end when `before` is null. */
|
|
51
|
+
| {
|
|
52
|
+
type: 'insertRows';
|
|
53
|
+
sheet: string;
|
|
54
|
+
ids: string[];
|
|
55
|
+
before: string | null;
|
|
56
|
+
} | {
|
|
57
|
+
type: 'deleteRows';
|
|
58
|
+
sheet: string;
|
|
59
|
+
ids: string[];
|
|
60
|
+
} | {
|
|
61
|
+
type: 'insertCols';
|
|
62
|
+
sheet: string;
|
|
63
|
+
ids: string[];
|
|
64
|
+
before: string | null;
|
|
65
|
+
} | {
|
|
66
|
+
type: 'deleteCols';
|
|
67
|
+
sheet: string;
|
|
68
|
+
ids: string[];
|
|
69
|
+
}
|
|
70
|
+
/** An Excel number format code, or null for General. */
|
|
71
|
+
| {
|
|
72
|
+
type: 'setFormat';
|
|
73
|
+
range: RangeRef;
|
|
74
|
+
format: string | null;
|
|
75
|
+
} | {
|
|
76
|
+
type: 'setStyle';
|
|
77
|
+
range: RangeRef;
|
|
78
|
+
style: CellStyle | null;
|
|
79
|
+
} | {
|
|
80
|
+
type: 'merge';
|
|
81
|
+
range: RangeRef;
|
|
82
|
+
} | {
|
|
83
|
+
type: 'unmerge';
|
|
84
|
+
range: RangeRef;
|
|
85
|
+
}
|
|
86
|
+
/** A named range. `ref` is a stored formula without `=`; null removes the name. */
|
|
87
|
+
| {
|
|
88
|
+
type: 'setName';
|
|
89
|
+
name: string;
|
|
90
|
+
ref: string | null;
|
|
91
|
+
} | {
|
|
92
|
+
type: 'addSheet';
|
|
93
|
+
id: string;
|
|
94
|
+
name: string;
|
|
95
|
+
rows: string[];
|
|
96
|
+
cols: string[];
|
|
97
|
+
} | {
|
|
98
|
+
type: 'removeSheet';
|
|
99
|
+
id: string;
|
|
100
|
+
} | {
|
|
101
|
+
type: 'renameSheet';
|
|
102
|
+
id: string;
|
|
103
|
+
name: string;
|
|
104
|
+
};
|
|
105
|
+
export interface CellData {
|
|
106
|
+
row: string;
|
|
107
|
+
col: string;
|
|
108
|
+
value?: CellValue;
|
|
109
|
+
/** The stored ID form, with `=`. */
|
|
110
|
+
formula?: string;
|
|
111
|
+
}
|
|
112
|
+
export interface SheetData {
|
|
113
|
+
id: string;
|
|
114
|
+
name: string;
|
|
115
|
+
rows: string[];
|
|
116
|
+
cols: string[];
|
|
117
|
+
cells: CellData[];
|
|
118
|
+
}
|
|
119
|
+
export interface SheetDoc {
|
|
120
|
+
sheets: SheetData[];
|
|
121
|
+
names?: {
|
|
122
|
+
name: string;
|
|
123
|
+
ref: string;
|
|
124
|
+
}[];
|
|
125
|
+
}
|
|
126
|
+
/** One computed value that changed. */
|
|
127
|
+
export interface CellChange extends CellRef {
|
|
128
|
+
value: CellValue;
|
|
129
|
+
}
|
|
130
|
+
export interface FunctionInfo {
|
|
131
|
+
name: string;
|
|
132
|
+
/** The help form, for example `ROUND(number, num_digits)`. `[x]` is optional. */
|
|
133
|
+
signature?: string;
|
|
134
|
+
}
|
|
135
|
+
/** The cells an engine reads again after each recalc, when it cannot list the changes itself. */
|
|
136
|
+
export interface WatchRange {
|
|
137
|
+
sheet: string;
|
|
138
|
+
rows: string[];
|
|
139
|
+
cols: string[];
|
|
140
|
+
}
|
|
141
|
+
export interface FormulaEngine {
|
|
142
|
+
load(doc: SheetDoc): Promise<void>;
|
|
143
|
+
/** Applies the ops and gives the computed values that changed. */
|
|
144
|
+
apply(ops: SheetOp[]): Promise<CellChange[]>;
|
|
145
|
+
/** Evaluates a stored formula as if it is on `sheet`, without a change to the document. */
|
|
146
|
+
evaluate(sheet: string, formula: string): Promise<CellValue>;
|
|
147
|
+
/** The function names, for the formula bar help. */
|
|
148
|
+
functions(): FunctionInfo[];
|
|
149
|
+
/**
|
|
150
|
+
* Limits the change diff to these ranges (for example, the viewport), or null for all cells
|
|
151
|
+
* with content. Gives the current values of the watched cells. Optional: an engine that
|
|
152
|
+
* lists its own changes does not need it.
|
|
153
|
+
*/
|
|
154
|
+
watch?(ranges: WatchRange[] | null): Promise<CellChange[]>;
|
|
155
|
+
dispose?(): void;
|
|
156
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ember-lattice/formula",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "The Lattice FormulaEngine interface, A1 and R1C1 parsing for the editor, and ID-based reference storage. No framework.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"data-grid",
|
|
7
|
+
"server-side-row-model",
|
|
8
|
+
"protocol"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"author": "Joe Gaudet <joe@joegaudet.com>",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/joegaudet/ember-lattice.git",
|
|
15
|
+
"directory": "packages/formula"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://joegaudet.com/ember-lattice/",
|
|
18
|
+
"bugs": "https://github.com/joegaudet/ember-lattice/issues",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"default": "./dist/index.js"
|
|
25
|
+
},
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"CHANGELOG.md",
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE.md",
|
|
33
|
+
"NOTICE"
|
|
34
|
+
],
|
|
35
|
+
"sideEffects": false,
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"tsup": "^8.5.1"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=22.12.0"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@sindresorhus/is": "^7.2.0"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "tsc --build --force && tsup"
|
|
50
|
+
}
|
|
6
51
|
}
|