@kayr/umbika-core 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +18 -2
- package/THIRD_PARTY_LICENSES.txt +20 -0
- package/dist/calculation-pass.d.ts +13 -0
- package/dist/calculation-pass.js +60 -0
- package/dist/choice-options.d.ts +2 -0
- package/dist/choice-options.js +28 -0
- package/dist/condition-evaluation.d.ts +26 -0
- package/dist/condition-evaluation.js +294 -0
- package/dist/expression-types.d.ts +47 -0
- package/dist/expression-types.js +1 -0
- package/dist/expressions/interpreter.d.ts +8 -0
- package/dist/expressions/interpreter.js +272 -0
- package/dist/expressions/parser.d.ts +2 -0
- package/dist/expressions/parser.js +37 -0
- package/dist/expressions/preparation.d.ts +34 -0
- package/dist/expressions/preparation.js +190 -0
- package/dist/expressions/references.d.ts +26 -0
- package/dist/expressions/references.js +184 -0
- package/dist/expressions.d.ts +8 -0
- package/dist/expressions.js +24 -0
- package/dist/field-path.d.ts +47 -0
- package/dist/field-path.js +95 -0
- package/dist/field-validation.d.ts +4 -0
- package/dist/field-validation.js +77 -0
- package/dist/form-evaluation.d.ts +46 -0
- package/dist/form-evaluation.js +110 -0
- package/dist/form.d.ts +7 -0
- package/dist/form.js +333 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/input-rules.d.ts +9 -0
- package/dist/input-rules.js +236 -0
- package/dist/language/diagnostics.d.ts +44 -0
- package/dist/language/diagnostics.js +42 -0
- package/dist/language/lower.d.ts +12 -0
- package/dist/language/lower.js +208 -0
- package/dist/language/parser.d.ts +66 -0
- package/dist/language/parser.js +493 -0
- package/dist/language/scanner.d.ts +37 -0
- package/dist/language/scanner.js +183 -0
- package/dist/language-types.d.ts +31 -0
- package/dist/language-types.js +1 -0
- package/dist/language.d.ts +8 -0
- package/dist/language.js +123 -0
- package/dist/schema-ownership.d.ts +4 -0
- package/dist/schema-ownership.js +25 -0
- package/dist/schema-validation.d.ts +2 -0
- package/dist/schema-validation.js +159 -0
- package/dist/standalone/form-engine-expressions.min.d.mts +444 -0
- package/dist/standalone/form-engine-expressions.min.d.ts +444 -0
- package/dist/standalone/form-engine-expressions.min.js +46 -0
- package/dist/standalone/form-engine-expressions.min.mjs +46 -0
- package/dist/standalone/form-engine-language.min.d.mts +474 -0
- package/dist/standalone/form-engine-language.min.d.ts +474 -0
- package/dist/standalone/form-engine-language.min.js +60 -0
- package/dist/standalone/form-engine-language.min.mjs +60 -0
- package/dist/standalone/form-engine.min.d.mts +383 -0
- package/dist/standalone/form-engine.min.d.ts +383 -0
- package/dist/standalone/form-engine.min.js +24 -0
- package/dist/standalone/form-engine.min.mjs +24 -0
- package/dist/submission-projection.d.ts +3 -0
- package/dist/submission-projection.js +55 -0
- package/dist/types.d.ts +351 -0
- package/dist/types.js +1 -0
- package/dist/vars.d.ts +2 -0
- package/dist/vars.js +45 -0
- package/dist/working-data.d.ts +11 -0
- package/dist/working-data.js +89 -0
- package/package.json +42 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kayr
|
|
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/README.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kayr/umbika-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Experimental form engine for JavaScript and TypeScript. The core owns form
|
|
4
|
+
answers, rules, validation, and submission data independently of the UI.
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
npm install @kayr/umbika-core
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```js
|
|
11
|
+
import { createForm } from "@kayr/umbika-core";
|
|
12
|
+
|
|
13
|
+
const form = createForm({
|
|
14
|
+
schema: [{ type: "text", id: "name", label: "Name", required: true }],
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
form.input(["name"], "Ada");
|
|
18
|
+
console.log(form.getSubmissionData()); // { name: "Ada" }
|
|
19
|
+
```
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2013 Stephen Oney, https://ericsmekens.github.io/jsep/
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { CalculatedField, CalculatedValue, ConditionScope, FieldPath } from "./types.js";
|
|
2
|
+
import type { WorkingData } from "./working-data.js";
|
|
3
|
+
export interface CalculatedInstance {
|
|
4
|
+
node: CalculatedField;
|
|
5
|
+
path: FieldPath;
|
|
6
|
+
parentValues: WorkingData;
|
|
7
|
+
}
|
|
8
|
+
export declare function createCalculationPass(scopesByPath: ReadonlyMap<string, ConditionScope>, instancesByPath: ReadonlyMap<string, CalculatedInstance>): {
|
|
9
|
+
resolve: (node: CalculatedField, path: FieldPath) => CalculatedValue;
|
|
10
|
+
clearCalculatedValues(): void;
|
|
11
|
+
resolveAllCalculations(): void;
|
|
12
|
+
commitCalculatedResults(): void;
|
|
13
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { pathKey } from "./field-path.js";
|
|
2
|
+
// Hold the temporary results and active path stack for one form evaluation.
|
|
3
|
+
export function createCalculationPass(scopesByPath, instancesByPath) {
|
|
4
|
+
const results = new Map();
|
|
5
|
+
const stack = [];
|
|
6
|
+
function resolve(node, path) {
|
|
7
|
+
const key = pathKey(path);
|
|
8
|
+
if (results.has(key))
|
|
9
|
+
return results.get(key);
|
|
10
|
+
const cycleStart = stack.findIndex((activePath) => pathKey(activePath) === key);
|
|
11
|
+
if (cycleStart !== -1) {
|
|
12
|
+
const cycle = [...stack.slice(cycleStart), path];
|
|
13
|
+
throw new Error(`Circular calculation: ${cycle.map(pathKey).join(" → ")}`);
|
|
14
|
+
}
|
|
15
|
+
const scope = scopesByPath.get(key);
|
|
16
|
+
if (!scope)
|
|
17
|
+
throw new Error(`missing calculation scope for ${key}`);
|
|
18
|
+
stack.push(path);
|
|
19
|
+
try {
|
|
20
|
+
const result = node.calculate(scope);
|
|
21
|
+
requireCalculatedValue(result, path);
|
|
22
|
+
results.set(key, result);
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
finally {
|
|
26
|
+
stack.pop();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
resolve,
|
|
31
|
+
clearCalculatedValues() {
|
|
32
|
+
for (const { node, parentValues } of instancesByPath.values()) {
|
|
33
|
+
delete parentValues[node.id];
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
resolveAllCalculations() {
|
|
37
|
+
for (const { node, path } of instancesByPath.values()) {
|
|
38
|
+
resolve(node, path);
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
commitCalculatedResults() {
|
|
42
|
+
for (const [key, value] of results) {
|
|
43
|
+
const instance = instancesByPath.get(key);
|
|
44
|
+
if (!instance) {
|
|
45
|
+
throw new Error(`missing calculated field for ${key}`);
|
|
46
|
+
}
|
|
47
|
+
instance.parentValues[instance.node.id] = value;
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function requireCalculatedValue(value, path) {
|
|
53
|
+
if (value !== null &&
|
|
54
|
+
value !== undefined &&
|
|
55
|
+
typeof value !== "string" &&
|
|
56
|
+
typeof value !== "number" &&
|
|
57
|
+
typeof value !== "boolean") {
|
|
58
|
+
throw new TypeError(`calculation at ${JSON.stringify(path)} must return a primitive value`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { validateOptions } from "./schema-validation.js";
|
|
2
|
+
// Resolve and own the choices for one relevant concrete field.
|
|
3
|
+
export function resolveOptions(field, scope, path) {
|
|
4
|
+
if (typeof field.options !== "function")
|
|
5
|
+
return field.options;
|
|
6
|
+
const name = `field ${JSON.stringify(path)}`;
|
|
7
|
+
let result;
|
|
8
|
+
try {
|
|
9
|
+
result = field.options(scope);
|
|
10
|
+
}
|
|
11
|
+
catch (cause) {
|
|
12
|
+
throw new Error(`${name} options evaluation failed`, { cause });
|
|
13
|
+
}
|
|
14
|
+
if (result != null &&
|
|
15
|
+
(typeof result === "object" || typeof result === "function") &&
|
|
16
|
+
typeof result.then === "function")
|
|
17
|
+
throw new TypeError(`${name} options must complete synchronously`);
|
|
18
|
+
const options = result ?? [];
|
|
19
|
+
validateOptions(name, options, true);
|
|
20
|
+
try {
|
|
21
|
+
return structuredClone(options);
|
|
22
|
+
}
|
|
23
|
+
catch (cause) {
|
|
24
|
+
throw new TypeError(`${name} options must have cloneable metadata`, {
|
|
25
|
+
cause,
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { CalculatedField, ConditionScope, FieldPath, FormField, FormNode, FormVars } from "./types.js";
|
|
2
|
+
import type { WorkingData, RowIdentities } from "./working-data.js";
|
|
3
|
+
export interface FieldRuleScope extends ConditionScope {
|
|
4
|
+
readonly value: unknown;
|
|
5
|
+
}
|
|
6
|
+
export type BoundObjectValidators = WeakMap<FieldRuleScope, (relevance: ReadonlyMap<string, boolean>) => string | undefined>;
|
|
7
|
+
export declare function evaluateConditions(field: FormField, scope: ConditionScope, relevant: boolean): {
|
|
8
|
+
relevant: boolean;
|
|
9
|
+
required: boolean;
|
|
10
|
+
readOnly: boolean;
|
|
11
|
+
};
|
|
12
|
+
export declare function evaluateRelevance(node: FormNode, scope: ConditionScope): boolean;
|
|
13
|
+
export declare function evaluateReadOnlyAtPath(nodes: readonly FormNode[], path: FieldPath, scopesByPath: ReadonlyMap<string, FieldRuleScope>): boolean;
|
|
14
|
+
export declare function createConditionScopeStore(nodes: readonly FormNode[], answers: WorkingData, rowIdentities: RowIdentities, vars: FormVars): {
|
|
15
|
+
scopesByPath: Map<string, FieldRuleScope>;
|
|
16
|
+
objectValidators: BoundObjectValidators;
|
|
17
|
+
beginEvaluation(): {
|
|
18
|
+
resolve: (node: CalculatedField, path: FieldPath) => import("./types.js").CalculatedValue;
|
|
19
|
+
clearCalculatedValues(): void;
|
|
20
|
+
resolveAllCalculations(): void;
|
|
21
|
+
commitCalculatedResults(): void;
|
|
22
|
+
};
|
|
23
|
+
endEvaluation(): void;
|
|
24
|
+
sync: () => void;
|
|
25
|
+
removeBelow(path: FieldPath): void;
|
|
26
|
+
};
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { createCalculationPass, } from "./calculation-pass.js";
|
|
2
|
+
import { appendPath, pathKey, requireRowId } from "./field-path.js";
|
|
3
|
+
// Evaluate leaf conditions after inherited relevance is known.
|
|
4
|
+
export function evaluateConditions(field, scope, relevant) {
|
|
5
|
+
return {
|
|
6
|
+
relevant,
|
|
7
|
+
required: relevant && evaluateCondition(field.required, false, scope, "required"),
|
|
8
|
+
readOnly: field.type === "calculated" ||
|
|
9
|
+
(relevant && evaluateReadOnly(field, scope)),
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export function evaluateRelevance(node, scope) {
|
|
13
|
+
return evaluateCondition(node.relevant, true, scope, "relevant");
|
|
14
|
+
}
|
|
15
|
+
// Resolve one direct-edit lock during recovery using only the relevance chain
|
|
16
|
+
// of the already resolved concrete leaf and its own condition.
|
|
17
|
+
export function evaluateReadOnlyAtPath(nodes, path, scopesByPath) {
|
|
18
|
+
let currentNodes = nodes;
|
|
19
|
+
for (let index = 0; index < path.length; index += 1) {
|
|
20
|
+
const node = currentNodes.find(({ id }) => id === path[index]);
|
|
21
|
+
const key = pathKey(path.slice(0, index + 1));
|
|
22
|
+
const scope = scopesByPath.get(key);
|
|
23
|
+
if (!node || !scope)
|
|
24
|
+
throw new Error(`missing condition scope for ${key}`);
|
|
25
|
+
if (!evaluateRelevance(node, scope))
|
|
26
|
+
return false;
|
|
27
|
+
if (node.type === "object" || node.type === "table") {
|
|
28
|
+
currentNodes = node.children;
|
|
29
|
+
if (node.type === "table")
|
|
30
|
+
index += 1; // Skip the concrete row ID.
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
return evaluateReadOnly(node, scope);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
throw new Error(`missing field for ${pathKey(path)}`);
|
|
37
|
+
}
|
|
38
|
+
function evaluateReadOnly(field, scope) {
|
|
39
|
+
return evaluateCondition(field.readOnly, false, scope, "readOnly");
|
|
40
|
+
}
|
|
41
|
+
// Create stable scopes and structural contexts, then add scopes for appended
|
|
42
|
+
// rows on demand without replacing existing instances.
|
|
43
|
+
export function createConditionScopeStore(nodes, answers, rowIdentities, vars) {
|
|
44
|
+
const _scopesByPath = new Map();
|
|
45
|
+
const _objectValidators = new WeakMap();
|
|
46
|
+
const _contextsByPath = new Map();
|
|
47
|
+
const _calculatedInstancesByPath = new Map();
|
|
48
|
+
let _activeCalculationPass = null;
|
|
49
|
+
const _formView = _createValuesView(nodes, answers, []);
|
|
50
|
+
const _formContext = {
|
|
51
|
+
kind: "form",
|
|
52
|
+
data: _formView,
|
|
53
|
+
path: [],
|
|
54
|
+
parent: null,
|
|
55
|
+
id: null,
|
|
56
|
+
index: null,
|
|
57
|
+
previous: null,
|
|
58
|
+
};
|
|
59
|
+
_contextsByPath.set(pathKey([]), _formContext);
|
|
60
|
+
function _syncNodes(currentNodes, parentValues, parent, parentContext, parentPath) {
|
|
61
|
+
for (const node of currentNodes) {
|
|
62
|
+
const path = appendPath(parentPath, node.id);
|
|
63
|
+
const key = pathKey(path);
|
|
64
|
+
_ensureScope(node, parent, parentContext, path, key);
|
|
65
|
+
if (node.type === "calculated") {
|
|
66
|
+
_registerCalculatedInstance(node, parentValues, path, key);
|
|
67
|
+
}
|
|
68
|
+
if (node.type === "object") {
|
|
69
|
+
_syncObject(node, parentValues, parentContext, path, key);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (node.type === "table") {
|
|
73
|
+
_syncTable(node, parentValues, parentContext, path, key);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function _ensureScope(node, parent, parentContext, path, key) {
|
|
78
|
+
if (_scopesByPath.has(key))
|
|
79
|
+
return;
|
|
80
|
+
const common = {
|
|
81
|
+
form: _formView,
|
|
82
|
+
parent,
|
|
83
|
+
context: parentContext,
|
|
84
|
+
vars,
|
|
85
|
+
};
|
|
86
|
+
if (node.type === "table") {
|
|
87
|
+
_scopesByPath.set(key, Object.freeze({
|
|
88
|
+
...common,
|
|
89
|
+
get value() {
|
|
90
|
+
return parent[node.id];
|
|
91
|
+
},
|
|
92
|
+
get rows() {
|
|
93
|
+
return parent[node.id];
|
|
94
|
+
},
|
|
95
|
+
}));
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const scope = Object.freeze({
|
|
99
|
+
...common,
|
|
100
|
+
get value() {
|
|
101
|
+
return parent[node.id];
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
_scopesByPath.set(key, scope);
|
|
105
|
+
if (node.type === "object" && node.validate !== undefined) {
|
|
106
|
+
_objectValidators.set(scope, bindObjectValidator(node.validate, path, scope));
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function _registerCalculatedInstance(node, parentValues, path, key) {
|
|
110
|
+
_calculatedInstancesByPath.set(key, { node, path, parentValues });
|
|
111
|
+
}
|
|
112
|
+
function _syncObject(node, parentValues, parentContext, path, key) {
|
|
113
|
+
const childValues = parentValues[node.id];
|
|
114
|
+
let context = _contextsByPath.get(key);
|
|
115
|
+
if (!context) {
|
|
116
|
+
context = {
|
|
117
|
+
kind: "object",
|
|
118
|
+
data: _createValuesView(node.children, childValues, path),
|
|
119
|
+
path,
|
|
120
|
+
parent: parentContext,
|
|
121
|
+
id: null,
|
|
122
|
+
index: null,
|
|
123
|
+
previous: null,
|
|
124
|
+
};
|
|
125
|
+
_contextsByPath.set(key, context);
|
|
126
|
+
}
|
|
127
|
+
_syncNodes(node.children, childValues, context.data, context, path);
|
|
128
|
+
}
|
|
129
|
+
function _syncTable(node, parentValues, parentContext, path, key) {
|
|
130
|
+
const rows = parentValues[node.id];
|
|
131
|
+
let tableContext = _contextsByPath.get(key);
|
|
132
|
+
if (!tableContext) {
|
|
133
|
+
tableContext = {
|
|
134
|
+
kind: "table",
|
|
135
|
+
data: [],
|
|
136
|
+
path,
|
|
137
|
+
parent: parentContext,
|
|
138
|
+
id: null,
|
|
139
|
+
index: null,
|
|
140
|
+
previous: null,
|
|
141
|
+
};
|
|
142
|
+
_contextsByPath.set(key, tableContext);
|
|
143
|
+
}
|
|
144
|
+
const rowViews = [];
|
|
145
|
+
for (const rowValues of rows) {
|
|
146
|
+
const rowContext = _ensureRowContext(node, rows, rowValues, tableContext, path);
|
|
147
|
+
const rowView = rowContext.data;
|
|
148
|
+
rowViews.push(rowView);
|
|
149
|
+
_syncNodes(node.children, rowValues, rowView, rowContext, rowContext.path);
|
|
150
|
+
}
|
|
151
|
+
const tableView = tableContext.data;
|
|
152
|
+
tableView.splice(0, tableView.length, ...rowViews);
|
|
153
|
+
}
|
|
154
|
+
function _ensureRowContext(node, rows, rowValues, tableContext, tablePath) {
|
|
155
|
+
const rowId = requireRowId(rowIdentities, rowValues);
|
|
156
|
+
const rowPath = appendPath(tablePath, rowId);
|
|
157
|
+
const rowKey = pathKey(rowPath);
|
|
158
|
+
let rowContext = _contextsByPath.get(rowKey);
|
|
159
|
+
if (!rowContext) {
|
|
160
|
+
rowContext = {
|
|
161
|
+
kind: "row",
|
|
162
|
+
data: _createValuesView(node.children, rowValues, rowPath),
|
|
163
|
+
path: rowPath,
|
|
164
|
+
parent: tableContext,
|
|
165
|
+
id: rowId,
|
|
166
|
+
get index() {
|
|
167
|
+
return rows.indexOf(rowValues);
|
|
168
|
+
},
|
|
169
|
+
get previous() {
|
|
170
|
+
const index = rows.indexOf(rowValues);
|
|
171
|
+
if (index <= 0)
|
|
172
|
+
return null;
|
|
173
|
+
const previousRow = rows[index - 1];
|
|
174
|
+
const previousId = requireRowId(rowIdentities, previousRow);
|
|
175
|
+
return (_contextsByPath.get(pathKey(appendPath(tablePath, previousId))) ??
|
|
176
|
+
null);
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
_contextsByPath.set(rowKey, rowContext);
|
|
180
|
+
}
|
|
181
|
+
return rowContext;
|
|
182
|
+
}
|
|
183
|
+
function _sync() {
|
|
184
|
+
_calculatedInstancesByPath.clear();
|
|
185
|
+
_syncNodes(nodes, answers, _formView, _formContext, []);
|
|
186
|
+
}
|
|
187
|
+
_sync();
|
|
188
|
+
return {
|
|
189
|
+
scopesByPath: _scopesByPath,
|
|
190
|
+
objectValidators: _objectValidators,
|
|
191
|
+
beginEvaluation() {
|
|
192
|
+
_activeCalculationPass = createCalculationPass(_scopesByPath, _calculatedInstancesByPath);
|
|
193
|
+
return _activeCalculationPass;
|
|
194
|
+
},
|
|
195
|
+
endEvaluation() {
|
|
196
|
+
_activeCalculationPass = null;
|
|
197
|
+
},
|
|
198
|
+
sync: _sync,
|
|
199
|
+
removeBelow(path) {
|
|
200
|
+
removeMapEntriesBelow(_scopesByPath, path);
|
|
201
|
+
removeMapEntriesBelow(_contextsByPath, path);
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
function _createValuesView(currentNodes, values, parentPath) {
|
|
205
|
+
const view = Object.create(null);
|
|
206
|
+
for (const node of currentNodes) {
|
|
207
|
+
const path = appendPath(parentPath, node.id);
|
|
208
|
+
Object.defineProperty(view, node.id, {
|
|
209
|
+
enumerable: true,
|
|
210
|
+
get() {
|
|
211
|
+
if (node.type === "calculated" && _activeCalculationPass) {
|
|
212
|
+
return _activeCalculationPass.resolve(node, path);
|
|
213
|
+
}
|
|
214
|
+
if (node.type === "object" || node.type === "table") {
|
|
215
|
+
return _contextsByPath.get(pathKey(path))?.data;
|
|
216
|
+
}
|
|
217
|
+
return Object.hasOwn(values, node.id) ? values[node.id] : undefined;
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
// Compatible application data shares the same live read boundary as
|
|
222
|
+
// declared answers. Declared getters above remain authoritative, including
|
|
223
|
+
// calculated dependency resolution and structural container views.
|
|
224
|
+
for (const key of Object.keys(values)) {
|
|
225
|
+
if (Object.hasOwn(view, key))
|
|
226
|
+
continue;
|
|
227
|
+
Object.defineProperty(view, key, {
|
|
228
|
+
enumerable: true,
|
|
229
|
+
get() {
|
|
230
|
+
return values[key];
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
return view;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
// Keep a stable, validator-only scope while borrowing the current relevance
|
|
238
|
+
// index for the duration of each synchronous invocation.
|
|
239
|
+
function bindObjectValidator(validate, path, scope) {
|
|
240
|
+
let relevanceByPath;
|
|
241
|
+
const validatorScope = Object.freeze({
|
|
242
|
+
form: scope.form,
|
|
243
|
+
parent: scope.parent,
|
|
244
|
+
context: scope.context,
|
|
245
|
+
vars: scope.vars,
|
|
246
|
+
get value() {
|
|
247
|
+
return scope.value;
|
|
248
|
+
},
|
|
249
|
+
isRelevant(childId) {
|
|
250
|
+
if (!relevanceByPath) {
|
|
251
|
+
throw new Error("isRelevant is only available during object validation");
|
|
252
|
+
}
|
|
253
|
+
const relevant = typeof childId === "string"
|
|
254
|
+
? relevanceByPath.get(pathKey(appendPath(path, childId)))
|
|
255
|
+
: undefined;
|
|
256
|
+
if (relevant === undefined) {
|
|
257
|
+
throw new Error(`object ${pathKey(path)} has no direct child ${JSON.stringify(childId)}`);
|
|
258
|
+
}
|
|
259
|
+
return relevant;
|
|
260
|
+
},
|
|
261
|
+
});
|
|
262
|
+
return (evaluatedRelevance) => {
|
|
263
|
+
relevanceByPath = evaluatedRelevance;
|
|
264
|
+
try {
|
|
265
|
+
return validate(validatorScope);
|
|
266
|
+
}
|
|
267
|
+
finally {
|
|
268
|
+
relevanceByPath = undefined;
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
function removeMapEntriesBelow(map, prefix) {
|
|
273
|
+
for (const key of map.keys()) {
|
|
274
|
+
const path = JSON.parse(key);
|
|
275
|
+
if (path.length >= prefix.length &&
|
|
276
|
+
prefix.every((segment, index) => path[index] === segment)) {
|
|
277
|
+
map.delete(key);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
function evaluateCondition(condition, fallback, scope, name) {
|
|
282
|
+
if (condition === undefined)
|
|
283
|
+
return fallback;
|
|
284
|
+
if (typeof condition === "boolean")
|
|
285
|
+
return condition;
|
|
286
|
+
if (typeof condition !== "function") {
|
|
287
|
+
throw new TypeError(`${name} condition must be a boolean or function`);
|
|
288
|
+
}
|
|
289
|
+
const result = condition(scope);
|
|
290
|
+
if (typeof result !== "boolean") {
|
|
291
|
+
throw new TypeError(`${name} condition must return a boolean`);
|
|
292
|
+
}
|
|
293
|
+
return result;
|
|
294
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { TextField, SelectField, MultiSelectField, CalculatedField, ObjectField, TableField, CopyAction, FormValues, FormVars } from "./types.js";
|
|
2
|
+
/** A formula marker is recognized only in documented rule positions. */
|
|
3
|
+
export type Formula = `=${string}`;
|
|
4
|
+
export type Expression = Formula;
|
|
5
|
+
export interface ExpressionCopyAction extends Omit<CopyAction, "when"> {
|
|
6
|
+
when?: CopyAction["when"] | Formula;
|
|
7
|
+
}
|
|
8
|
+
export interface ExpressionInputChange {
|
|
9
|
+
copy: readonly ExpressionCopyAction[];
|
|
10
|
+
}
|
|
11
|
+
type EditableRules<T extends TextField | SelectField | MultiSelectField> = Omit<T, "required" | "relevant" | "readOnly" | "validate" | "onChange"> & {
|
|
12
|
+
required?: T["required"] | Formula;
|
|
13
|
+
relevant?: T["relevant"] | Formula;
|
|
14
|
+
readOnly?: T["readOnly"] | Formula;
|
|
15
|
+
validate?: T["validate"] | Formula;
|
|
16
|
+
onChange?: ExpressionInputChange;
|
|
17
|
+
};
|
|
18
|
+
export type ExpressionTextField = EditableRules<TextField>;
|
|
19
|
+
export type ExpressionSelectField = Omit<EditableRules<SelectField>, "options"> & {
|
|
20
|
+
options: SelectField["options"] | Formula;
|
|
21
|
+
};
|
|
22
|
+
export type ExpressionMultiSelectField = Omit<EditableRules<MultiSelectField>, "options"> & {
|
|
23
|
+
options: MultiSelectField["options"] | Formula;
|
|
24
|
+
};
|
|
25
|
+
export type ExpressionCalculatedField = Omit<CalculatedField, "calculate" | "relevant" | "validate"> & {
|
|
26
|
+
calculate: CalculatedField["calculate"] | Formula;
|
|
27
|
+
relevant?: CalculatedField["relevant"] | Formula;
|
|
28
|
+
validate?: CalculatedField["validate"] | Formula;
|
|
29
|
+
};
|
|
30
|
+
export type ExpressionObjectField = Omit<ObjectField, "relevant" | "validate" | "children"> & {
|
|
31
|
+
relevant?: ObjectField["relevant"] | Formula;
|
|
32
|
+
validate?: ObjectField["validate"] | Formula;
|
|
33
|
+
children: readonly ExpressionFormNode[];
|
|
34
|
+
};
|
|
35
|
+
export type ExpressionTableField = Omit<TableField, "relevant" | "validate" | "children"> & {
|
|
36
|
+
relevant?: TableField["relevant"] | Formula;
|
|
37
|
+
validate?: TableField["validate"] | Formula;
|
|
38
|
+
children: readonly ExpressionFormNode[];
|
|
39
|
+
};
|
|
40
|
+
export type ExpressionFormField = ExpressionTextField | ExpressionSelectField | ExpressionMultiSelectField | ExpressionCalculatedField;
|
|
41
|
+
export type ExpressionFormNode = ExpressionFormField | ExpressionObjectField | ExpressionTableField;
|
|
42
|
+
export interface CreateExpressionFormOptions {
|
|
43
|
+
schema: readonly ExpressionFormNode[];
|
|
44
|
+
data?: FormValues;
|
|
45
|
+
vars?: FormVars;
|
|
46
|
+
}
|
|
47
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type jsep from "jsep";
|
|
2
|
+
import type { Descriptor } from "./references.js";
|
|
3
|
+
import type { ConditionScope, InputScope } from "../types.js";
|
|
4
|
+
export declare function checkTree(expression: jsep.Expression | null | undefined, roots: Map<string, Descriptor>): Descriptor;
|
|
5
|
+
export declare function interpret(expression: jsep.Expression, scope: Readonly<ConditionScope | InputScope>, roots: Map<string, Descriptor>): {
|
|
6
|
+
value: unknown;
|
|
7
|
+
descriptor: Descriptor;
|
|
8
|
+
};
|