@molecule/api-formula-engine 1.0.2 → 1.0.3
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 +111 -24
- package/README.md +514 -262
- package/package.json +3 -3
package/LICENSE
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
|
|
1
2
|
Apache License
|
|
2
3
|
Version 2.0, January 2004
|
|
3
4
|
http://www.apache.org/licenses/
|
|
@@ -34,21 +35,33 @@
|
|
|
34
35
|
|
|
35
36
|
"Work" shall mean the work of authorship, whether in Source or
|
|
36
37
|
Object form, made available under the License, as indicated by a
|
|
37
|
-
copyright notice that is included in or attached to the work
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
38
40
|
|
|
39
41
|
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
42
|
form, that is based on (or derived from) the Work and for which the
|
|
41
43
|
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
-
represent, as a whole, an original work of authorship.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
49
62
|
|
|
50
63
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
-
on behalf of whom a Contribution has been received by
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
52
65
|
subsequently incorporated within the Work.
|
|
53
66
|
|
|
54
67
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
@@ -61,8 +74,18 @@
|
|
|
61
74
|
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
75
|
this License, each Contributor hereby grants to You a perpetual,
|
|
63
76
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
-
|
|
65
|
-
and otherwise transfer the Work
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
66
89
|
|
|
67
90
|
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
91
|
Work or Derivative Works thereof in any medium, with or without
|
|
@@ -84,23 +107,87 @@
|
|
|
84
107
|
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
108
|
distribution, then any Derivative Works that You distribute must
|
|
86
109
|
include a readable copy of the attribution notices contained
|
|
87
|
-
within such NOTICE file
|
|
88
|
-
|
|
89
|
-
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
90
138
|
|
|
91
139
|
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
-
names, trademarks, service marks, or product names of the Licensor
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
93
143
|
|
|
94
144
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
-
agreed to in writing, Licensor provides the Work
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
104
191
|
|
|
105
192
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
193
|
you may not use this file except in compliance with the License.
|
package/README.md
CHANGED
|
@@ -3,15 +3,11 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
|
3
3
|
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
4
|
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
5
|
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
-
Generated: 2026-
|
|
6
|
+
Generated: 2026-10-16T23:29:41.143Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/api-formula-engine
|
|
10
10
|
|
|
11
|
-
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
12
|
-
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
13
|
-
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
14
|
-
|
|
15
11
|
Spreadsheet-style formula engine for molecule.dev.
|
|
16
12
|
|
|
17
13
|
Pure-function parser + evaluator + dependency tracker for
|
|
@@ -45,6 +41,10 @@ stateless one-shot math.
|
|
|
45
41
|
|
|
46
42
|
Pure functions, zero I/O, zero DB access, zero hardcoded UI text.
|
|
47
43
|
|
|
44
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
45
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
46
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
47
|
+
|
|
48
48
|
## Quick Start
|
|
49
49
|
|
|
50
50
|
```typescript
|
|
@@ -79,27 +79,30 @@ npm install @molecule/api-formula-engine
|
|
|
79
79
|
|
|
80
80
|
### Interfaces
|
|
81
81
|
|
|
82
|
-
#### `
|
|
82
|
+
#### `Token`
|
|
83
83
|
|
|
84
|
-
|
|
84
|
+
Lexical token. `value` is the raw source text; `op` carries the
|
|
85
|
+
normalized operator string for `kind: 'op'` tokens.
|
|
85
86
|
|
|
86
87
|
```typescript
|
|
87
|
-
interface
|
|
88
|
-
readonly kind:
|
|
89
|
-
readonly
|
|
90
|
-
readonly
|
|
91
|
-
readonly
|
|
88
|
+
interface Token {
|
|
89
|
+
readonly kind: TokenKind
|
|
90
|
+
readonly value: string
|
|
91
|
+
readonly position: number
|
|
92
|
+
readonly errorCode?: FormulaErrorCode
|
|
92
93
|
}
|
|
93
94
|
```
|
|
94
95
|
|
|
95
|
-
#### `
|
|
96
|
+
#### `FormulaError`
|
|
96
97
|
|
|
97
|
-
|
|
98
|
+
Spreadsheet error value. Returned (never thrown) by `evaluate`/`getValue`
|
|
99
|
+
to allow downstream cells to propagate it like Excel.
|
|
98
100
|
|
|
99
101
|
```typescript
|
|
100
|
-
interface
|
|
101
|
-
readonly
|
|
102
|
-
readonly
|
|
102
|
+
interface FormulaError {
|
|
103
|
+
readonly type: 'error'
|
|
104
|
+
readonly code: FormulaErrorCode
|
|
105
|
+
readonly message?: string
|
|
103
106
|
}
|
|
104
107
|
```
|
|
105
108
|
|
|
@@ -126,6 +129,39 @@ interface CellRange {
|
|
|
126
129
|
}
|
|
127
130
|
```
|
|
128
131
|
|
|
132
|
+
#### `NumberLiteralNode`
|
|
133
|
+
|
|
134
|
+
AST: numeric literal.
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
interface NumberLiteralNode {
|
|
138
|
+
readonly kind: 'number'
|
|
139
|
+
readonly value: number
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
#### `StringLiteralNode`
|
|
144
|
+
|
|
145
|
+
AST: string literal (double-quoted).
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
interface StringLiteralNode {
|
|
149
|
+
readonly kind: 'string'
|
|
150
|
+
readonly value: string
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
#### `BooleanLiteralNode`
|
|
155
|
+
|
|
156
|
+
AST: boolean literal (TRUE / FALSE).
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
interface BooleanLiteralNode {
|
|
160
|
+
readonly kind: 'boolean'
|
|
161
|
+
readonly value: boolean
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
129
165
|
#### `ErrorLiteralNode`
|
|
130
166
|
|
|
131
167
|
AST: error literal (`#DIV/0!`, etc.).
|
|
@@ -137,27 +173,53 @@ interface ErrorLiteralNode {
|
|
|
137
173
|
}
|
|
138
174
|
```
|
|
139
175
|
|
|
140
|
-
#### `
|
|
176
|
+
#### `ReferenceNode`
|
|
141
177
|
|
|
142
|
-
|
|
178
|
+
AST: cell reference (e.g. `A1`, `$A$1`).
|
|
143
179
|
|
|
144
180
|
```typescript
|
|
145
|
-
interface
|
|
146
|
-
|
|
147
|
-
readonly
|
|
181
|
+
interface ReferenceNode {
|
|
182
|
+
readonly kind: 'reference'
|
|
183
|
+
readonly coord: CellCoord
|
|
184
|
+
/** Original textual representation, preserved for diagnostics. */
|
|
185
|
+
readonly text: string
|
|
148
186
|
}
|
|
149
187
|
```
|
|
150
188
|
|
|
151
|
-
#### `
|
|
189
|
+
#### `RangeNode`
|
|
152
190
|
|
|
153
|
-
|
|
154
|
-
to allow downstream cells to propagate it like Excel.
|
|
191
|
+
AST: cell range (e.g. `A1:B5`).
|
|
155
192
|
|
|
156
193
|
```typescript
|
|
157
|
-
interface
|
|
158
|
-
readonly
|
|
159
|
-
readonly
|
|
160
|
-
readonly
|
|
194
|
+
interface RangeNode {
|
|
195
|
+
readonly kind: 'range'
|
|
196
|
+
readonly range: CellRange
|
|
197
|
+
readonly text: string
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
#### `UnaryOpNode`
|
|
202
|
+
|
|
203
|
+
AST: unary operation (e.g. `-A1`, `A1%`).
|
|
204
|
+
|
|
205
|
+
```typescript
|
|
206
|
+
interface UnaryOpNode {
|
|
207
|
+
readonly kind: 'unary'
|
|
208
|
+
readonly op: UnaryOperator
|
|
209
|
+
readonly operand: AstNode
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
#### `BinaryOpNode`
|
|
214
|
+
|
|
215
|
+
AST: binary operation.
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
interface BinaryOpNode {
|
|
219
|
+
readonly kind: 'binary'
|
|
220
|
+
readonly op: BinaryOperator
|
|
221
|
+
readonly left: AstNode
|
|
222
|
+
readonly right: AstNode
|
|
161
223
|
}
|
|
162
224
|
```
|
|
163
225
|
|
|
@@ -186,39 +248,14 @@ interface FunctionContext {
|
|
|
186
248
|
}
|
|
187
249
|
```
|
|
188
250
|
|
|
189
|
-
#### `
|
|
190
|
-
|
|
191
|
-
AST: numeric literal.
|
|
192
|
-
|
|
193
|
-
```typescript
|
|
194
|
-
interface NumberLiteralNode {
|
|
195
|
-
readonly kind: 'number'
|
|
196
|
-
readonly value: number
|
|
197
|
-
}
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
#### `RangeNode`
|
|
201
|
-
|
|
202
|
-
AST: cell range (e.g. `A1:B5`).
|
|
203
|
-
|
|
204
|
-
```typescript
|
|
205
|
-
interface RangeNode {
|
|
206
|
-
readonly kind: 'range'
|
|
207
|
-
readonly range: CellRange
|
|
208
|
-
readonly text: string
|
|
209
|
-
}
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
#### `ReferenceNode`
|
|
251
|
+
#### `EvaluateOptions`
|
|
213
252
|
|
|
214
|
-
|
|
253
|
+
Options shared by `evaluate` and the `Sheet.recompute` family.
|
|
215
254
|
|
|
216
255
|
```typescript
|
|
217
|
-
interface
|
|
218
|
-
|
|
219
|
-
readonly
|
|
220
|
-
/** Original textual representation, preserved for diagnostics. */
|
|
221
|
-
readonly text: string
|
|
256
|
+
interface EvaluateOptions {
|
|
257
|
+
/** Override the clock used by `TODAY()` / `NOW()`. Defaults to `Date.now()`. */
|
|
258
|
+
readonly now?: () => Date
|
|
222
259
|
}
|
|
223
260
|
```
|
|
224
261
|
|
|
@@ -237,44 +274,57 @@ interface SheetConfig {
|
|
|
237
274
|
}
|
|
238
275
|
```
|
|
239
276
|
|
|
240
|
-
|
|
277
|
+
### Types
|
|
241
278
|
|
|
242
|
-
|
|
279
|
+
#### `ResolveCell`
|
|
280
|
+
|
|
281
|
+
Function used to resolve a cell coordinate to its value. Returns
|
|
282
|
+
`null` for empty cells. Implementations may evaluate dependent
|
|
283
|
+
formulas (in which case they should detect cycles and return a
|
|
284
|
+
`#CIRC!` error to the caller).
|
|
243
285
|
|
|
244
286
|
```typescript
|
|
245
|
-
|
|
246
|
-
readonly kind: 'string'
|
|
247
|
-
readonly value: string
|
|
248
|
-
}
|
|
287
|
+
type ResolveCell = (coord: CellCoord) => CellValue
|
|
249
288
|
```
|
|
250
289
|
|
|
251
|
-
#### `
|
|
290
|
+
#### `TokenKind`
|
|
252
291
|
|
|
253
|
-
Lexical token.
|
|
254
|
-
normalized operator string for `kind: 'op'` tokens.
|
|
292
|
+
Lexical token kinds.
|
|
255
293
|
|
|
256
294
|
```typescript
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
295
|
+
type TokenKind =
|
|
296
|
+
| 'number'
|
|
297
|
+
| 'string'
|
|
298
|
+
| 'boolean'
|
|
299
|
+
| 'error'
|
|
300
|
+
| 'identifier'
|
|
301
|
+
| 'reference'
|
|
302
|
+
| 'range'
|
|
303
|
+
| 'op'
|
|
304
|
+
| 'lparen'
|
|
305
|
+
| 'rparen'
|
|
306
|
+
| 'comma'
|
|
263
307
|
```
|
|
264
308
|
|
|
265
|
-
#### `
|
|
309
|
+
#### `FormulaErrorCode`
|
|
266
310
|
|
|
267
|
-
|
|
311
|
+
Spreadsheet error codes. Errors propagate through expressions: any
|
|
312
|
+
arithmetic or function call involving a `FormulaError` returns the
|
|
313
|
+
left-most error unchanged (Excel-compatible semantics).
|
|
268
314
|
|
|
269
315
|
```typescript
|
|
270
|
-
|
|
271
|
-
readonly kind: 'unary'
|
|
272
|
-
readonly op: UnaryOperator
|
|
273
|
-
readonly operand: AstNode
|
|
274
|
-
}
|
|
316
|
+
type FormulaErrorCode = '#DIV/0!' | '#VALUE!' | '#REF!' | '#NAME?' | '#NUM!' | '#N/A' | '#CIRC!'
|
|
275
317
|
```
|
|
276
318
|
|
|
277
|
-
|
|
319
|
+
#### `CellValue`
|
|
320
|
+
|
|
321
|
+
Possible cell values. `null` represents an empty cell; numbers, strings,
|
|
322
|
+
booleans, and `Date`s are all valid scalar values. `FormulaError`
|
|
323
|
+
represents a propagating error.
|
|
324
|
+
|
|
325
|
+
```typescript
|
|
326
|
+
type CellValue = number | string | boolean | Date | null | FormulaError
|
|
327
|
+
```
|
|
278
328
|
|
|
279
329
|
#### `AstNode`
|
|
280
330
|
|
|
@@ -293,33 +343,21 @@ type AstNode =
|
|
|
293
343
|
| FunctionCallNode
|
|
294
344
|
```
|
|
295
345
|
|
|
296
|
-
#### `
|
|
297
|
-
|
|
298
|
-
Binary operators. `^` = power, `&` = string concat,
|
|
299
|
-
`=`/`<>` = equality, `<`/`<=`/`>`/`>=` = ordering.
|
|
300
|
-
|
|
301
|
-
```typescript
|
|
302
|
-
type BinaryOperator = '+' | '-' | '*' | '/' | '^' | '&' | '=' | '<>' | '<' | '<=' | '>' | '>='
|
|
303
|
-
```
|
|
304
|
-
|
|
305
|
-
#### `CellValue`
|
|
346
|
+
#### `UnaryOperator`
|
|
306
347
|
|
|
307
|
-
|
|
308
|
-
booleans, and `Date`s are all valid scalar values. `FormulaError`
|
|
309
|
-
represents a propagating error.
|
|
348
|
+
Unary operators supported by the parser.
|
|
310
349
|
|
|
311
350
|
```typescript
|
|
312
|
-
type
|
|
351
|
+
type UnaryOperator = '+' | '-' | '%'
|
|
313
352
|
```
|
|
314
353
|
|
|
315
|
-
#### `
|
|
354
|
+
#### `BinaryOperator`
|
|
316
355
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
left-most error unchanged (Excel-compatible semantics).
|
|
356
|
+
Binary operators. `^` = power, `&` = string concat,
|
|
357
|
+
`=`/`<>` = equality, `<`/`<=`/`>`/`>=` = ordering.
|
|
320
358
|
|
|
321
359
|
```typescript
|
|
322
|
-
type
|
|
360
|
+
type BinaryOperator = '+' | '-' | '*' | '/' | '^' | '&' | '=' | '<>' | '<' | '<=' | '>' | '>='
|
|
323
361
|
```
|
|
324
362
|
|
|
325
363
|
#### `FormulaFunction`
|
|
@@ -336,77 +374,327 @@ type FormulaFunction = (
|
|
|
336
374
|
) => CellValue
|
|
337
375
|
```
|
|
338
376
|
|
|
339
|
-
|
|
377
|
+
### Classes
|
|
340
378
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
379
|
+
#### `Sheet`
|
|
380
|
+
|
|
381
|
+
In-memory single-sheet engine. References use A1 notation (`A1`,
|
|
382
|
+
`$A$1`, `BC42`).
|
|
345
383
|
|
|
346
384
|
```typescript
|
|
347
|
-
|
|
385
|
+
class Sheet {
|
|
386
|
+
private readonly cells = new Map<string, CellRecord>()
|
|
387
|
+
/** dependent-of[key] = set of cells that reference `key`. */
|
|
388
|
+
private readonly dependents = new Map<string, Set<string>>()
|
|
389
|
+
private readonly config: Required<SheetConfig>
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Create a new Sheet with optional dimension limits.
|
|
393
|
+
*
|
|
394
|
+
* @param config - Sheet configuration.
|
|
395
|
+
*/
|
|
396
|
+
constructor(config: SheetConfig = {}) {
|
|
397
|
+
this.config = { ...DEFAULT_CONFIG, ...config }
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Set a literal value at a cell.
|
|
402
|
+
*
|
|
403
|
+
* @param ref - A1-style cell reference (e.g. `A1`, `$B$3`).
|
|
404
|
+
* @param value - Literal value (number, string, boolean, Date, or null).
|
|
405
|
+
* @param options - Evaluation options forwarded to recompute.
|
|
406
|
+
*/
|
|
407
|
+
setValue(ref: string, value: CellValue, options: EvaluateOptions = {}): void {
|
|
408
|
+
const coord = this.refToCoord(ref)
|
|
409
|
+
this.assignCell(coord, value, null, null, options)
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Set a formula at a cell. The formula text may include or omit the
|
|
414
|
+
* leading `=`. The formula is parsed eagerly and `#NAME?` /
|
|
415
|
+
* `#VALUE!` errors caused by parser failures are stored as the
|
|
416
|
+
* cell's value.
|
|
417
|
+
*
|
|
418
|
+
* @param ref - A1-style cell reference.
|
|
419
|
+
* @param formula - Formula text (e.g. `=SUM(A1:A10)` or `SUM(A1:A10)`).
|
|
420
|
+
* @param options - Evaluation options forwarded to recompute.
|
|
421
|
+
*/
|
|
422
|
+
setFormula(ref: string, formula: string, options: EvaluateOptions = {}): void {
|
|
423
|
+
const coord = this.refToCoord(ref)
|
|
424
|
+
let ast: AstNode | null = null
|
|
425
|
+
let parseError: CellValue | null = null
|
|
426
|
+
try {
|
|
427
|
+
ast = parseFormula(formula)
|
|
428
|
+
} catch (e) {
|
|
429
|
+
parseError = makeError('#VALUE!', (e as Error).message)
|
|
430
|
+
}
|
|
431
|
+
if (parseError) {
|
|
432
|
+
this.assignCell(coord, parseError, null, formula, options)
|
|
433
|
+
return
|
|
434
|
+
}
|
|
435
|
+
this.assignCell(coord, null, ast, formula, options)
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Clear a cell (remove value, formula, and dependency edges).
|
|
440
|
+
*
|
|
441
|
+
* @param ref - A1-style cell reference.
|
|
442
|
+
* @param options - Evaluation options forwarded to recompute.
|
|
443
|
+
*/
|
|
444
|
+
clear(ref: string, options: EvaluateOptions = {}): void {
|
|
445
|
+
const coord = this.refToCoord(ref)
|
|
446
|
+
const key = coordKey(coord)
|
|
447
|
+
const existing = this.cells.get(key)
|
|
448
|
+
if (existing) {
|
|
449
|
+
for (const dep of existing.precedents) {
|
|
450
|
+
this.dependents.get(dep)?.delete(key)
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
this.cells.delete(key)
|
|
454
|
+
this.recomputeDownstream(key, options)
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Read the most recently computed value for a cell.
|
|
459
|
+
*
|
|
460
|
+
* @param ref - A1-style cell reference.
|
|
461
|
+
* @returns Cell value (`null` if the cell is empty).
|
|
462
|
+
*/
|
|
463
|
+
getValue(ref: string): CellValue {
|
|
464
|
+
const coord = this.refToCoord(ref)
|
|
465
|
+
const key = coordKey(coord)
|
|
466
|
+
const cell = this.cells.get(key)
|
|
467
|
+
return cell ? cell.computed : null
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Return the formula text for a cell, or `null` if the cell holds a
|
|
472
|
+
* literal value or is empty.
|
|
473
|
+
*
|
|
474
|
+
* @param ref - A1-style cell reference.
|
|
475
|
+
* @returns Formula text (with leading `=`) or `null`.
|
|
476
|
+
*/
|
|
477
|
+
getFormula(ref: string): string | null {
|
|
478
|
+
const coord = this.refToCoord(ref)
|
|
479
|
+
const key = coordKey(coord)
|
|
480
|
+
return this.cells.get(key)?.formula ?? null
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Recompute every formula cell. Useful after bulk edits or when
|
|
485
|
+
* `now()` semantics change.
|
|
486
|
+
*
|
|
487
|
+
* @param options - Evaluation options.
|
|
488
|
+
*/
|
|
489
|
+
recomputeAll(options: EvaluateOptions = {}): void {
|
|
490
|
+
const keys = [...this.cells.keys()]
|
|
491
|
+
const { order, cycle } = topologicalSort(this.dependents, keys)
|
|
492
|
+
for (const k of order) this.evaluateKey(k, options, cycle)
|
|
493
|
+
for (const k of cycle) {
|
|
494
|
+
const cell = this.cells.get(k)
|
|
495
|
+
if (!cell) continue
|
|
496
|
+
cell.computed = makeError('#CIRC!')
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Evaluate a free-standing formula string against this sheet's data.
|
|
502
|
+
* Does NOT mutate the sheet. Useful for ad-hoc evaluations such as
|
|
503
|
+
* invoice line-item totals or computed-column previews.
|
|
504
|
+
*
|
|
505
|
+
* @param formula - Formula text.
|
|
506
|
+
* @param options - Evaluation options.
|
|
507
|
+
* @returns Evaluated cell value.
|
|
508
|
+
*/
|
|
509
|
+
evaluateFormula(formula: string, options: EvaluateOptions = {}): CellValue {
|
|
510
|
+
let ast: AstNode
|
|
511
|
+
try {
|
|
512
|
+
ast = parseFormula(formula)
|
|
513
|
+
} catch (e) {
|
|
514
|
+
return makeError('#VALUE!', (e as Error).message)
|
|
515
|
+
}
|
|
516
|
+
return evaluate(ast, (coord) => this.cells.get(coordKey(coord))?.computed ?? null, options)
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// --- Internals ---
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* Parse an A1-style reference string into a validated CellCoord.
|
|
523
|
+
*/
|
|
524
|
+
private refToCoord(ref: string): CellCoord {
|
|
525
|
+
const coord = parseCellReference(ref)
|
|
526
|
+
if (!coord) throw new Error(`Invalid cell reference: ${ref}`)
|
|
527
|
+
if (coord.col >= this.config.numCols || coord.row >= this.config.numRows) {
|
|
528
|
+
throw new Error(`Reference out of bounds: ${ref}`)
|
|
529
|
+
}
|
|
530
|
+
return coord
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* Write a cell record, rewire dependency edges, and trigger downstream recompute.
|
|
535
|
+
*/
|
|
536
|
+
private assignCell(
|
|
537
|
+
coord: CellCoord,
|
|
538
|
+
value: CellValue,
|
|
539
|
+
ast: AstNode | null,
|
|
540
|
+
formula: string | null,
|
|
541
|
+
options: EvaluateOptions,
|
|
542
|
+
): void {
|
|
543
|
+
const key = coordKey(coord)
|
|
544
|
+
const existing = this.cells.get(key)
|
|
545
|
+
if (existing) {
|
|
546
|
+
for (const dep of existing.precedents) {
|
|
547
|
+
this.dependents.get(dep)?.delete(key)
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
const precedents = new Set<string>()
|
|
551
|
+
if (ast) {
|
|
552
|
+
for (const c of collectReferences(ast)) {
|
|
553
|
+
const k = coordKey(c)
|
|
554
|
+
precedents.add(k)
|
|
555
|
+
let set = this.dependents.get(k)
|
|
556
|
+
if (!set) {
|
|
557
|
+
set = new Set()
|
|
558
|
+
this.dependents.set(k, set)
|
|
559
|
+
}
|
|
560
|
+
set.add(key)
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
const record: CellRecord = {
|
|
564
|
+
value,
|
|
565
|
+
ast,
|
|
566
|
+
formula,
|
|
567
|
+
precedents,
|
|
568
|
+
computed: value,
|
|
569
|
+
}
|
|
570
|
+
this.cells.set(key, record)
|
|
571
|
+
if (ast) this.evaluateKey(key, options, new Set())
|
|
572
|
+
this.recomputeDownstream(key, options)
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* Recompute all formula cells that transitively depend on the given key.
|
|
577
|
+
*/
|
|
578
|
+
private recomputeDownstream(rootKey: string, options: EvaluateOptions): void {
|
|
579
|
+
// Find every transitive dependent of the changed cell.
|
|
580
|
+
const affected = new Set<string>()
|
|
581
|
+
const stack = [rootKey]
|
|
582
|
+
while (stack.length > 0) {
|
|
583
|
+
const k = stack.pop()!
|
|
584
|
+
const outs = this.dependents.get(k)
|
|
585
|
+
if (!outs) continue
|
|
586
|
+
for (const t of outs) {
|
|
587
|
+
if (!affected.has(t)) {
|
|
588
|
+
affected.add(t)
|
|
589
|
+
stack.push(t)
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
if (affected.size === 0) return
|
|
594
|
+
const keys = [...affected]
|
|
595
|
+
const { order, cycle } = topologicalSort(this.dependents, keys)
|
|
596
|
+
for (const k of order) this.evaluateKey(k, options, cycle)
|
|
597
|
+
for (const k of cycle) {
|
|
598
|
+
const cell = this.cells.get(k)
|
|
599
|
+
if (!cell) continue
|
|
600
|
+
cell.computed = makeError('#CIRC!')
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* Evaluate (or propagate) the computed value for a single cell key.
|
|
606
|
+
*/
|
|
607
|
+
private evaluateKey(key: string, options: EvaluateOptions, cycleSet: ReadonlySet<string>): void {
|
|
608
|
+
const cell = this.cells.get(key)
|
|
609
|
+
if (!cell) return
|
|
610
|
+
if (cycleSet.has(key)) {
|
|
611
|
+
cell.computed = makeError('#CIRC!')
|
|
612
|
+
return
|
|
613
|
+
}
|
|
614
|
+
if (!cell.ast) {
|
|
615
|
+
cell.computed = cell.value
|
|
616
|
+
return
|
|
617
|
+
}
|
|
618
|
+
const value = evaluate(
|
|
619
|
+
cell.ast,
|
|
620
|
+
(coord) => {
|
|
621
|
+
const k = coordKey(coord)
|
|
622
|
+
if (cycleSet.has(k)) return makeError('#CIRC!')
|
|
623
|
+
const target = this.cells.get(k)
|
|
624
|
+
return target ? target.computed : null
|
|
625
|
+
},
|
|
626
|
+
options,
|
|
627
|
+
)
|
|
628
|
+
cell.computed = value
|
|
629
|
+
if (isError(value)) {
|
|
630
|
+
cell.value = value
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
348
634
|
```
|
|
349
635
|
|
|
350
|
-
|
|
636
|
+
### Functions
|
|
351
637
|
|
|
352
|
-
|
|
638
|
+
#### `collectReferences(node)`
|
|
353
639
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
| 'error'
|
|
360
|
-
| 'identifier'
|
|
361
|
-
| 'reference'
|
|
362
|
-
| 'range'
|
|
363
|
-
| 'op'
|
|
364
|
-
| 'lparen'
|
|
365
|
-
| 'rparen'
|
|
366
|
-
| 'comma'
|
|
640
|
+
Walk an AST and collect every coordinate it references (single
|
|
641
|
+
references and every cell inside ranges).
|
|
642
|
+
|
|
643
|
+
```typescript
|
|
644
|
+
function collectReferences(node: AstNode): CellCoord[]
|
|
367
645
|
```
|
|
368
646
|
|
|
369
|
-
|
|
647
|
+
- `node` — Root AST node.
|
|
370
648
|
|
|
371
|
-
|
|
649
|
+
**Returns:** Flat list of referenced coordinates (may contain duplicates).
|
|
372
650
|
|
|
373
|
-
|
|
374
|
-
type UnaryOperator = '+' | '-' | '%'
|
|
375
|
-
```
|
|
651
|
+
#### `topologicalSort(dependents, keys)`
|
|
376
652
|
|
|
377
|
-
|
|
653
|
+
Topologically sort a set of coordinate keys by their dependency graph
|
|
654
|
+
such that, for any edge `a → b` (b depends on a), `a` precedes `b`
|
|
655
|
+
in the result.
|
|
378
656
|
|
|
379
|
-
|
|
657
|
+
Uses Kahn's algorithm. If a cycle is detected, the cycle members are
|
|
658
|
+
returned in `cycle` and excluded from `order` — callers typically
|
|
659
|
+
mark cycle members with a `#CIRC!` error.
|
|
380
660
|
|
|
381
|
-
|
|
382
|
-
|
|
661
|
+
```typescript
|
|
662
|
+
function topologicalSort(
|
|
663
|
+
dependents: ReadonlyMap<string, ReadonlySet<string>>,
|
|
664
|
+
keys: readonly string[],
|
|
665
|
+
): { order: string[]; cycle: Set<string> }
|
|
666
|
+
```
|
|
383
667
|
|
|
384
|
-
|
|
668
|
+
- `dependents` — Map from a key to the set of keys that depend on it. (i.e. forward edges in the recompute order.)
|
|
669
|
+
- `keys` — Keys to sort.
|
|
385
670
|
|
|
386
|
-
|
|
671
|
+
**Returns:** `{ order, cycle }` — `order` is the topological order; `cycle` is the set of keys participating in (or downstream of) a cycle.
|
|
387
672
|
|
|
388
|
-
|
|
389
|
-
|
|
673
|
+
#### `evaluate(node, resolveCell, options)`
|
|
674
|
+
|
|
675
|
+
Evaluate an AST in the context of a cell-resolution function.
|
|
390
676
|
|
|
391
677
|
```typescript
|
|
392
|
-
function
|
|
678
|
+
function evaluate(node: AstNode, resolveCell: ResolveCell, options?: EvaluateOptions): CellValue
|
|
393
679
|
```
|
|
394
680
|
|
|
395
681
|
- `node` — Root AST node.
|
|
682
|
+
- `resolveCell` — Function returning the value of a referenced cell.
|
|
683
|
+
- `options` — Evaluation options.
|
|
396
684
|
|
|
397
|
-
**Returns:**
|
|
685
|
+
**Returns:** Evaluated cell value (errors are returned, not thrown).
|
|
398
686
|
|
|
399
|
-
#### `
|
|
687
|
+
#### `parseFormula(input)`
|
|
400
688
|
|
|
401
|
-
|
|
689
|
+
Parse a formula string into an AST.
|
|
402
690
|
|
|
403
691
|
```typescript
|
|
404
|
-
function
|
|
692
|
+
function parseFormula(input: string): AstNode
|
|
405
693
|
```
|
|
406
694
|
|
|
407
|
-
- `
|
|
695
|
+
- `input` — Formula text (with or without a leading `=`).
|
|
408
696
|
|
|
409
|
-
**Returns:**
|
|
697
|
+
**Returns:** Root AST node.
|
|
410
698
|
|
|
411
699
|
#### `columnLettersToIndex(letters)`
|
|
412
700
|
|
|
@@ -420,103 +708,102 @@ function columnLettersToIndex(letters: string): number
|
|
|
420
708
|
|
|
421
709
|
**Returns:** 0-indexed column number.
|
|
422
710
|
|
|
423
|
-
#### `
|
|
711
|
+
#### `columnIndexToLetters(index)`
|
|
424
712
|
|
|
425
|
-
|
|
426
|
-
boolean compared as 0/1 within numbers).
|
|
713
|
+
Convert a 0-indexed column to letters (`0`→`A`, `25`→`Z`, `26`→`AA`).
|
|
427
714
|
|
|
428
715
|
```typescript
|
|
429
|
-
function
|
|
716
|
+
function columnIndexToLetters(index: number): string
|
|
430
717
|
```
|
|
431
718
|
|
|
432
|
-
- `
|
|
433
|
-
- `right` — Right operand.
|
|
719
|
+
- `index` — 0-indexed column.
|
|
434
720
|
|
|
435
|
-
**Returns:**
|
|
721
|
+
**Returns:** Column letters (uppercase).
|
|
436
722
|
|
|
437
|
-
#### `
|
|
723
|
+
#### `parseCellReference(ref)`
|
|
438
724
|
|
|
439
|
-
|
|
725
|
+
Parse a single cell reference (e.g. `A1`, `$A$1`, `BC42`).
|
|
440
726
|
|
|
441
727
|
```typescript
|
|
442
|
-
function
|
|
728
|
+
function parseCellReference(ref: string): CellCoord | null
|
|
443
729
|
```
|
|
444
730
|
|
|
445
|
-
- `
|
|
731
|
+
- `ref` — Reference text.
|
|
446
732
|
|
|
447
|
-
**Returns:**
|
|
733
|
+
**Returns:** Parsed cell coordinate, or `null` if invalid.
|
|
448
734
|
|
|
449
|
-
#### `
|
|
735
|
+
#### `parseCellRange(ref)`
|
|
450
736
|
|
|
451
|
-
|
|
737
|
+
Parse a cell range (e.g. `A1:B5`). Normalizes so `start` is top-left
|
|
738
|
+
and `end` is bottom-right regardless of input ordering.
|
|
452
739
|
|
|
453
740
|
```typescript
|
|
454
|
-
function
|
|
741
|
+
function parseCellRange(ref: string): CellRange | null
|
|
455
742
|
```
|
|
456
743
|
|
|
457
|
-
- `
|
|
744
|
+
- `ref` — Range text.
|
|
458
745
|
|
|
459
|
-
**Returns:**
|
|
746
|
+
**Returns:** Parsed range, or `null` if invalid.
|
|
460
747
|
|
|
461
|
-
#### `
|
|
748
|
+
#### `formatCellReference(coord)`
|
|
462
749
|
|
|
463
|
-
|
|
750
|
+
Format a cell coordinate as A1-style text (`{col:0,row:0}` → `A1`).
|
|
464
751
|
|
|
465
752
|
```typescript
|
|
466
|
-
function
|
|
753
|
+
function formatCellReference(coord: CellCoord): string
|
|
467
754
|
```
|
|
468
755
|
|
|
469
|
-
- `
|
|
470
|
-
- `resolveCell` — Function returning the value of a referenced cell.
|
|
471
|
-
- `options` — Evaluation options.
|
|
756
|
+
- `coord` — Cell coordinate.
|
|
472
757
|
|
|
473
|
-
**Returns:**
|
|
758
|
+
**Returns:** A1-style reference text.
|
|
474
759
|
|
|
475
|
-
#### `
|
|
760
|
+
#### `iterateRange(range)`
|
|
476
761
|
|
|
477
|
-
|
|
762
|
+
Iterate every coordinate in a range. Yields top-to-bottom, left-to-right.
|
|
478
763
|
|
|
479
764
|
```typescript
|
|
480
|
-
function
|
|
765
|
+
function iterateRange(range: CellRange): Generator<CellCoord, any, any>
|
|
481
766
|
```
|
|
482
767
|
|
|
483
|
-
- `
|
|
484
|
-
|
|
485
|
-
**Returns:** The first error value, or `null`.
|
|
768
|
+
- `range` — Cell range.
|
|
486
769
|
|
|
487
|
-
#### `
|
|
770
|
+
#### `coordKey(coord)`
|
|
488
771
|
|
|
489
|
-
|
|
772
|
+
Encode a coordinate to a cache-friendly string key (`"col,row"`).
|
|
490
773
|
|
|
491
774
|
```typescript
|
|
492
|
-
function
|
|
775
|
+
function coordKey(coord: CellCoord): string
|
|
493
776
|
```
|
|
494
777
|
|
|
495
778
|
- `coord` — Cell coordinate.
|
|
496
779
|
|
|
497
|
-
**Returns:**
|
|
780
|
+
**Returns:** Stable string key.
|
|
498
781
|
|
|
499
|
-
#### `
|
|
782
|
+
#### `refOf(coord)`
|
|
500
783
|
|
|
501
|
-
|
|
784
|
+
Reformat a cell coordinate as A1-style text. Re-exported for
|
|
785
|
+
convenience.
|
|
502
786
|
|
|
503
787
|
```typescript
|
|
504
|
-
function
|
|
788
|
+
function refOf(coord: CellCoord): string
|
|
505
789
|
```
|
|
506
790
|
|
|
507
|
-
- `
|
|
791
|
+
- `coord` — Cell coordinate.
|
|
508
792
|
|
|
509
|
-
**Returns:**
|
|
793
|
+
**Returns:** A1-style reference text.
|
|
510
794
|
|
|
511
|
-
#### `
|
|
795
|
+
#### `tokenize(input)`
|
|
512
796
|
|
|
513
|
-
|
|
797
|
+
Tokenize a formula string. The leading `=` (if present) is stripped
|
|
798
|
+
by the caller — pass the _body_ of the formula here.
|
|
514
799
|
|
|
515
800
|
```typescript
|
|
516
|
-
function
|
|
801
|
+
function tokenize(input: string): Token[]
|
|
517
802
|
```
|
|
518
803
|
|
|
519
|
-
- `
|
|
804
|
+
- `input` — Raw formula text (no leading `=`).
|
|
805
|
+
|
|
806
|
+
**Returns:** Flat token list.
|
|
520
807
|
|
|
521
808
|
#### `makeError(code, message)`
|
|
522
809
|
|
|
@@ -531,67 +818,59 @@ function makeError(code: FormulaErrorCode, message?: string): FormulaError
|
|
|
531
818
|
|
|
532
819
|
**Returns:** The error value.
|
|
533
820
|
|
|
534
|
-
#### `
|
|
821
|
+
#### `isError(value)`
|
|
535
822
|
|
|
536
|
-
|
|
537
|
-
and `end` is bottom-right regardless of input ordering.
|
|
823
|
+
Type-guard for `FormulaError`.
|
|
538
824
|
|
|
539
825
|
```typescript
|
|
540
|
-
function
|
|
826
|
+
function isError(value: unknown): boolean
|
|
541
827
|
```
|
|
542
828
|
|
|
543
|
-
- `
|
|
829
|
+
- `value` — Cell value to inspect.
|
|
544
830
|
|
|
545
|
-
**Returns:**
|
|
831
|
+
**Returns:** `true` if the value is a propagating error.
|
|
546
832
|
|
|
547
|
-
#### `
|
|
833
|
+
#### `firstError(values)`
|
|
548
834
|
|
|
549
|
-
|
|
835
|
+
Returns the first error encountered in a flat list, or `null` if none.
|
|
550
836
|
|
|
551
837
|
```typescript
|
|
552
|
-
function
|
|
838
|
+
function firstError(values: readonly CellValue[]): FormulaError | null
|
|
553
839
|
```
|
|
554
840
|
|
|
555
|
-
- `
|
|
556
|
-
|
|
557
|
-
**Returns:** Parsed cell coordinate, or `null` if invalid.
|
|
558
|
-
|
|
559
|
-
#### `parseFormula(input)`
|
|
560
|
-
|
|
561
|
-
Parse a formula string into an AST.
|
|
562
|
-
|
|
563
|
-
```typescript
|
|
564
|
-
function parseFormula(input: string): AstNode
|
|
565
|
-
```
|
|
841
|
+
- `values` — Cell values to scan.
|
|
566
842
|
|
|
567
|
-
|
|
843
|
+
**Returns:** The first error value, or `null`.
|
|
568
844
|
|
|
569
|
-
|
|
845
|
+
#### `toNumber(value)`
|
|
570
846
|
|
|
571
|
-
|
|
847
|
+
Coerce a cell value to a number (Excel-compatible).
|
|
572
848
|
|
|
573
|
-
|
|
574
|
-
|
|
849
|
+
- `null` → 0
|
|
850
|
+
- boolean → 0/1
|
|
851
|
+
- `Date` → days since 1899-12-30 (Excel's epoch)
|
|
852
|
+
- string → parsed as number, `#VALUE!` if not parseable
|
|
853
|
+
- error → propagated unchanged
|
|
575
854
|
|
|
576
855
|
```typescript
|
|
577
|
-
function
|
|
856
|
+
function toNumber(value: CellValue): number | FormulaError
|
|
578
857
|
```
|
|
579
858
|
|
|
580
|
-
- `
|
|
859
|
+
- `value` — Cell value.
|
|
581
860
|
|
|
582
|
-
**Returns:**
|
|
861
|
+
**Returns:** Number, or `FormulaError` on failure.
|
|
583
862
|
|
|
584
|
-
#### `
|
|
863
|
+
#### `toStringValue(value)`
|
|
585
864
|
|
|
586
|
-
|
|
865
|
+
Coerce a cell value to a string for display / `&` concatenation.
|
|
587
866
|
|
|
588
867
|
```typescript
|
|
589
|
-
function
|
|
868
|
+
function toStringValue(value: CellValue): string | FormulaError
|
|
590
869
|
```
|
|
591
870
|
|
|
592
|
-
- `
|
|
871
|
+
- `value` — Cell value.
|
|
593
872
|
|
|
594
|
-
**Returns:**
|
|
873
|
+
**Returns:** String, or `FormulaError` for propagating errors.
|
|
595
874
|
|
|
596
875
|
#### `toBoolean(value)`
|
|
597
876
|
|
|
@@ -605,70 +884,43 @@ function toBoolean(value: CellValue): boolean | FormulaError
|
|
|
605
884
|
|
|
606
885
|
**Returns:** Boolean, or `FormulaError` on failure.
|
|
607
886
|
|
|
608
|
-
#### `
|
|
609
|
-
|
|
610
|
-
Tokenize a formula string. The leading `=` (if present) is stripped
|
|
611
|
-
by the caller — pass the _body_ of the formula here.
|
|
612
|
-
|
|
613
|
-
```typescript
|
|
614
|
-
function tokenize(input: string): Token[]
|
|
615
|
-
```
|
|
616
|
-
|
|
617
|
-
- `input` — Raw formula text (no leading `=`).
|
|
618
|
-
|
|
619
|
-
**Returns:** Flat token list.
|
|
620
|
-
|
|
621
|
-
#### `toNumber(value)`
|
|
622
|
-
|
|
623
|
-
Coerce a cell value to a number (Excel-compatible).
|
|
887
|
+
#### `dateToSerial(date)`
|
|
624
888
|
|
|
625
|
-
|
|
626
|
-
- boolean → 0/1
|
|
627
|
-
- `Date` → days since 1899-12-30 (Excel's epoch)
|
|
628
|
-
- string → parsed as number, `#VALUE!` if not parseable
|
|
629
|
-
- error → propagated unchanged
|
|
889
|
+
Convert a `Date` to an Excel serial number (days since 1899-12-30).
|
|
630
890
|
|
|
631
891
|
```typescript
|
|
632
|
-
function
|
|
892
|
+
function dateToSerial(date: Date): number
|
|
633
893
|
```
|
|
634
894
|
|
|
635
|
-
- `
|
|
636
|
-
|
|
637
|
-
**Returns:** Number, or `FormulaError` on failure.
|
|
895
|
+
- `date` — Date instance.
|
|
638
896
|
|
|
639
|
-
|
|
897
|
+
**Returns:** Excel serial date.
|
|
640
898
|
|
|
641
|
-
|
|
642
|
-
such that, for any edge `a → b` (b depends on a), `a` precedes `b`
|
|
643
|
-
in the result.
|
|
899
|
+
#### `serialToDate(serial)`
|
|
644
900
|
|
|
645
|
-
|
|
646
|
-
returned in `cycle` and excluded from `order` — callers typically
|
|
647
|
-
mark cycle members with a `#CIRC!` error.
|
|
901
|
+
Convert an Excel serial number to a `Date` (UTC).
|
|
648
902
|
|
|
649
903
|
```typescript
|
|
650
|
-
function
|
|
651
|
-
dependents: ReadonlyMap<string, ReadonlySet<string>>,
|
|
652
|
-
keys: readonly string[],
|
|
653
|
-
): { order: string[]; cycle: Set<string> }
|
|
904
|
+
function serialToDate(serial: number): Date
|
|
654
905
|
```
|
|
655
906
|
|
|
656
|
-
- `
|
|
657
|
-
- `keys` — Keys to sort.
|
|
907
|
+
- `serial` — Excel serial date.
|
|
658
908
|
|
|
659
|
-
**Returns:**
|
|
909
|
+
**Returns:** Date instance.
|
|
660
910
|
|
|
661
|
-
#### `
|
|
911
|
+
#### `compareValues(left, right)`
|
|
662
912
|
|
|
663
|
-
|
|
913
|
+
Compare two scalar cell values (Excel ordering: number < string,
|
|
914
|
+
boolean compared as 0/1 within numbers).
|
|
664
915
|
|
|
665
916
|
```typescript
|
|
666
|
-
function
|
|
917
|
+
function compareValues(left: CellValue, right: CellValue): number | FormulaError
|
|
667
918
|
```
|
|
668
919
|
|
|
669
|
-
- `
|
|
920
|
+
- `left` — Left operand.
|
|
921
|
+
- `right` — Right operand.
|
|
670
922
|
|
|
671
|
-
**Returns:**
|
|
923
|
+
**Returns:** -1, 0, 1, or `FormulaError` if uncomparable.
|
|
672
924
|
|
|
673
925
|
### Constants
|
|
674
926
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/api-formula-engine",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Spreadsheet-style formula evaluator: parser, evaluator, dependency tracker for cell graphs with arithmetic, comparison, string ops, references, aggregations, conditionals, date/text functions, and circular-ref detection.",
|
|
5
5
|
"homepage": "https://www.molecule.dev/packages/api-formula-engine",
|
|
6
6
|
"type": "module",
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"directory": "packages/api/utilities/formula-engine"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@types/node": "26.
|
|
36
|
+
"@types/node": "26.6.5",
|
|
37
37
|
"typescript": "6.0.3",
|
|
38
|
-
"vitest": "4.1.
|
|
38
|
+
"vitest": "4.1.12"
|
|
39
39
|
}
|
|
40
40
|
}
|