@molecule/api-formula-engine 1.0.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/LICENSE +115 -0
- package/dist/browser-guard.d.ts +2 -0
- package/dist/browser-guard.d.ts.map +1 -0
- package/dist/browser-guard.js +18 -0
- package/dist/dependencies.d.ts +44 -0
- package/dist/dependencies.d.ts.map +1 -0
- package/dist/dependencies.js +110 -0
- package/dist/evaluator.d.ts +29 -0
- package/dist/evaluator.d.ts.map +1 -0
- package/dist/evaluator.js +159 -0
- package/dist/functions.d.ts +22 -0
- package/dist/functions.d.ts.map +1 -0
- package/dist/functions.js +774 -0
- package/dist/index.d.ts +67 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +66 -0
- package/dist/parser.d.ts +28 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +255 -0
- package/dist/references.d.ts +57 -0
- package/dist/references.d.ts.map +1 -0
- package/dist/references.js +116 -0
- package/dist/sheet.d.ts +130 -0
- package/dist/sheet.d.ts.map +1 -0
- package/dist/sheet.js +291 -0
- package/dist/tokenizer.d.ts +32 -0
- package/dist/tokenizer.d.ts.map +1 -0
- package/dist/tokenizer.js +187 -0
- package/dist/types.d.ts +141 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/dist/values.d.ts +80 -0
- package/dist/values.d.ts.map +1 -0
- package/dist/values.js +190 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-guard.d.ts","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser guard — `@molecule/api-formula-engine` is SERVER-ONLY.
|
|
3
|
+
*
|
|
4
|
+
* Generated by scripts/gen-browser-guards.mjs (workspace root) — edit THAT, not this.
|
|
5
|
+
* Evaluating a server package in a browser bundle is always an import-graph mistake
|
|
6
|
+
* (node APIs, secrets); without this guard it surfaces as a cryptic downstream crash
|
|
7
|
+
* ("Buffer is not defined") far from the culprit. Throwing here names the package and
|
|
8
|
+
* the fix at the exact moment the client bundle evaluates it. jsdom tests and SSR are
|
|
9
|
+
* unaffected: the throw requires browser globals AND the absence of a node runtime.
|
|
10
|
+
*/
|
|
11
|
+
const g = globalThis;
|
|
12
|
+
if (g.window !== undefined && g.document !== undefined && !g.process?.versions?.node) {
|
|
13
|
+
throw new Error('@molecule/api-formula-engine is SERVER-ONLY: it was bundled into browser/client code. Import it only ' +
|
|
14
|
+
'from server code (a server route/function or your API), or dynamic-import it inside ' +
|
|
15
|
+
'the server handler — never from components or shared client modules, and never ' +
|
|
16
|
+
'polyfill Buffer/process to silence this.');
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dependency-graph helpers used by the Sheet engine for incremental
|
|
3
|
+
* recompute and circular-reference detection.
|
|
4
|
+
*
|
|
5
|
+
* Pure functions — no I/O, no globals.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import { coordKey } from './references.js';
|
|
10
|
+
import type { AstNode, CellCoord } from './types.js';
|
|
11
|
+
/**
|
|
12
|
+
* Walk an AST and collect every coordinate it references (single
|
|
13
|
+
* references and every cell inside ranges).
|
|
14
|
+
*
|
|
15
|
+
* @param node - Root AST node.
|
|
16
|
+
* @returns Flat list of referenced coordinates (may contain duplicates).
|
|
17
|
+
*/
|
|
18
|
+
export declare function collectReferences(node: AstNode): CellCoord[];
|
|
19
|
+
/**
|
|
20
|
+
* Topologically sort a set of coordinate keys by their dependency graph
|
|
21
|
+
* such that, for any edge `a → b` (b depends on a), `a` precedes `b`
|
|
22
|
+
* in the result.
|
|
23
|
+
*
|
|
24
|
+
* Uses Kahn's algorithm. If a cycle is detected, the cycle members are
|
|
25
|
+
* returned in `cycle` and excluded from `order` — callers typically
|
|
26
|
+
* mark cycle members with a `#CIRC!` error.
|
|
27
|
+
*
|
|
28
|
+
* @param dependents - Map from a key to the set of keys that depend on it.
|
|
29
|
+
* (i.e. forward edges in the recompute order.)
|
|
30
|
+
* @param keys - Keys to sort.
|
|
31
|
+
* @returns `{ order, cycle }` — `order` is the topological order;
|
|
32
|
+
* `cycle` is the set of keys participating in (or downstream of) a
|
|
33
|
+
* cycle.
|
|
34
|
+
*/
|
|
35
|
+
export declare function topologicalSort(dependents: ReadonlyMap<string, ReadonlySet<string>>, keys: ReadonlyArray<string>): {
|
|
36
|
+
order: string[];
|
|
37
|
+
cycle: Set<string>;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Encode a coordinate to its canonical string key. Re-exported from
|
|
41
|
+
* `references` for convenience.
|
|
42
|
+
*/
|
|
43
|
+
export { coordKey };
|
|
44
|
+
//# sourceMappingURL=dependencies.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dependencies.d.ts","sourceRoot":"","sources":["../src/dependencies.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,QAAQ,EAAgB,MAAM,iBAAiB,CAAA;AACxD,OAAO,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAEpD;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,OAAO,GAAG,SAAS,EAAE,CAI5D;AA4BD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,eAAe,CAC7B,UAAU,EAAE,WAAW,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC,EACpD,IAAI,EAAE,aAAa,CAAC,MAAM,CAAC,GAC1B;IAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IAAC,KAAK,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;CAAE,CA8BzC;AAED;;;GAGG;AACH,OAAO,EAAE,QAAQ,EAAE,CAAA"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dependency-graph helpers used by the Sheet engine for incremental
|
|
3
|
+
* recompute and circular-reference detection.
|
|
4
|
+
*
|
|
5
|
+
* Pure functions — no I/O, no globals.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import { coordKey, iterateRange } from './references.js';
|
|
10
|
+
/**
|
|
11
|
+
* Walk an AST and collect every coordinate it references (single
|
|
12
|
+
* references and every cell inside ranges).
|
|
13
|
+
*
|
|
14
|
+
* @param node - Root AST node.
|
|
15
|
+
* @returns Flat list of referenced coordinates (may contain duplicates).
|
|
16
|
+
*/
|
|
17
|
+
export function collectReferences(node) {
|
|
18
|
+
const acc = [];
|
|
19
|
+
walk(node, acc);
|
|
20
|
+
return acc;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Recursively walk an AST node and push every referenced coordinate into `acc`.
|
|
24
|
+
*/
|
|
25
|
+
function walk(node, acc) {
|
|
26
|
+
switch (node.kind) {
|
|
27
|
+
case 'reference':
|
|
28
|
+
acc.push(node.coord);
|
|
29
|
+
return;
|
|
30
|
+
case 'range':
|
|
31
|
+
for (const c of iterateRange(node.range))
|
|
32
|
+
acc.push(c);
|
|
33
|
+
return;
|
|
34
|
+
case 'unary':
|
|
35
|
+
walk(node.operand, acc);
|
|
36
|
+
return;
|
|
37
|
+
case 'binary':
|
|
38
|
+
walk(node.left, acc);
|
|
39
|
+
walk(node.right, acc);
|
|
40
|
+
return;
|
|
41
|
+
case 'call':
|
|
42
|
+
for (const a of node.args)
|
|
43
|
+
walk(a, acc);
|
|
44
|
+
return;
|
|
45
|
+
default:
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Topologically sort a set of coordinate keys by their dependency graph
|
|
51
|
+
* such that, for any edge `a → b` (b depends on a), `a` precedes `b`
|
|
52
|
+
* in the result.
|
|
53
|
+
*
|
|
54
|
+
* Uses Kahn's algorithm. If a cycle is detected, the cycle members are
|
|
55
|
+
* returned in `cycle` and excluded from `order` — callers typically
|
|
56
|
+
* mark cycle members with a `#CIRC!` error.
|
|
57
|
+
*
|
|
58
|
+
* @param dependents - Map from a key to the set of keys that depend on it.
|
|
59
|
+
* (i.e. forward edges in the recompute order.)
|
|
60
|
+
* @param keys - Keys to sort.
|
|
61
|
+
* @returns `{ order, cycle }` — `order` is the topological order;
|
|
62
|
+
* `cycle` is the set of keys participating in (or downstream of) a
|
|
63
|
+
* cycle.
|
|
64
|
+
*/
|
|
65
|
+
export function topologicalSort(dependents, keys) {
|
|
66
|
+
const inDegree = new Map();
|
|
67
|
+
const set = new Set(keys);
|
|
68
|
+
for (const k of set)
|
|
69
|
+
inDegree.set(k, 0);
|
|
70
|
+
for (const k of set) {
|
|
71
|
+
const outs = dependents.get(k);
|
|
72
|
+
if (!outs)
|
|
73
|
+
continue;
|
|
74
|
+
for (const target of outs) {
|
|
75
|
+
if (!set.has(target))
|
|
76
|
+
continue;
|
|
77
|
+
inDegree.set(target, (inDegree.get(target) ?? 0) + 1);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const queue = [];
|
|
81
|
+
for (const [k, d] of inDegree)
|
|
82
|
+
if (d === 0)
|
|
83
|
+
queue.push(k);
|
|
84
|
+
const order = [];
|
|
85
|
+
while (queue.length > 0) {
|
|
86
|
+
const k = queue.shift();
|
|
87
|
+
order.push(k);
|
|
88
|
+
const outs = dependents.get(k);
|
|
89
|
+
if (!outs)
|
|
90
|
+
continue;
|
|
91
|
+
for (const target of outs) {
|
|
92
|
+
if (!set.has(target))
|
|
93
|
+
continue;
|
|
94
|
+
const next = (inDegree.get(target) ?? 0) - 1;
|
|
95
|
+
inDegree.set(target, next);
|
|
96
|
+
if (next === 0)
|
|
97
|
+
queue.push(target);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const cycle = new Set();
|
|
101
|
+
for (const k of set)
|
|
102
|
+
if (!order.includes(k))
|
|
103
|
+
cycle.add(k);
|
|
104
|
+
return { order, cycle };
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Encode a coordinate to its canonical string key. Re-exported from
|
|
108
|
+
* `references` for convenience.
|
|
109
|
+
*/
|
|
110
|
+
export { coordKey };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AST evaluator.
|
|
3
|
+
*
|
|
4
|
+
* Walks an AST node and produces a `CellValue`. Cell references are
|
|
5
|
+
* resolved through a caller-supplied `resolveCell` lookup function so
|
|
6
|
+
* the evaluator stays decoupled from the underlying sheet storage.
|
|
7
|
+
*
|
|
8
|
+
* Pure function — no I/O, no globals.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
import type { AstNode, CellCoord, CellValue, EvaluateOptions } from './types.js';
|
|
13
|
+
/**
|
|
14
|
+
* Function used to resolve a cell coordinate to its value. Returns
|
|
15
|
+
* `null` for empty cells. Implementations may evaluate dependent
|
|
16
|
+
* formulas (in which case they should detect cycles and return a
|
|
17
|
+
* `#CIRC!` error to the caller).
|
|
18
|
+
*/
|
|
19
|
+
export type ResolveCell = (coord: CellCoord) => CellValue;
|
|
20
|
+
/**
|
|
21
|
+
* Evaluate an AST in the context of a cell-resolution function.
|
|
22
|
+
*
|
|
23
|
+
* @param node - Root AST node.
|
|
24
|
+
* @param resolveCell - Function returning the value of a referenced cell.
|
|
25
|
+
* @param options - Evaluation options.
|
|
26
|
+
* @returns Evaluated cell value (errors are returned, not thrown).
|
|
27
|
+
*/
|
|
28
|
+
export declare function evaluate(node: AstNode, resolveCell: ResolveCell, options?: EvaluateOptions): CellValue;
|
|
29
|
+
//# sourceMappingURL=evaluator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"evaluator.d.ts","sourceRoot":"","sources":["../src/evaluator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAIH,OAAO,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,eAAe,EAAmB,MAAM,YAAY,CAAA;AAGjG;;;;;GAKG;AACH,MAAM,MAAM,WAAW,GAAG,CAAC,KAAK,EAAE,SAAS,KAAK,SAAS,CAAA;AAEzD;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CACtB,IAAI,EAAE,OAAO,EACb,WAAW,EAAE,WAAW,EACxB,OAAO,GAAE,eAAoB,GAC5B,SAAS,CAKX"}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AST evaluator.
|
|
3
|
+
*
|
|
4
|
+
* Walks an AST node and produces a `CellValue`. Cell references are
|
|
5
|
+
* resolved through a caller-supplied `resolveCell` lookup function so
|
|
6
|
+
* the evaluator stays decoupled from the underlying sheet storage.
|
|
7
|
+
*
|
|
8
|
+
* Pure function — no I/O, no globals.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
import { BUILTIN_FUNCTIONS } from './functions.js';
|
|
13
|
+
import { iterateRange } from './references.js';
|
|
14
|
+
import { compareValues, isError, makeError, toNumber, toStringValue } from './values.js';
|
|
15
|
+
/**
|
|
16
|
+
* Evaluate an AST in the context of a cell-resolution function.
|
|
17
|
+
*
|
|
18
|
+
* @param node - Root AST node.
|
|
19
|
+
* @param resolveCell - Function returning the value of a referenced cell.
|
|
20
|
+
* @param options - Evaluation options.
|
|
21
|
+
* @returns Evaluated cell value (errors are returned, not thrown).
|
|
22
|
+
*/
|
|
23
|
+
export function evaluate(node, resolveCell, options = {}) {
|
|
24
|
+
const ctx = {
|
|
25
|
+
now: options.now ?? (() => new Date()),
|
|
26
|
+
};
|
|
27
|
+
return evalNode(node, resolveCell, ctx);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Recursively evaluate a single AST node and return its cell value.
|
|
31
|
+
*/
|
|
32
|
+
function evalNode(node, resolveCell, ctx) {
|
|
33
|
+
switch (node.kind) {
|
|
34
|
+
case 'number':
|
|
35
|
+
return node.value;
|
|
36
|
+
case 'string':
|
|
37
|
+
return node.value;
|
|
38
|
+
case 'boolean':
|
|
39
|
+
return node.value;
|
|
40
|
+
case 'errorLiteral':
|
|
41
|
+
return makeError(node.code);
|
|
42
|
+
case 'reference':
|
|
43
|
+
return resolveCell(node.coord);
|
|
44
|
+
case 'range':
|
|
45
|
+
// A range used outside a function context coerces to its first cell.
|
|
46
|
+
return resolveCell(node.range.start);
|
|
47
|
+
case 'unary':
|
|
48
|
+
return evalUnary(node.op, evalNode(node.operand, resolveCell, ctx));
|
|
49
|
+
case 'binary':
|
|
50
|
+
return evalBinary(node.op, evalNode(node.left, resolveCell, ctx), evalNode(node.right, resolveCell, ctx));
|
|
51
|
+
case 'call':
|
|
52
|
+
return evalCall(node.name, node.args, resolveCell, ctx);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Apply a unary operator (`+`, `-`, `%`) to a cell value and return the result.
|
|
57
|
+
*/
|
|
58
|
+
function evalUnary(op, operand) {
|
|
59
|
+
if (isError(operand))
|
|
60
|
+
return operand;
|
|
61
|
+
const n = toNumber(operand);
|
|
62
|
+
if (isError(n))
|
|
63
|
+
return n;
|
|
64
|
+
switch (op) {
|
|
65
|
+
case '+':
|
|
66
|
+
return n;
|
|
67
|
+
case '-':
|
|
68
|
+
return -n;
|
|
69
|
+
case '%':
|
|
70
|
+
return n / 100;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Apply a binary operator to two cell values and return the result.
|
|
75
|
+
*/
|
|
76
|
+
function evalBinary(op, left, right) {
|
|
77
|
+
if (isError(left))
|
|
78
|
+
return left;
|
|
79
|
+
if (isError(right))
|
|
80
|
+
return right;
|
|
81
|
+
if (op === '&') {
|
|
82
|
+
const l = toStringValue(left);
|
|
83
|
+
if (isError(l))
|
|
84
|
+
return l;
|
|
85
|
+
const r = toStringValue(right);
|
|
86
|
+
if (isError(r))
|
|
87
|
+
return r;
|
|
88
|
+
return l + r;
|
|
89
|
+
}
|
|
90
|
+
if (op === '=' || op === '<>' || op === '<' || op === '<=' || op === '>' || op === '>=') {
|
|
91
|
+
const cmp = compareValues(left, right);
|
|
92
|
+
if (isError(cmp))
|
|
93
|
+
return cmp;
|
|
94
|
+
switch (op) {
|
|
95
|
+
case '=':
|
|
96
|
+
return cmp === 0;
|
|
97
|
+
case '<>':
|
|
98
|
+
return cmp !== 0;
|
|
99
|
+
case '<':
|
|
100
|
+
return cmp < 0;
|
|
101
|
+
case '<=':
|
|
102
|
+
return cmp <= 0;
|
|
103
|
+
case '>':
|
|
104
|
+
return cmp > 0;
|
|
105
|
+
case '>=':
|
|
106
|
+
return cmp >= 0;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// Arithmetic
|
|
110
|
+
const l = toNumber(left);
|
|
111
|
+
if (isError(l))
|
|
112
|
+
return l;
|
|
113
|
+
const r = toNumber(right);
|
|
114
|
+
if (isError(r))
|
|
115
|
+
return r;
|
|
116
|
+
switch (op) {
|
|
117
|
+
case '+':
|
|
118
|
+
return l + r;
|
|
119
|
+
case '-':
|
|
120
|
+
return l - r;
|
|
121
|
+
case '*':
|
|
122
|
+
return l * r;
|
|
123
|
+
case '/':
|
|
124
|
+
if (r === 0)
|
|
125
|
+
return makeError('#DIV/0!');
|
|
126
|
+
return l / r;
|
|
127
|
+
case '^': {
|
|
128
|
+
const v = l ** r;
|
|
129
|
+
if (!Number.isFinite(v))
|
|
130
|
+
return makeError('#NUM!');
|
|
131
|
+
return v;
|
|
132
|
+
}
|
|
133
|
+
default:
|
|
134
|
+
return makeError('#VALUE!', `Unknown operator: ${op}`);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Look up and invoke a built-in function by name, expanding range arguments into arrays.
|
|
139
|
+
*/
|
|
140
|
+
function evalCall(name, argNodes, resolveCell, ctx) {
|
|
141
|
+
const fn = BUILTIN_FUNCTIONS[name];
|
|
142
|
+
if (!fn)
|
|
143
|
+
return makeError('#NAME?', `Unknown function: ${name}`);
|
|
144
|
+
// Evaluate each argument: ranges expand into arrays.
|
|
145
|
+
const args = [];
|
|
146
|
+
for (const arg of argNodes) {
|
|
147
|
+
if (arg.kind === 'range') {
|
|
148
|
+
const expanded = [];
|
|
149
|
+
for (const c of iterateRange(arg.range)) {
|
|
150
|
+
expanded.push(resolveCell(c));
|
|
151
|
+
}
|
|
152
|
+
args.push(expanded);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
args.push(evalNode(arg, resolveCell, ctx));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return fn(args, ctx);
|
|
159
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Built-in function registry.
|
|
3
|
+
*
|
|
4
|
+
* Each entry is a pure function: given a list of evaluated argument
|
|
5
|
+
* values (scalars or expanded ranges), returns a `CellValue`.
|
|
6
|
+
*
|
|
7
|
+
* Implements: SUM, AVERAGE/AVG, MIN, MAX, COUNT, COUNTA, COUNTIF,
|
|
8
|
+
* SUMIF, IF, AND, OR, NOT, IFS, SWITCH, IFERROR, ISERROR, ISNUMBER,
|
|
9
|
+
* ISBLANK, CONCAT/CONCATENATE, LEFT, RIGHT, MID, LEN, TRIM, UPPER,
|
|
10
|
+
* LOWER, SUBSTITUTE, ROUND, ABS, MOD, POWER, SQRT, INT, CEILING,
|
|
11
|
+
* FLOOR, DATE, TODAY, NOW, YEAR, MONTH, DAY, DATEDIF / DATEDIFF.
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/
|
|
15
|
+
import type { FormulaFunction } from './types.js';
|
|
16
|
+
/**
|
|
17
|
+
* Map of function names (uppercase) to implementations. Aliases like
|
|
18
|
+
* `AVG → AVERAGE` and `DATEDIF → DATEDIFF` map to the same function
|
|
19
|
+
* reference.
|
|
20
|
+
*/
|
|
21
|
+
export declare const BUILTIN_FUNCTIONS: Readonly<Record<string, FormulaFunction>>;
|
|
22
|
+
//# sourceMappingURL=functions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"functions.d.ts","sourceRoot":"","sources":["../src/functions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EAAa,eAAe,EAAmB,MAAM,YAAY,CAAA;AA6nB7E;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CA8CtE,CAAA"}
|