@revisium/formula 0.8.0 → 0.10.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/dist/__tests__/array-context.spec.d.ts +2 -0
- package/dist/__tests__/array-context.spec.d.ts.map +1 -0
- package/dist/__tests__/dependency-graph.spec.d.ts +2 -0
- package/dist/__tests__/dependency-graph.spec.d.ts.map +1 -0
- package/dist/__tests__/evaluate-complex.spec.d.ts +2 -0
- package/dist/__tests__/evaluate-complex.spec.d.ts.map +1 -0
- package/dist/__tests__/parse-formula.spec.d.ts +2 -0
- package/dist/__tests__/parse-formula.spec.d.ts.map +1 -0
- package/dist/__tests__/parser.spec.d.ts +2 -0
- package/dist/__tests__/parser.spec.d.ts.map +1 -0
- package/dist/__tests__/replace-dependencies.spec.d.ts +2 -0
- package/dist/__tests__/replace-dependencies.spec.d.ts.map +1 -0
- package/dist/__tests__/serialize-ast.spec.d.ts +2 -0
- package/dist/__tests__/serialize-ast.spec.d.ts.map +1 -0
- package/dist/__tests__/validate-syntax.spec.d.ts +2 -0
- package/dist/__tests__/validate-syntax.spec.d.ts.map +1 -0
- package/dist/dependency-graph.d.ts +17 -0
- package/dist/dependency-graph.d.ts.map +1 -0
- package/dist/editor/index.cjs +4 -19
- package/dist/editor/index.d.ts +8 -1
- package/dist/editor/index.d.ts.map +1 -0
- package/dist/editor/index.mjs +3 -0
- package/dist/formula-spec.cjs +831 -535
- package/dist/formula-spec.cjs.map +1 -1
- package/dist/formula-spec.d.ts +10 -5
- package/dist/formula-spec.d.ts.map +1 -0
- package/dist/formula-spec.mjs +1062 -0
- package/dist/formula-spec.mjs.map +1 -0
- package/dist/index.cjs +121 -131
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +10 -25
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +130 -0
- package/dist/index.mjs.map +1 -0
- package/dist/ohm/core/parser.d.ts +30 -0
- package/dist/ohm/core/parser.d.ts.map +1 -0
- package/dist/ohm/core/replace-dependencies.d.ts +3 -0
- package/dist/ohm/core/replace-dependencies.d.ts.map +1 -0
- package/dist/ohm/core/serialize-ast.d.ts +3 -0
- package/dist/ohm/core/serialize-ast.d.ts.map +1 -0
- package/dist/ohm/core/types.d.ts +78 -0
- package/dist/ohm/core/types.d.ts.map +1 -0
- package/dist/ohm/grammar/index.d.ts +3 -0
- package/dist/ohm/grammar/index.d.ts.map +1 -0
- package/dist/ohm/index.d.ts +8 -0
- package/dist/ohm/index.d.ts.map +1 -0
- package/dist/ohm/semantics/index.d.ts +2 -0
- package/dist/ohm/semantics/index.d.ts.map +1 -0
- package/dist/parse-formula.d.ts +9 -0
- package/dist/parse-formula.d.ts.map +1 -0
- package/dist/types.d.ts +52 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/validate-syntax-Dttxs0ku.mjs +1426 -0
- package/dist/validate-syntax-Dttxs0ku.mjs.map +1 -0
- package/dist/validate-syntax-q3bM-DaM.cjs +1507 -0
- package/dist/validate-syntax-q3bM-DaM.cjs.map +1 -0
- package/dist/validate-syntax.d.ts +9 -0
- package/dist/validate-syntax.d.ts.map +1 -0
- package/package.json +12 -11
- package/dist/chunk-4FIOGFOL.cjs +0 -1512
- package/dist/chunk-4FIOGFOL.cjs.map +0 -1
- package/dist/chunk-RMAHNB4D.js +0 -1482
- package/dist/chunk-RMAHNB4D.js.map +0 -1
- package/dist/editor/index.cjs.map +0 -1
- package/dist/editor/index.d.cts +0 -1
- package/dist/editor/index.js +0 -3
- package/dist/editor/index.js.map +0 -1
- package/dist/formula-spec.d.cts +0 -68
- package/dist/formula-spec.js +0 -765
- package/dist/formula-spec.js.map +0 -1
- package/dist/index-CPsOPCQ6.d.cts +0 -166
- package/dist/index-CPsOPCQ6.d.ts +0 -166
- package/dist/index.d.cts +0 -25
- package/dist/index.js +0 -111
- package/dist/index.js.map +0 -1
|
@@ -0,0 +1,1062 @@
|
|
|
1
|
+
//#region src/formula-spec.ts
|
|
2
|
+
const formulaSpec = {
|
|
3
|
+
version: "1.2",
|
|
4
|
+
description: "Formula expressions for computed fields. Formulas reference other fields and calculate values automatically.",
|
|
5
|
+
syntax: {
|
|
6
|
+
fieldReferences: [
|
|
7
|
+
"Simple field: fieldName (e.g., price, quantity)",
|
|
8
|
+
"Nested path: object.property (e.g., stats.damage)",
|
|
9
|
+
"Array index: array[0] or array[-1] for last element",
|
|
10
|
+
"Bracket notation: [\"field-name\"] for field names containing hyphens",
|
|
11
|
+
" - Required when field name contains hyphen (-)",
|
|
12
|
+
" - Without brackets: field-name is parsed as \"field minus name\"",
|
|
13
|
+
" - With brackets: [\"field-name\"] references the field correctly",
|
|
14
|
+
"Combined: items[0].price, user.addresses[-1].city, obj[\"field-name\"].value"
|
|
15
|
+
],
|
|
16
|
+
arithmeticOperators: [
|
|
17
|
+
{
|
|
18
|
+
operator: "+",
|
|
19
|
+
description: "Addition or string concatenation"
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
operator: "-",
|
|
23
|
+
description: "Subtraction"
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
operator: "*",
|
|
27
|
+
description: "Multiplication"
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
operator: "/",
|
|
31
|
+
description: "Division"
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
operator: "%",
|
|
35
|
+
description: "Modulo (remainder)"
|
|
36
|
+
}
|
|
37
|
+
],
|
|
38
|
+
comparisonOperators: [
|
|
39
|
+
{
|
|
40
|
+
operator: "==",
|
|
41
|
+
description: "Equal"
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
operator: "!=",
|
|
45
|
+
description: "Not equal"
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
operator: ">",
|
|
49
|
+
description: "Greater than"
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
operator: "<",
|
|
53
|
+
description: "Less than"
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
operator: ">=",
|
|
57
|
+
description: "Greater or equal"
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
operator: "<=",
|
|
61
|
+
description: "Less or equal"
|
|
62
|
+
}
|
|
63
|
+
],
|
|
64
|
+
logicalOperators: [
|
|
65
|
+
{
|
|
66
|
+
operator: "&&",
|
|
67
|
+
description: "Logical AND"
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
operator: "||",
|
|
71
|
+
description: "Logical OR"
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
operator: "!",
|
|
75
|
+
description: "Logical NOT"
|
|
76
|
+
}
|
|
77
|
+
],
|
|
78
|
+
other: ["Parentheses: (a + b) * c", "Unary minus: -value, a + -b"]
|
|
79
|
+
},
|
|
80
|
+
functions: {
|
|
81
|
+
string: [
|
|
82
|
+
{
|
|
83
|
+
name: "concat",
|
|
84
|
+
description: "Concatenate multiple values into a single string",
|
|
85
|
+
signature: "concat(value1, value2, ...)",
|
|
86
|
+
returnType: "string",
|
|
87
|
+
examples: ["concat(firstName, \" \", lastName) // \"John Doe\"", "concat(\"Price: \", price, \" USD\") // \"Price: 100 USD\""]
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
name: "upper",
|
|
91
|
+
description: "Convert string to uppercase",
|
|
92
|
+
signature: "upper(text)",
|
|
93
|
+
returnType: "string",
|
|
94
|
+
examples: ["upper(name) // \"HELLO\""]
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
name: "lower",
|
|
98
|
+
description: "Convert string to lowercase",
|
|
99
|
+
signature: "lower(text)",
|
|
100
|
+
returnType: "string",
|
|
101
|
+
examples: ["lower(name) // \"hello\""]
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
name: "trim",
|
|
105
|
+
description: "Remove whitespace from both ends of a string",
|
|
106
|
+
signature: "trim(text)",
|
|
107
|
+
returnType: "string",
|
|
108
|
+
examples: ["trim(name) // \"hello\" from \" hello \""]
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
name: "left",
|
|
112
|
+
description: "Extract characters from the beginning of a string",
|
|
113
|
+
signature: "left(text, count)",
|
|
114
|
+
returnType: "string",
|
|
115
|
+
examples: ["left(name, 3) // \"hel\" from \"hello\""]
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
name: "right",
|
|
119
|
+
description: "Extract characters from the end of a string",
|
|
120
|
+
signature: "right(text, count)",
|
|
121
|
+
returnType: "string",
|
|
122
|
+
examples: ["right(name, 3) // \"llo\" from \"hello\""]
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
name: "replace",
|
|
126
|
+
description: "Replace first occurrence of a substring",
|
|
127
|
+
signature: "replace(text, search, replacement)",
|
|
128
|
+
returnType: "string",
|
|
129
|
+
examples: ["replace(name, \"o\", \"0\") // \"hell0\" from \"hello\""]
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
name: "join",
|
|
133
|
+
description: "Join array elements into a string",
|
|
134
|
+
signature: "join(array, separator?)",
|
|
135
|
+
returnType: "string",
|
|
136
|
+
examples: ["join(tags) // \"a,b,c\"", "join(tags, \" | \") // \"a | b | c\""]
|
|
137
|
+
}
|
|
138
|
+
],
|
|
139
|
+
numeric: [
|
|
140
|
+
{
|
|
141
|
+
name: "round",
|
|
142
|
+
description: "Round a number to specified decimal places",
|
|
143
|
+
signature: "round(number, decimals?)",
|
|
144
|
+
returnType: "number",
|
|
145
|
+
examples: ["round(3.14159, 2) // 3.14", "round(3.5) // 4"]
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
name: "floor",
|
|
149
|
+
description: "Round down to the nearest integer",
|
|
150
|
+
signature: "floor(number)",
|
|
151
|
+
returnType: "number",
|
|
152
|
+
examples: ["floor(3.7) // 3"]
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
name: "ceil",
|
|
156
|
+
description: "Round up to the nearest integer",
|
|
157
|
+
signature: "ceil(number)",
|
|
158
|
+
returnType: "number",
|
|
159
|
+
examples: ["ceil(3.2) // 4"]
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
name: "abs",
|
|
163
|
+
description: "Get the absolute value",
|
|
164
|
+
signature: "abs(number)",
|
|
165
|
+
returnType: "number",
|
|
166
|
+
examples: ["abs(-5) // 5"]
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
name: "sqrt",
|
|
170
|
+
description: "Calculate the square root",
|
|
171
|
+
signature: "sqrt(number)",
|
|
172
|
+
returnType: "number",
|
|
173
|
+
examples: ["sqrt(16) // 4"]
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
name: "pow",
|
|
177
|
+
description: "Raise a number to a power",
|
|
178
|
+
signature: "pow(base, exponent)",
|
|
179
|
+
returnType: "number",
|
|
180
|
+
examples: ["pow(2, 3) // 8"]
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
name: "min",
|
|
184
|
+
description: "Get the minimum of multiple values",
|
|
185
|
+
signature: "min(value1, value2, ...)",
|
|
186
|
+
returnType: "number",
|
|
187
|
+
examples: ["min(a, b, c) // smallest value"]
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
name: "max",
|
|
191
|
+
description: "Get the maximum of multiple values",
|
|
192
|
+
signature: "max(value1, value2, ...)",
|
|
193
|
+
returnType: "number",
|
|
194
|
+
examples: ["max(a, b, c) // largest value"]
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
name: "log",
|
|
198
|
+
description: "Calculate the natural logarithm",
|
|
199
|
+
signature: "log(number)",
|
|
200
|
+
returnType: "number",
|
|
201
|
+
examples: ["log(10) // 2.302..."]
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
name: "log10",
|
|
205
|
+
description: "Calculate the base-10 logarithm",
|
|
206
|
+
signature: "log10(number)",
|
|
207
|
+
returnType: "number",
|
|
208
|
+
examples: ["log10(100) // 2"]
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
name: "exp",
|
|
212
|
+
description: "Calculate e raised to a power",
|
|
213
|
+
signature: "exp(number)",
|
|
214
|
+
returnType: "number",
|
|
215
|
+
examples: ["exp(1) // 2.718..."]
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
name: "sign",
|
|
219
|
+
description: "Get the sign of a number (-1, 0, or 1)",
|
|
220
|
+
signature: "sign(number)",
|
|
221
|
+
returnType: "number",
|
|
222
|
+
examples: [
|
|
223
|
+
"sign(-5) // -1",
|
|
224
|
+
"sign(0) // 0",
|
|
225
|
+
"sign(5) // 1"
|
|
226
|
+
]
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
name: "length",
|
|
230
|
+
description: "Get the length of a string or array",
|
|
231
|
+
signature: "length(value)",
|
|
232
|
+
returnType: "number",
|
|
233
|
+
examples: ["length(name) // 5 from \"hello\"", "length(items) // 3"]
|
|
234
|
+
}
|
|
235
|
+
],
|
|
236
|
+
boolean: [
|
|
237
|
+
{
|
|
238
|
+
name: "and",
|
|
239
|
+
description: "Logical AND of two values",
|
|
240
|
+
signature: "and(a, b)",
|
|
241
|
+
returnType: "boolean",
|
|
242
|
+
examples: ["and(isActive, hasPermission) // true if both true"]
|
|
243
|
+
},
|
|
244
|
+
{
|
|
245
|
+
name: "or",
|
|
246
|
+
description: "Logical OR of two values",
|
|
247
|
+
signature: "or(a, b)",
|
|
248
|
+
returnType: "boolean",
|
|
249
|
+
examples: ["or(isAdmin, isOwner) // true if either true"]
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
name: "not",
|
|
253
|
+
description: "Logical NOT of a value",
|
|
254
|
+
signature: "not(value)",
|
|
255
|
+
returnType: "boolean",
|
|
256
|
+
examples: ["not(isDeleted) // true if false"]
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
name: "contains",
|
|
260
|
+
description: "Check if a string contains a substring",
|
|
261
|
+
signature: "contains(text, search)",
|
|
262
|
+
returnType: "boolean",
|
|
263
|
+
examples: ["contains(name, \"ell\") // true for \"hello\""]
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
name: "startswith",
|
|
267
|
+
description: "Check if a string starts with a prefix",
|
|
268
|
+
signature: "startswith(text, prefix)",
|
|
269
|
+
returnType: "boolean",
|
|
270
|
+
examples: ["startswith(name, \"hel\") // true for \"hello\""]
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
name: "endswith",
|
|
274
|
+
description: "Check if a string ends with a suffix",
|
|
275
|
+
signature: "endswith(text, suffix)",
|
|
276
|
+
returnType: "boolean",
|
|
277
|
+
examples: ["endswith(name, \"llo\") // true for \"hello\""]
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
name: "isnull",
|
|
281
|
+
description: "Check if a value is null or undefined",
|
|
282
|
+
signature: "isnull(value)",
|
|
283
|
+
returnType: "boolean",
|
|
284
|
+
examples: ["isnull(optionalField) // true if null/undefined"]
|
|
285
|
+
},
|
|
286
|
+
{
|
|
287
|
+
name: "includes",
|
|
288
|
+
description: "Check if an array contains a value",
|
|
289
|
+
signature: "includes(array, value)",
|
|
290
|
+
returnType: "boolean",
|
|
291
|
+
examples: ["includes(tags, \"featured\") // true if array contains value"]
|
|
292
|
+
}
|
|
293
|
+
],
|
|
294
|
+
array: [
|
|
295
|
+
{
|
|
296
|
+
name: "sum",
|
|
297
|
+
description: "Calculate the sum of array elements. Supports wildcard property access to sum nested values.",
|
|
298
|
+
signature: "sum(array)",
|
|
299
|
+
returnType: "number",
|
|
300
|
+
examples: [
|
|
301
|
+
"sum(prices) // total of all prices",
|
|
302
|
+
"sum(items[*].price) // sum prices from array of objects",
|
|
303
|
+
"sum(orders[*].items[*].amount) // sum nested arrays"
|
|
304
|
+
]
|
|
305
|
+
},
|
|
306
|
+
{
|
|
307
|
+
name: "avg",
|
|
308
|
+
description: "Calculate the average of array elements. Supports wildcard property access.",
|
|
309
|
+
signature: "avg(array)",
|
|
310
|
+
returnType: "number",
|
|
311
|
+
examples: ["avg(scores) // average score", "avg(items[*].rating) // average rating from array of objects"]
|
|
312
|
+
},
|
|
313
|
+
{
|
|
314
|
+
name: "count",
|
|
315
|
+
description: "Get the number of elements in an array",
|
|
316
|
+
signature: "count(array)",
|
|
317
|
+
returnType: "number",
|
|
318
|
+
examples: ["count(items) // number of items"]
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
name: "first",
|
|
322
|
+
description: "Get the first element of an array",
|
|
323
|
+
signature: "first(array)",
|
|
324
|
+
returnType: "any",
|
|
325
|
+
examples: ["first(items) // first item"]
|
|
326
|
+
},
|
|
327
|
+
{
|
|
328
|
+
name: "last",
|
|
329
|
+
description: "Get the last element of an array",
|
|
330
|
+
signature: "last(array)",
|
|
331
|
+
returnType: "any",
|
|
332
|
+
examples: ["last(items) // last item"]
|
|
333
|
+
}
|
|
334
|
+
],
|
|
335
|
+
conversion: [
|
|
336
|
+
{
|
|
337
|
+
name: "tostring",
|
|
338
|
+
description: "Convert a value to string",
|
|
339
|
+
signature: "tostring(value)",
|
|
340
|
+
returnType: "string",
|
|
341
|
+
examples: ["tostring(42) // \"42\""]
|
|
342
|
+
},
|
|
343
|
+
{
|
|
344
|
+
name: "tonumber",
|
|
345
|
+
description: "Convert a value to number",
|
|
346
|
+
signature: "tonumber(value)",
|
|
347
|
+
returnType: "number",
|
|
348
|
+
examples: ["tonumber(\"42\") // 42"]
|
|
349
|
+
},
|
|
350
|
+
{
|
|
351
|
+
name: "toboolean",
|
|
352
|
+
description: "Convert a value to boolean",
|
|
353
|
+
signature: "toboolean(value)",
|
|
354
|
+
returnType: "boolean",
|
|
355
|
+
examples: ["toboolean(1) // true", "toboolean(0) // false"]
|
|
356
|
+
}
|
|
357
|
+
],
|
|
358
|
+
conditional: [{
|
|
359
|
+
name: "if",
|
|
360
|
+
description: "Return one of two values based on a condition",
|
|
361
|
+
signature: "if(condition, valueIfTrue, valueIfFalse)",
|
|
362
|
+
returnType: "any",
|
|
363
|
+
examples: ["if(stock > 0, \"Available\", \"Out of Stock\")", "if(price > 100, price * 0.9, price)"]
|
|
364
|
+
}, {
|
|
365
|
+
name: "coalesce",
|
|
366
|
+
description: "Return the first non-null value",
|
|
367
|
+
signature: "coalesce(value1, value2, ...)",
|
|
368
|
+
returnType: "any",
|
|
369
|
+
examples: ["coalesce(nickname, name, \"Anonymous\")"]
|
|
370
|
+
}]
|
|
371
|
+
},
|
|
372
|
+
features: [
|
|
373
|
+
{
|
|
374
|
+
name: "simple_refs",
|
|
375
|
+
description: "Reference top-level fields by name",
|
|
376
|
+
minVersion: "1.0",
|
|
377
|
+
examples: [
|
|
378
|
+
"price",
|
|
379
|
+
"quantity",
|
|
380
|
+
"baseDamage"
|
|
381
|
+
],
|
|
382
|
+
dependenciesExtracted: [
|
|
383
|
+
"[\"price\"]",
|
|
384
|
+
"[\"quantity\"]",
|
|
385
|
+
"[\"baseDamage\"]"
|
|
386
|
+
]
|
|
387
|
+
},
|
|
388
|
+
{
|
|
389
|
+
name: "arithmetic",
|
|
390
|
+
description: "Basic math operations (+, -, *, /)",
|
|
391
|
+
minVersion: "1.0",
|
|
392
|
+
examples: [
|
|
393
|
+
"price * 1.1",
|
|
394
|
+
"a + b - c",
|
|
395
|
+
"quantity * price"
|
|
396
|
+
]
|
|
397
|
+
},
|
|
398
|
+
{
|
|
399
|
+
name: "comparison",
|
|
400
|
+
description: "Compare values (>, <, >=, <=, ==, !=)",
|
|
401
|
+
minVersion: "1.0",
|
|
402
|
+
examples: [
|
|
403
|
+
"price > 100",
|
|
404
|
+
"x == 10",
|
|
405
|
+
"quantity >= 5"
|
|
406
|
+
]
|
|
407
|
+
},
|
|
408
|
+
{
|
|
409
|
+
name: "nested_path",
|
|
410
|
+
description: "Access nested object properties using dot notation",
|
|
411
|
+
minVersion: "1.1",
|
|
412
|
+
examples: [
|
|
413
|
+
"stats.damage",
|
|
414
|
+
"user.profile.name",
|
|
415
|
+
"item.metadata.category"
|
|
416
|
+
],
|
|
417
|
+
dependenciesExtracted: ["[\"stats.damage\"]"]
|
|
418
|
+
},
|
|
419
|
+
{
|
|
420
|
+
name: "array_index",
|
|
421
|
+
description: "Access array elements by numeric index. Negative indices access from the end",
|
|
422
|
+
minVersion: "1.1",
|
|
423
|
+
examples: [
|
|
424
|
+
"items[0].price",
|
|
425
|
+
"inventory[1].quantity",
|
|
426
|
+
"items[-1].name // last element",
|
|
427
|
+
"items[-2].price // second to last"
|
|
428
|
+
],
|
|
429
|
+
dependenciesExtracted: ["[\"items[0].price\"]", "[\"items[-1].name\"]"]
|
|
430
|
+
},
|
|
431
|
+
{
|
|
432
|
+
name: "array_wildcard_property",
|
|
433
|
+
description: "Access properties across all array elements using [*] wildcard. Property access after wildcard maps over all elements. Multiple wildcards flatten nested arrays.",
|
|
434
|
+
minVersion: "1.1",
|
|
435
|
+
examples: [
|
|
436
|
+
"items[*].price // [10, 20, 30] - map property",
|
|
437
|
+
"sum(items[*].price) // 60 - sum mapped values",
|
|
438
|
+
"avg(items[*].rating) // average of all ratings",
|
|
439
|
+
"values[*].nested.value // deeply nested property access",
|
|
440
|
+
"orders[*].items // [[1,2], [3,4]] - array of arrays",
|
|
441
|
+
"orders[*].items[*] // [1,2,3,4] - flattened",
|
|
442
|
+
"sum(orders[*].items[*].amount) // sum all nested amounts"
|
|
443
|
+
],
|
|
444
|
+
dependenciesExtracted: ["[\"items[*].price\"]", "[\"orders[*].items[*].amount\"]"]
|
|
445
|
+
},
|
|
446
|
+
{
|
|
447
|
+
name: "root_path",
|
|
448
|
+
description: "Absolute path reference starting with /. Always resolves from root data, even inside array item formulas",
|
|
449
|
+
minVersion: "1.1",
|
|
450
|
+
examples: [
|
|
451
|
+
"/taxRate",
|
|
452
|
+
"/config.tax",
|
|
453
|
+
"price * (1 + /taxRate)",
|
|
454
|
+
"price * /config.multiplier"
|
|
455
|
+
],
|
|
456
|
+
dependenciesExtracted: ["[\"/taxRate\"]", "[\"/config.tax\"]"]
|
|
457
|
+
},
|
|
458
|
+
{
|
|
459
|
+
name: "relative_path",
|
|
460
|
+
description: "Relative path reference starting with ../. Each ../ goes up one level in the path hierarchy. Works with nested objects, arrays, and combinations. Supports accessing nested properties after the relative prefix (e.g., ../config.value)",
|
|
461
|
+
minVersion: "1.1",
|
|
462
|
+
examples: [
|
|
463
|
+
"../discount",
|
|
464
|
+
"../../rootRate",
|
|
465
|
+
"../config.multiplier",
|
|
466
|
+
"price * (1 - ../discount)",
|
|
467
|
+
"price * ../../globalRate",
|
|
468
|
+
"price * ../settings.tax.rate"
|
|
469
|
+
],
|
|
470
|
+
dependenciesExtracted: [
|
|
471
|
+
"[\"../discount\"]",
|
|
472
|
+
"[\"../../rootRate\"]",
|
|
473
|
+
"[\"../config.multiplier\"]"
|
|
474
|
+
]
|
|
475
|
+
},
|
|
476
|
+
{
|
|
477
|
+
name: "function_named_fields",
|
|
478
|
+
description: "Fields can have the same name as built-in functions (max, min, sum, etc.). Built-in functions are always checked first when a function call is made",
|
|
479
|
+
minVersion: "1.0",
|
|
480
|
+
examples: [
|
|
481
|
+
"max(max, 0)",
|
|
482
|
+
"min(min, 100)",
|
|
483
|
+
"max(max - field.min, 0)",
|
|
484
|
+
"round(round * 2)"
|
|
485
|
+
]
|
|
486
|
+
},
|
|
487
|
+
{
|
|
488
|
+
name: "bracket_notation",
|
|
489
|
+
description: "Access fields containing hyphens using bracket notation with quotes. Required because \"field-name\" would be parsed as \"field minus name\" (subtraction). Works like JavaScript object[\"key\"] syntax.",
|
|
490
|
+
minVersion: "1.1",
|
|
491
|
+
examples: [
|
|
492
|
+
"[\"field-name\"] // Without brackets: field - name (subtraction!)",
|
|
493
|
+
"['field-name'] // Single quotes also work",
|
|
494
|
+
"[\"field-one\"][\"field-two\"]",
|
|
495
|
+
"obj[\"field-name\"].value",
|
|
496
|
+
"[\"items-list\"][0][\"val\"]",
|
|
497
|
+
"[\"price-new\"] * 2"
|
|
498
|
+
],
|
|
499
|
+
dependenciesExtracted: ["[\"field-name\"]", "['field-name']"]
|
|
500
|
+
},
|
|
501
|
+
{
|
|
502
|
+
name: "context_token",
|
|
503
|
+
description: "Array context tokens provide information about current position and neighboring elements when evaluating formulas inside arrays. # prefix for scalar metadata (number/boolean), @ prefix for object references.",
|
|
504
|
+
minVersion: "1.2",
|
|
505
|
+
examples: [
|
|
506
|
+
"#index // Current array index (0-based)",
|
|
507
|
+
"#length // Array length",
|
|
508
|
+
"#first // true if first element",
|
|
509
|
+
"#last // true if last element",
|
|
510
|
+
"@prev // Previous element (null if first)",
|
|
511
|
+
"@next // Next element (null if last)",
|
|
512
|
+
"#parent.index // Index in parent array (nested arrays)",
|
|
513
|
+
"#parent.length // Length of parent array",
|
|
514
|
+
"#root.index // Index in topmost array",
|
|
515
|
+
"@root.prev // Previous element in topmost array",
|
|
516
|
+
"if(#first, value, @prev.total + value) // Running total",
|
|
517
|
+
"concat(#parent.index + 1, \".\", #index + 1) // \"1.1\", \"1.2\" numbering"
|
|
518
|
+
]
|
|
519
|
+
}
|
|
520
|
+
],
|
|
521
|
+
versionDetection: [
|
|
522
|
+
{
|
|
523
|
+
feature: "Simple refs, arithmetic, comparisons",
|
|
524
|
+
minVersion: "1.0"
|
|
525
|
+
},
|
|
526
|
+
{
|
|
527
|
+
feature: "Function-named fields (max(max, 0))",
|
|
528
|
+
minVersion: "1.0"
|
|
529
|
+
},
|
|
530
|
+
{
|
|
531
|
+
feature: "Nested paths (a.b)",
|
|
532
|
+
minVersion: "1.1"
|
|
533
|
+
},
|
|
534
|
+
{
|
|
535
|
+
feature: "Array index ([0], [-1])",
|
|
536
|
+
minVersion: "1.1"
|
|
537
|
+
},
|
|
538
|
+
{
|
|
539
|
+
feature: "Array wildcard property (items[*].price)",
|
|
540
|
+
minVersion: "1.1"
|
|
541
|
+
},
|
|
542
|
+
{
|
|
543
|
+
feature: "Absolute paths (/field)",
|
|
544
|
+
minVersion: "1.1"
|
|
545
|
+
},
|
|
546
|
+
{
|
|
547
|
+
feature: "Relative paths (../field)",
|
|
548
|
+
minVersion: "1.1"
|
|
549
|
+
},
|
|
550
|
+
{
|
|
551
|
+
feature: "Bracket notation ([\"field-name\"])",
|
|
552
|
+
minVersion: "1.1"
|
|
553
|
+
},
|
|
554
|
+
{
|
|
555
|
+
feature: "Context tokens (#index, @prev)",
|
|
556
|
+
minVersion: "1.2"
|
|
557
|
+
}
|
|
558
|
+
],
|
|
559
|
+
parseResult: {
|
|
560
|
+
description: "The parser automatically detects the minimum required version",
|
|
561
|
+
interface: `interface ParseResult {
|
|
562
|
+
ast: ASTNode; // Abstract syntax tree
|
|
563
|
+
dependencies: string[]; // List of field dependencies
|
|
564
|
+
features: string[]; // List of detected features
|
|
565
|
+
minVersion: string; // Minimum required version ("1.0" or "1.1")
|
|
566
|
+
}`
|
|
567
|
+
},
|
|
568
|
+
astUtilities: [{
|
|
569
|
+
name: "serializeAst",
|
|
570
|
+
description: "Convert an AST back to a formula string. Useful for debugging or displaying parsed formulas.",
|
|
571
|
+
signature: "serializeAst(ast: ASTNode): string",
|
|
572
|
+
code: `import { parseFormula, serializeAst } from '@revisium/formula';
|
|
573
|
+
|
|
574
|
+
const { ast } = parseFormula('price * (1 + taxRate)');
|
|
575
|
+
serializeAst(ast)
|
|
576
|
+
// "price * (1 + taxRate)"
|
|
577
|
+
|
|
578
|
+
// After modifying AST nodes, serialize back to string
|
|
579
|
+
const { ast: ast2 } = parseFormula('a + b');
|
|
580
|
+
serializeAst(ast2)
|
|
581
|
+
// "a + b"`
|
|
582
|
+
}, {
|
|
583
|
+
name: "replaceDependencies",
|
|
584
|
+
description: "Replace field references in an AST with new names. Useful for renaming fields or migrating formulas.",
|
|
585
|
+
signature: "replaceDependencies(ast: ASTNode, replacements: Record<string, string>): ASTNode",
|
|
586
|
+
code: `import { parseFormula, replaceDependencies, serializeAst } from '@revisium/formula';
|
|
587
|
+
|
|
588
|
+
// Rename a field in a formula
|
|
589
|
+
const { ast } = parseFormula('oldPrice * quantity');
|
|
590
|
+
const newAst = replaceDependencies(ast, { oldPrice: 'price' });
|
|
591
|
+
serializeAst(newAst)
|
|
592
|
+
// "price * quantity"
|
|
593
|
+
|
|
594
|
+
// Rename multiple fields
|
|
595
|
+
const { ast: ast2 } = parseFormula('a + b * c');
|
|
596
|
+
const newAst2 = replaceDependencies(ast2, { a: 'x', b: 'y', c: 'z' });
|
|
597
|
+
serializeAst(newAst2)
|
|
598
|
+
// "x + y * z"
|
|
599
|
+
|
|
600
|
+
// Works with nested paths
|
|
601
|
+
const { ast: ast3 } = parseFormula('stats.damage * multiplier');
|
|
602
|
+
const newAst3 = replaceDependencies(ast3, { 'stats.damage': 'stats.power' });
|
|
603
|
+
serializeAst(newAst3)
|
|
604
|
+
// "stats.power * multiplier"`
|
|
605
|
+
}],
|
|
606
|
+
examples: [
|
|
607
|
+
{
|
|
608
|
+
expression: "price * quantity",
|
|
609
|
+
description: "Calculate total from price and quantity",
|
|
610
|
+
result: "number"
|
|
611
|
+
},
|
|
612
|
+
{
|
|
613
|
+
expression: "firstName + \" \" + lastName",
|
|
614
|
+
description: "Concatenate strings with space",
|
|
615
|
+
result: "string"
|
|
616
|
+
},
|
|
617
|
+
{
|
|
618
|
+
expression: "quantity > 0",
|
|
619
|
+
description: "Check if in stock",
|
|
620
|
+
result: "boolean"
|
|
621
|
+
},
|
|
622
|
+
{
|
|
623
|
+
expression: "if(stock > 0, \"Available\", \"Out of Stock\")",
|
|
624
|
+
description: "Conditional text based on stock",
|
|
625
|
+
result: "string"
|
|
626
|
+
},
|
|
627
|
+
{
|
|
628
|
+
expression: "price * (1 + taxRate)",
|
|
629
|
+
description: "Price with tax",
|
|
630
|
+
result: "number"
|
|
631
|
+
},
|
|
632
|
+
{
|
|
633
|
+
expression: "items[0].price + items[1].price",
|
|
634
|
+
description: "Sum first two item prices (v1.1)",
|
|
635
|
+
result: "number"
|
|
636
|
+
}
|
|
637
|
+
],
|
|
638
|
+
apiExamples: [
|
|
639
|
+
{
|
|
640
|
+
name: "Simple Expression (v1.0)",
|
|
641
|
+
description: "Parse a basic arithmetic expression",
|
|
642
|
+
code: `parseExpression('price * 1.1')
|
|
643
|
+
// {
|
|
644
|
+
// minVersion: "1.0",
|
|
645
|
+
// features: [],
|
|
646
|
+
// dependencies: ["price"]
|
|
647
|
+
// }`
|
|
648
|
+
},
|
|
649
|
+
{
|
|
650
|
+
name: "Nested Path (v1.1)",
|
|
651
|
+
description: "Parse expression with nested object access",
|
|
652
|
+
code: `parseExpression('stats.damage * multiplier')
|
|
653
|
+
// {
|
|
654
|
+
// minVersion: "1.1",
|
|
655
|
+
// features: ["nested_path"],
|
|
656
|
+
// dependencies: ["stats.damage", "multiplier"]
|
|
657
|
+
// }`
|
|
658
|
+
},
|
|
659
|
+
{
|
|
660
|
+
name: "Array Access (v1.1)",
|
|
661
|
+
description: "Parse expression with array index access",
|
|
662
|
+
code: `parseExpression('items[0].price + items[1].price')
|
|
663
|
+
// {
|
|
664
|
+
// minVersion: "1.1",
|
|
665
|
+
// features: ["array_index", "nested_path"],
|
|
666
|
+
// dependencies: ["items[0].price", "items[1].price"]
|
|
667
|
+
// }`
|
|
668
|
+
},
|
|
669
|
+
{
|
|
670
|
+
name: "Evaluate expressions",
|
|
671
|
+
description: "Execute formulas with context data",
|
|
672
|
+
code: `evaluate('price * 1.1', { price: 100 })
|
|
673
|
+
// 110
|
|
674
|
+
|
|
675
|
+
evaluate('stats.damage', { stats: { damage: 50 } })
|
|
676
|
+
// 50
|
|
677
|
+
|
|
678
|
+
evaluate('items[0].price', { items: [{ price: 10 }] })
|
|
679
|
+
// 10
|
|
680
|
+
|
|
681
|
+
evaluate('price > 100', { price: 150 })
|
|
682
|
+
// true
|
|
683
|
+
|
|
684
|
+
evaluate('a + b * c', { a: 1, b: 2, c: 3 })
|
|
685
|
+
// 7`
|
|
686
|
+
},
|
|
687
|
+
{
|
|
688
|
+
name: "Function-named fields",
|
|
689
|
+
description: "Fields can have the same name as built-in functions",
|
|
690
|
+
code: `// Built-in functions take precedence in function calls
|
|
691
|
+
evaluate('max(max, 0)', { max: 10 })
|
|
692
|
+
// 10 (max() function, then max field)
|
|
693
|
+
|
|
694
|
+
evaluate('max(max - field.min, 0)', { max: 100, field: { min: 20 } })
|
|
695
|
+
// 80
|
|
696
|
+
|
|
697
|
+
evaluate('round(round * 2)', { round: 3.7 })
|
|
698
|
+
// 7
|
|
699
|
+
|
|
700
|
+
// Field named "sum" doesn't conflict with sum() function
|
|
701
|
+
evaluate('sum(values) + sum', { values: [1, 2, 3], sum: 10 })
|
|
702
|
+
// 16`
|
|
703
|
+
},
|
|
704
|
+
{
|
|
705
|
+
name: "Evaluate with context (array items)",
|
|
706
|
+
description: "Use evaluateWithContext() for array item formulas with path resolution",
|
|
707
|
+
code: `// Absolute path: /field always resolves from root
|
|
708
|
+
evaluateWithContext('price * (1 + /taxRate)', {
|
|
709
|
+
rootData: { taxRate: 0.1, items: [{ price: 100 }] },
|
|
710
|
+
itemData: { price: 100 },
|
|
711
|
+
currentPath: 'items[0]'
|
|
712
|
+
})
|
|
713
|
+
// 110
|
|
714
|
+
|
|
715
|
+
// Nested absolute path
|
|
716
|
+
evaluateWithContext('price * /config.multiplier', {
|
|
717
|
+
rootData: { config: { multiplier: 1.5 }, items: [] },
|
|
718
|
+
itemData: { price: 100 },
|
|
719
|
+
currentPath: 'items[0]'
|
|
720
|
+
})
|
|
721
|
+
// 150
|
|
722
|
+
|
|
723
|
+
// Relative path: ../field resolves from parent (root)
|
|
724
|
+
evaluateWithContext('price * (1 - ../discount)', {
|
|
725
|
+
rootData: { discount: 0.2, items: [] },
|
|
726
|
+
itemData: { price: 100 },
|
|
727
|
+
currentPath: 'items[0]'
|
|
728
|
+
})
|
|
729
|
+
// 80
|
|
730
|
+
|
|
731
|
+
// itemData takes precedence over rootData for same field
|
|
732
|
+
evaluateWithContext('value + 10', {
|
|
733
|
+
rootData: { value: 100 },
|
|
734
|
+
itemData: { value: 50 },
|
|
735
|
+
currentPath: 'items[0]'
|
|
736
|
+
})
|
|
737
|
+
// 60`
|
|
738
|
+
},
|
|
739
|
+
{
|
|
740
|
+
name: "Relative paths - path resolution",
|
|
741
|
+
description: "Understanding how ../ resolves based on currentPath. Each ../ goes up one segment (object property or array element counts as one segment)",
|
|
742
|
+
code: `// Path structure explanation:
|
|
743
|
+
// currentPath splits by "." (dots), keeping array indices attached to field names
|
|
744
|
+
// "items[0]" = 1 segment
|
|
745
|
+
// "items[0].inner" = 2 segments: ["items[0]", "inner"]
|
|
746
|
+
// "container.items[0]" = 2 segments: ["container", "items[0]"]
|
|
747
|
+
|
|
748
|
+
// Single ../ from array item -> goes to root
|
|
749
|
+
// currentPath: "items[0]" (1 segment)
|
|
750
|
+
// ../ goes up 1 level -> root
|
|
751
|
+
evaluateWithContext('price * ../discount', {
|
|
752
|
+
rootData: { discount: 0.2, items: [{ price: 100 }] },
|
|
753
|
+
itemData: { price: 100 },
|
|
754
|
+
currentPath: 'items[0]'
|
|
755
|
+
})
|
|
756
|
+
// Resolves ../discount to root.discount = 0.2
|
|
757
|
+
// Result: 100 * 0.2 = 20
|
|
758
|
+
|
|
759
|
+
// Single ../ from nested object in array -> goes to array item
|
|
760
|
+
// currentPath: "items[0].inner" (2 segments)
|
|
761
|
+
// ../ goes up 1 level -> "items[0]"
|
|
762
|
+
evaluateWithContext('price * ../itemMultiplier', {
|
|
763
|
+
rootData: { items: [{ itemMultiplier: 3, inner: { price: 10 } }] },
|
|
764
|
+
itemData: { price: 10 },
|
|
765
|
+
currentPath: 'items[0].inner'
|
|
766
|
+
})
|
|
767
|
+
// Resolves ../itemMultiplier to items[0].itemMultiplier = 3
|
|
768
|
+
// Result: 10 * 3 = 30
|
|
769
|
+
|
|
770
|
+
// Double ../../ from nested object in array -> goes to root
|
|
771
|
+
// currentPath: "items[0].inner" (2 segments)
|
|
772
|
+
// ../../ goes up 2 levels -> root
|
|
773
|
+
evaluateWithContext('price * ../../rootRate', {
|
|
774
|
+
rootData: { rootRate: 2, items: [{ inner: { price: 5 } }] },
|
|
775
|
+
itemData: { price: 5 },
|
|
776
|
+
currentPath: 'items[0].inner'
|
|
777
|
+
})
|
|
778
|
+
// Resolves ../../rootRate to root.rootRate = 2
|
|
779
|
+
// Result: 5 * 2 = 10`
|
|
780
|
+
},
|
|
781
|
+
{
|
|
782
|
+
name: "Relative paths - nested arrays",
|
|
783
|
+
description: "How relative paths work with arrays inside objects and nested arrays",
|
|
784
|
+
code: `// Array inside nested object
|
|
785
|
+
// currentPath: "container.items[0]" (2 segments: ["container", "items[0]"])
|
|
786
|
+
// ../ goes up 1 level -> "container"
|
|
787
|
+
evaluateWithContext('price * ../containerRate', {
|
|
788
|
+
rootData: {
|
|
789
|
+
container: {
|
|
790
|
+
containerRate: 4,
|
|
791
|
+
items: [{ price: 5 }]
|
|
792
|
+
}
|
|
793
|
+
},
|
|
794
|
+
itemData: { price: 5 },
|
|
795
|
+
currentPath: 'container.items[0]'
|
|
796
|
+
})
|
|
797
|
+
// Resolves ../containerRate to container.containerRate = 4
|
|
798
|
+
// Result: 5 * 4 = 20
|
|
799
|
+
|
|
800
|
+
// ../../ from array inside object -> goes to root
|
|
801
|
+
// currentPath: "container.items[0]" (2 segments)
|
|
802
|
+
// ../../ goes up 2 levels -> root
|
|
803
|
+
evaluateWithContext('price * ../../rootVal', {
|
|
804
|
+
rootData: {
|
|
805
|
+
rootVal: 6,
|
|
806
|
+
container: { items: [{ price: 5 }] }
|
|
807
|
+
},
|
|
808
|
+
itemData: { price: 5 },
|
|
809
|
+
currentPath: 'container.items[0]'
|
|
810
|
+
})
|
|
811
|
+
// Resolves ../../rootVal to root.rootVal = 6
|
|
812
|
+
// Result: 5 * 6 = 30
|
|
813
|
+
|
|
814
|
+
// Nested arrays: items[].subItems[]
|
|
815
|
+
// currentPath: "items[0].subItems[0]" (2 segments: ["items[0]", "subItems[0]"])
|
|
816
|
+
// ../ goes up 1 level -> "items[0]"
|
|
817
|
+
evaluateWithContext('qty * ../itemPrice', {
|
|
818
|
+
rootData: {
|
|
819
|
+
items: [{ itemPrice: 10, subItems: [{ qty: 3 }] }]
|
|
820
|
+
},
|
|
821
|
+
itemData: { qty: 3 },
|
|
822
|
+
currentPath: 'items[0].subItems[0]'
|
|
823
|
+
})
|
|
824
|
+
// Resolves ../itemPrice to items[0].itemPrice = 10
|
|
825
|
+
// Result: 3 * 10 = 30`
|
|
826
|
+
},
|
|
827
|
+
{
|
|
828
|
+
name: "Relative paths - accessing nested properties",
|
|
829
|
+
description: "Relative paths can include nested property access after the ../ prefix",
|
|
830
|
+
code: `// ../sibling.nested accesses a sibling with nested property
|
|
831
|
+
// currentPath: "items[0].products[0]" (2 segments)
|
|
832
|
+
// ../ goes to "items[0]", then accesses .config.discount
|
|
833
|
+
evaluateWithContext('price * ../config.discount', {
|
|
834
|
+
rootData: {
|
|
835
|
+
items: [{
|
|
836
|
+
config: { discount: 0.9 },
|
|
837
|
+
products: [{ price: 100 }]
|
|
838
|
+
}]
|
|
839
|
+
},
|
|
840
|
+
itemData: { price: 100 },
|
|
841
|
+
currentPath: 'items[0].products[0]'
|
|
842
|
+
})
|
|
843
|
+
// Resolves ../config.discount to items[0].config.discount = 0.9
|
|
844
|
+
// Result: 100 * 0.9 = 90
|
|
845
|
+
|
|
846
|
+
// Deep nested: ../../settings.tax.rate
|
|
847
|
+
evaluateWithContext('amount * ../../settings.tax.rate', {
|
|
848
|
+
rootData: {
|
|
849
|
+
settings: { tax: { rate: 0.1 } },
|
|
850
|
+
orders: [{ items: [{ amount: 200 }] }]
|
|
851
|
+
},
|
|
852
|
+
itemData: { amount: 200 },
|
|
853
|
+
currentPath: 'orders[0].items[0]'
|
|
854
|
+
})
|
|
855
|
+
// Resolves ../../settings.tax.rate to root.settings.tax.rate = 0.1
|
|
856
|
+
// Result: 200 * 0.1 = 20`
|
|
857
|
+
},
|
|
858
|
+
{
|
|
859
|
+
name: "Relative paths - complex nesting",
|
|
860
|
+
description: "Complex scenarios with arrays inside objects inside arrays",
|
|
861
|
+
code: `// Array inside object inside array
|
|
862
|
+
// Structure: items[].container.subItems[]
|
|
863
|
+
// currentPath: "items[0].container.subItems[0]" (3 segments)
|
|
864
|
+
evaluateWithContext('val * ../containerMultiplier', {
|
|
865
|
+
rootData: {
|
|
866
|
+
items: [{
|
|
867
|
+
container: {
|
|
868
|
+
containerMultiplier: 4,
|
|
869
|
+
subItems: [{ val: 3 }]
|
|
870
|
+
}
|
|
871
|
+
}]
|
|
872
|
+
},
|
|
873
|
+
itemData: { val: 3 },
|
|
874
|
+
currentPath: 'items[0].container.subItems[0]'
|
|
875
|
+
})
|
|
876
|
+
// ../ goes to "items[0].container"
|
|
877
|
+
// Resolves ../containerMultiplier to items[0].container.containerMultiplier = 4
|
|
878
|
+
// Result: 3 * 4 = 12
|
|
879
|
+
|
|
880
|
+
// ../../ from same structure -> goes to array item
|
|
881
|
+
evaluateWithContext('val * ../../itemRate', {
|
|
882
|
+
rootData: {
|
|
883
|
+
items: [{
|
|
884
|
+
itemRate: 5,
|
|
885
|
+
container: { subItems: [{ val: 2 }] }
|
|
886
|
+
}]
|
|
887
|
+
},
|
|
888
|
+
itemData: { val: 2 },
|
|
889
|
+
currentPath: 'items[0].container.subItems[0]'
|
|
890
|
+
})
|
|
891
|
+
// ../../ goes to "items[0]"
|
|
892
|
+
// Resolves ../../itemRate to items[0].itemRate = 5
|
|
893
|
+
// Result: 2 * 5 = 10
|
|
894
|
+
|
|
895
|
+
// ../../../ from same structure -> goes to root
|
|
896
|
+
evaluateWithContext('val * ../../../rootFactor', {
|
|
897
|
+
rootData: {
|
|
898
|
+
rootFactor: 3,
|
|
899
|
+
items: [{
|
|
900
|
+
container: { subItems: [{ val: 7 }] }
|
|
901
|
+
}]
|
|
902
|
+
},
|
|
903
|
+
itemData: { val: 7 },
|
|
904
|
+
currentPath: 'items[0].container.subItems[0]'
|
|
905
|
+
})
|
|
906
|
+
// ../../../ goes to root
|
|
907
|
+
// Resolves ../../../rootFactor to root.rootFactor = 3
|
|
908
|
+
// Result: 7 * 3 = 21`
|
|
909
|
+
},
|
|
910
|
+
{
|
|
911
|
+
name: "Array context tokens - basic",
|
|
912
|
+
description: "Use #index, #length, #first, #last for position info; @prev, @next for neighbor access",
|
|
913
|
+
code: `// arrayContext provides position info for array item formulas
|
|
914
|
+
const arrayContext = {
|
|
915
|
+
levels: [{
|
|
916
|
+
index: 2, // current position
|
|
917
|
+
length: 5, // array length
|
|
918
|
+
prev: { value: 20 }, // previous element
|
|
919
|
+
next: { value: 40 }, // next element
|
|
920
|
+
}]
|
|
921
|
+
};
|
|
922
|
+
|
|
923
|
+
evaluateWithContext('#index', { rootData: {}, arrayContext })
|
|
924
|
+
// 2
|
|
925
|
+
|
|
926
|
+
evaluateWithContext('#length', { rootData: {}, arrayContext })
|
|
927
|
+
// 5
|
|
928
|
+
|
|
929
|
+
evaluateWithContext('#first', { rootData: {}, arrayContext })
|
|
930
|
+
// false (index !== 0)
|
|
931
|
+
|
|
932
|
+
evaluateWithContext('#last', { rootData: {}, arrayContext })
|
|
933
|
+
// false (index !== length - 1)
|
|
934
|
+
|
|
935
|
+
evaluateWithContext('@prev.value', { rootData: {}, arrayContext })
|
|
936
|
+
// 20
|
|
937
|
+
|
|
938
|
+
evaluateWithContext('@next.value', { rootData: {}, arrayContext })
|
|
939
|
+
// 40
|
|
940
|
+
|
|
941
|
+
// At first element, @prev is null
|
|
942
|
+
evaluateWithContext('@prev', {
|
|
943
|
+
rootData: {},
|
|
944
|
+
arrayContext: { levels: [{ index: 0, length: 3, prev: null, next: {} }] }
|
|
945
|
+
})
|
|
946
|
+
// null`
|
|
947
|
+
},
|
|
948
|
+
{
|
|
949
|
+
name: "Array context tokens - nested arrays",
|
|
950
|
+
description: "Access parent array context with #parent.*, #root.*, @parent.*, @root.*",
|
|
951
|
+
code: `// For nested arrays like orders[].items[]:
|
|
952
|
+
// levels[0] = innermost (items), levels[1] = parent (orders)
|
|
953
|
+
const arrayContext = {
|
|
954
|
+
levels: [
|
|
955
|
+
{ index: 1, length: 3, prev: {}, next: {} }, // items[1]
|
|
956
|
+
{ index: 2, length: 5, prev: {}, next: {} }, // orders[2]
|
|
957
|
+
]
|
|
958
|
+
};
|
|
959
|
+
|
|
960
|
+
evaluateWithContext('#index', { rootData: {}, arrayContext })
|
|
961
|
+
// 1 (current item index)
|
|
962
|
+
|
|
963
|
+
evaluateWithContext('#parent.index', { rootData: {}, arrayContext })
|
|
964
|
+
// 2 (parent order index)
|
|
965
|
+
|
|
966
|
+
evaluateWithContext('#parent.length', { rootData: {}, arrayContext })
|
|
967
|
+
// 5 (number of orders)
|
|
968
|
+
|
|
969
|
+
// #root.* is shortcut for topmost array (same as #parent.* for 2 levels)
|
|
970
|
+
evaluateWithContext('#root.index', { rootData: {}, arrayContext })
|
|
971
|
+
// 2
|
|
972
|
+
|
|
973
|
+
// For 3+ levels, #root always points to outermost array
|
|
974
|
+
const threeLevel = {
|
|
975
|
+
levels: [
|
|
976
|
+
{ index: 0, length: 2, prev: null, next: {} }, // innermost
|
|
977
|
+
{ index: 1, length: 3, prev: {}, next: {} }, // middle
|
|
978
|
+
{ index: 2, length: 4, prev: {}, next: null }, // outermost (root)
|
|
979
|
+
]
|
|
980
|
+
};
|
|
981
|
+
|
|
982
|
+
evaluateWithContext('#parent.parent.index', { rootData: {}, arrayContext: threeLevel })
|
|
983
|
+
// 2 (outermost)
|
|
984
|
+
|
|
985
|
+
evaluateWithContext('#root.index', { rootData: {}, arrayContext: threeLevel })
|
|
986
|
+
// 2 (same as #parent.parent.index)`
|
|
987
|
+
},
|
|
988
|
+
{
|
|
989
|
+
name: "Array context tokens - practical examples",
|
|
990
|
+
description: "Common patterns: running total, numbering, delta calculation",
|
|
991
|
+
code: `// Running total pattern (like Excel)
|
|
992
|
+
// rows[].runningTotal = if(#first, value, @prev.runningTotal + value)
|
|
993
|
+
const rows = [
|
|
994
|
+
{ value: 10 },
|
|
995
|
+
{ value: 20 },
|
|
996
|
+
{ value: 15 },
|
|
997
|
+
];
|
|
998
|
+
|
|
999
|
+
// For rows[2]:
|
|
1000
|
+
evaluateWithContext('if(#first, value, @prev.value + value)', {
|
|
1001
|
+
rootData: {},
|
|
1002
|
+
itemData: { value: 15 },
|
|
1003
|
+
arrayContext: {
|
|
1004
|
+
levels: [{
|
|
1005
|
+
index: 2,
|
|
1006
|
+
length: 3,
|
|
1007
|
+
prev: { value: 20 }, // Note: use non-computed field from prev
|
|
1008
|
+
next: null,
|
|
1009
|
+
}]
|
|
1010
|
+
}
|
|
1011
|
+
})
|
|
1012
|
+
// 35 (20 + 15)
|
|
1013
|
+
|
|
1014
|
+
// Nested numbering like "1.1", "1.2", "2.1"
|
|
1015
|
+
// sections[].questions[].number
|
|
1016
|
+
evaluateWithContext('concat(#parent.index + 1, ".", #index + 1)', {
|
|
1017
|
+
rootData: {},
|
|
1018
|
+
arrayContext: {
|
|
1019
|
+
levels: [
|
|
1020
|
+
{ index: 1, length: 3, prev: {}, next: {} }, // question index
|
|
1021
|
+
{ index: 0, length: 2, prev: null, next: {} }, // section index
|
|
1022
|
+
]
|
|
1023
|
+
}
|
|
1024
|
+
})
|
|
1025
|
+
// "1.2"
|
|
1026
|
+
|
|
1027
|
+
// Delta from previous
|
|
1028
|
+
// measurements[].delta = if(#first, 0, value - @prev.value)
|
|
1029
|
+
evaluateWithContext('if(#first, 0, value - @prev.value)', {
|
|
1030
|
+
rootData: {},
|
|
1031
|
+
itemData: { value: 105 },
|
|
1032
|
+
arrayContext: {
|
|
1033
|
+
levels: [{
|
|
1034
|
+
index: 1,
|
|
1035
|
+
length: 3,
|
|
1036
|
+
prev: { value: 100 },
|
|
1037
|
+
next: { value: 102 },
|
|
1038
|
+
}]
|
|
1039
|
+
}
|
|
1040
|
+
})
|
|
1041
|
+
// 5`
|
|
1042
|
+
}
|
|
1043
|
+
],
|
|
1044
|
+
schemaUsage: {
|
|
1045
|
+
structure: "{ \"x-formula\": { \"version\": 1, \"expression\": \"...\" }, \"readOnly\": true }",
|
|
1046
|
+
fieldTypes: [
|
|
1047
|
+
"string",
|
|
1048
|
+
"number",
|
|
1049
|
+
"boolean"
|
|
1050
|
+
],
|
|
1051
|
+
rules: [
|
|
1052
|
+
"Add x-formula to string, number, or boolean field schema",
|
|
1053
|
+
"readOnly: true is REQUIRED for fields with x-formula",
|
|
1054
|
+
"Expression must reference existing fields in the same table",
|
|
1055
|
+
"Circular dependencies are not allowed (a references b, b references a)"
|
|
1056
|
+
]
|
|
1057
|
+
}
|
|
1058
|
+
};
|
|
1059
|
+
|
|
1060
|
+
//#endregion
|
|
1061
|
+
export { formulaSpec };
|
|
1062
|
+
//# sourceMappingURL=formula-spec.mjs.map
|