@revisium/formula 0.2.1 → 0.4.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.
@@ -31,10 +31,301 @@ var formulaSpec = {
31
31
  { operator: "||", description: "Logical OR" },
32
32
  { operator: "!", description: "Logical NOT" }
33
33
  ],
34
- other: [
35
- "Parentheses: (a + b) * c",
36
- "Ternary: condition ? valueIfTrue : valueIfFalse",
37
- "Unary minus: -value, a + -b"
34
+ other: ["Parentheses: (a + b) * c", "Unary minus: -value, a + -b"]
35
+ },
36
+ functions: {
37
+ string: [
38
+ {
39
+ name: "concat",
40
+ description: "Concatenate multiple values into a single string",
41
+ signature: "concat(value1, value2, ...)",
42
+ returnType: "string",
43
+ examples: [
44
+ 'concat(firstName, " ", lastName) // "John Doe"',
45
+ 'concat("Price: ", price, " USD") // "Price: 100 USD"'
46
+ ]
47
+ },
48
+ {
49
+ name: "upper",
50
+ description: "Convert string to uppercase",
51
+ signature: "upper(text)",
52
+ returnType: "string",
53
+ examples: ['upper(name) // "HELLO"']
54
+ },
55
+ {
56
+ name: "lower",
57
+ description: "Convert string to lowercase",
58
+ signature: "lower(text)",
59
+ returnType: "string",
60
+ examples: ['lower(name) // "hello"']
61
+ },
62
+ {
63
+ name: "trim",
64
+ description: "Remove whitespace from both ends of a string",
65
+ signature: "trim(text)",
66
+ returnType: "string",
67
+ examples: ['trim(name) // "hello" from " hello "']
68
+ },
69
+ {
70
+ name: "left",
71
+ description: "Extract characters from the beginning of a string",
72
+ signature: "left(text, count)",
73
+ returnType: "string",
74
+ examples: ['left(name, 3) // "hel" from "hello"']
75
+ },
76
+ {
77
+ name: "right",
78
+ description: "Extract characters from the end of a string",
79
+ signature: "right(text, count)",
80
+ returnType: "string",
81
+ examples: ['right(name, 3) // "llo" from "hello"']
82
+ },
83
+ {
84
+ name: "replace",
85
+ description: "Replace first occurrence of a substring",
86
+ signature: "replace(text, search, replacement)",
87
+ returnType: "string",
88
+ examples: ['replace(name, "o", "0") // "hell0" from "hello"']
89
+ },
90
+ {
91
+ name: "join",
92
+ description: "Join array elements into a string",
93
+ signature: "join(array, separator?)",
94
+ returnType: "string",
95
+ examples: ['join(tags) // "a,b,c"', 'join(tags, " | ") // "a | b | c"']
96
+ }
97
+ ],
98
+ numeric: [
99
+ {
100
+ name: "round",
101
+ description: "Round a number to specified decimal places",
102
+ signature: "round(number, decimals?)",
103
+ returnType: "number",
104
+ examples: ["round(3.14159, 2) // 3.14", "round(3.5) // 4"]
105
+ },
106
+ {
107
+ name: "floor",
108
+ description: "Round down to the nearest integer",
109
+ signature: "floor(number)",
110
+ returnType: "number",
111
+ examples: ["floor(3.7) // 3"]
112
+ },
113
+ {
114
+ name: "ceil",
115
+ description: "Round up to the nearest integer",
116
+ signature: "ceil(number)",
117
+ returnType: "number",
118
+ examples: ["ceil(3.2) // 4"]
119
+ },
120
+ {
121
+ name: "abs",
122
+ description: "Get the absolute value",
123
+ signature: "abs(number)",
124
+ returnType: "number",
125
+ examples: ["abs(-5) // 5"]
126
+ },
127
+ {
128
+ name: "sqrt",
129
+ description: "Calculate the square root",
130
+ signature: "sqrt(number)",
131
+ returnType: "number",
132
+ examples: ["sqrt(16) // 4"]
133
+ },
134
+ {
135
+ name: "pow",
136
+ description: "Raise a number to a power",
137
+ signature: "pow(base, exponent)",
138
+ returnType: "number",
139
+ examples: ["pow(2, 3) // 8"]
140
+ },
141
+ {
142
+ name: "min",
143
+ description: "Get the minimum of multiple values",
144
+ signature: "min(value1, value2, ...)",
145
+ returnType: "number",
146
+ examples: ["min(a, b, c) // smallest value"]
147
+ },
148
+ {
149
+ name: "max",
150
+ description: "Get the maximum of multiple values",
151
+ signature: "max(value1, value2, ...)",
152
+ returnType: "number",
153
+ examples: ["max(a, b, c) // largest value"]
154
+ },
155
+ {
156
+ name: "log",
157
+ description: "Calculate the natural logarithm",
158
+ signature: "log(number)",
159
+ returnType: "number",
160
+ examples: ["log(10) // 2.302..."]
161
+ },
162
+ {
163
+ name: "log10",
164
+ description: "Calculate the base-10 logarithm",
165
+ signature: "log10(number)",
166
+ returnType: "number",
167
+ examples: ["log10(100) // 2"]
168
+ },
169
+ {
170
+ name: "exp",
171
+ description: "Calculate e raised to a power",
172
+ signature: "exp(number)",
173
+ returnType: "number",
174
+ examples: ["exp(1) // 2.718..."]
175
+ },
176
+ {
177
+ name: "sign",
178
+ description: "Get the sign of a number (-1, 0, or 1)",
179
+ signature: "sign(number)",
180
+ returnType: "number",
181
+ examples: ["sign(-5) // -1", "sign(0) // 0", "sign(5) // 1"]
182
+ },
183
+ {
184
+ name: "length",
185
+ description: "Get the length of a string or array",
186
+ signature: "length(value)",
187
+ returnType: "number",
188
+ examples: ['length(name) // 5 from "hello"', "length(items) // 3"]
189
+ }
190
+ ],
191
+ boolean: [
192
+ {
193
+ name: "and",
194
+ description: "Logical AND of two values",
195
+ signature: "and(a, b)",
196
+ returnType: "boolean",
197
+ examples: ["and(isActive, hasPermission) // true if both true"]
198
+ },
199
+ {
200
+ name: "or",
201
+ description: "Logical OR of two values",
202
+ signature: "or(a, b)",
203
+ returnType: "boolean",
204
+ examples: ["or(isAdmin, isOwner) // true if either true"]
205
+ },
206
+ {
207
+ name: "not",
208
+ description: "Logical NOT of a value",
209
+ signature: "not(value)",
210
+ returnType: "boolean",
211
+ examples: ["not(isDeleted) // true if false"]
212
+ },
213
+ {
214
+ name: "contains",
215
+ description: "Check if a string contains a substring",
216
+ signature: "contains(text, search)",
217
+ returnType: "boolean",
218
+ examples: ['contains(name, "ell") // true for "hello"']
219
+ },
220
+ {
221
+ name: "startswith",
222
+ description: "Check if a string starts with a prefix",
223
+ signature: "startswith(text, prefix)",
224
+ returnType: "boolean",
225
+ examples: ['startswith(name, "hel") // true for "hello"']
226
+ },
227
+ {
228
+ name: "endswith",
229
+ description: "Check if a string ends with a suffix",
230
+ signature: "endswith(text, suffix)",
231
+ returnType: "boolean",
232
+ examples: ['endswith(name, "llo") // true for "hello"']
233
+ },
234
+ {
235
+ name: "isnull",
236
+ description: "Check if a value is null or undefined",
237
+ signature: "isnull(value)",
238
+ returnType: "boolean",
239
+ examples: ["isnull(optionalField) // true if null/undefined"]
240
+ },
241
+ {
242
+ name: "includes",
243
+ description: "Check if an array contains a value",
244
+ signature: "includes(array, value)",
245
+ returnType: "boolean",
246
+ examples: [
247
+ 'includes(tags, "featured") // true if array contains value'
248
+ ]
249
+ }
250
+ ],
251
+ array: [
252
+ {
253
+ name: "sum",
254
+ description: "Calculate the sum of array elements",
255
+ signature: "sum(array)",
256
+ returnType: "number",
257
+ examples: ["sum(prices) // total of all prices"]
258
+ },
259
+ {
260
+ name: "avg",
261
+ description: "Calculate the average of array elements",
262
+ signature: "avg(array)",
263
+ returnType: "number",
264
+ examples: ["avg(scores) // average score"]
265
+ },
266
+ {
267
+ name: "count",
268
+ description: "Get the number of elements in an array",
269
+ signature: "count(array)",
270
+ returnType: "number",
271
+ examples: ["count(items) // number of items"]
272
+ },
273
+ {
274
+ name: "first",
275
+ description: "Get the first element of an array",
276
+ signature: "first(array)",
277
+ returnType: "any",
278
+ examples: ["first(items) // first item"]
279
+ },
280
+ {
281
+ name: "last",
282
+ description: "Get the last element of an array",
283
+ signature: "last(array)",
284
+ returnType: "any",
285
+ examples: ["last(items) // last item"]
286
+ }
287
+ ],
288
+ conversion: [
289
+ {
290
+ name: "tostring",
291
+ description: "Convert a value to string",
292
+ signature: "tostring(value)",
293
+ returnType: "string",
294
+ examples: ['tostring(42) // "42"']
295
+ },
296
+ {
297
+ name: "tonumber",
298
+ description: "Convert a value to number",
299
+ signature: "tonumber(value)",
300
+ returnType: "number",
301
+ examples: ['tonumber("42") // 42']
302
+ },
303
+ {
304
+ name: "toboolean",
305
+ description: "Convert a value to boolean",
306
+ signature: "toboolean(value)",
307
+ returnType: "boolean",
308
+ examples: ["toboolean(1) // true", "toboolean(0) // false"]
309
+ }
310
+ ],
311
+ conditional: [
312
+ {
313
+ name: "if",
314
+ description: "Return one of two values based on a condition",
315
+ signature: "if(condition, valueIfTrue, valueIfFalse)",
316
+ returnType: "any",
317
+ examples: [
318
+ 'if(stock > 0, "Available", "Out of Stock")',
319
+ "if(price > 100, price * 0.9, price)"
320
+ ]
321
+ },
322
+ {
323
+ name: "coalesce",
324
+ description: "Return the first non-null value",
325
+ signature: "coalesce(value1, value2, ...)",
326
+ returnType: "any",
327
+ examples: ['coalesce(nickname, name, "Anonymous")']
328
+ }
38
329
  ]
39
330
  },
40
331
  features: [
@@ -75,12 +366,50 @@ var formulaSpec = {
75
366
  "items[-2].price // second to last"
76
367
  ],
77
368
  dependenciesExtracted: ['["items[0].price"]', '["items[-1].name"]']
369
+ },
370
+ {
371
+ name: "root_path",
372
+ description: "Absolute path reference starting with /. Always resolves from root data, even inside array item formulas",
373
+ minVersion: "1.1",
374
+ examples: [
375
+ "/taxRate",
376
+ "/config.tax",
377
+ "price * (1 + /taxRate)",
378
+ "price * /config.multiplier"
379
+ ],
380
+ dependenciesExtracted: ['["/taxRate"]', '["/config.tax"]']
381
+ },
382
+ {
383
+ name: "relative_path",
384
+ description: "Relative path reference starting with ../. Resolves from parent context (root data) when inside array item formulas",
385
+ minVersion: "1.1",
386
+ examples: [
387
+ "../discount",
388
+ "../settings.multiplier",
389
+ "price * (1 - ../discount)",
390
+ "price * ../settings.multiplier"
391
+ ],
392
+ dependenciesExtracted: ['["../discount"]', '["../settings.multiplier"]']
393
+ },
394
+ {
395
+ name: "function_named_fields",
396
+ 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",
397
+ minVersion: "1.0",
398
+ examples: [
399
+ "max(max, 0)",
400
+ "min(min, 100)",
401
+ "max(max - field.min, 0)",
402
+ "round(round * 2)"
403
+ ]
78
404
  }
79
405
  ],
80
406
  versionDetection: [
81
407
  { feature: "Simple refs, arithmetic, comparisons", minVersion: "1.0" },
408
+ { feature: "Function-named fields (max(max, 0))", minVersion: "1.0" },
82
409
  { feature: "Nested paths (a.b)", minVersion: "1.1" },
83
- { feature: "Array index ([0], [-1])", minVersion: "1.1" }
410
+ { feature: "Array index ([0], [-1])", minVersion: "1.1" },
411
+ { feature: "Absolute paths (/field)", minVersion: "1.1" },
412
+ { feature: "Relative paths (../field)", minVersion: "1.1" }
84
413
  ],
85
414
  parseResult: {
86
415
  description: "The parser automatically detects the minimum required version",
@@ -108,7 +437,7 @@ var formulaSpec = {
108
437
  result: "boolean"
109
438
  },
110
439
  {
111
- expression: 'stock > 0 ? "Available" : "Out of Stock"',
440
+ expression: 'if(stock > 0, "Available", "Out of Stock")',
112
441
  description: "Conditional text based on stock",
113
442
  result: "string"
114
443
  },
@@ -171,6 +500,58 @@ evaluate('price > 100', { price: 150 })
171
500
 
172
501
  evaluate('a + b * c', { a: 1, b: 2, c: 3 })
173
502
  // 7`
503
+ },
504
+ {
505
+ name: "Function-named fields",
506
+ description: "Fields can have the same name as built-in functions",
507
+ code: `// Built-in functions take precedence in function calls
508
+ evaluate('max(max, 0)', { max: 10 })
509
+ // 10 (max() function, then max field)
510
+
511
+ evaluate('max(max - field.min, 0)', { max: 100, field: { min: 20 } })
512
+ // 80
513
+
514
+ evaluate('round(round * 2)', { round: 3.7 })
515
+ // 7
516
+
517
+ // Field named "sum" doesn't conflict with sum() function
518
+ evaluate('sum(values) + sum', { values: [1, 2, 3], sum: 10 })
519
+ // 16`
520
+ },
521
+ {
522
+ name: "Evaluate with context (array items)",
523
+ description: "Use evaluateWithContext() for array item formulas with path resolution",
524
+ code: `// Absolute path: /field always resolves from root
525
+ evaluateWithContext('price * (1 + /taxRate)', {
526
+ rootData: { taxRate: 0.1, items: [{ price: 100 }] },
527
+ itemData: { price: 100 },
528
+ currentPath: 'items[0]'
529
+ })
530
+ // 110
531
+
532
+ // Nested absolute path
533
+ evaluateWithContext('price * /config.multiplier', {
534
+ rootData: { config: { multiplier: 1.5 }, items: [] },
535
+ itemData: { price: 100 },
536
+ currentPath: 'items[0]'
537
+ })
538
+ // 150
539
+
540
+ // Relative path: ../field resolves from parent (root)
541
+ evaluateWithContext('price * (1 - ../discount)', {
542
+ rootData: { discount: 0.2, items: [] },
543
+ itemData: { price: 100 },
544
+ currentPath: 'items[0]'
545
+ })
546
+ // 80
547
+
548
+ // itemData takes precedence over rootData for same field
549
+ evaluateWithContext('value + 10', {
550
+ rootData: { value: 100 },
551
+ itemData: { value: 50 },
552
+ currentPath: 'items[0]'
553
+ })
554
+ // 60`
174
555
  }
175
556
  ],
176
557
  schemaUsage: {
@@ -180,8 +561,7 @@ evaluate('a + b * c', { a: 1, b: 2, c: 3 })
180
561
  "Add x-formula to string, number, or boolean field schema",
181
562
  "readOnly: true is REQUIRED for fields with x-formula",
182
563
  "Expression must reference existing fields in the same table",
183
- "Circular dependencies are not allowed (a references b, b references a)",
184
- "Referenced fields must exist before the formula field in schema order"
564
+ "Circular dependencies are not allowed (a references b, b references a)"
185
565
  ]
186
566
  }
187
567
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/formula-spec.ts"],"names":[],"mappings":";;;AAuCO,IAAM,WAAA,GAA2B;AAAA,EACtC,OAAA,EAAS,KAAA;AAAA,EACT,WAAA,EACE,8GAAA;AAAA,EAEF,MAAA,EAAQ;AAAA,IACN,eAAA,EAAiB;AAAA,MACf,iDAAA;AAAA,MACA,mDAAA;AAAA,MACA,qDAAA;AAAA,MACA;AAAA,KACF;AAAA,IACA,mBAAA,EAAqB;AAAA,MACnB,EAAE,QAAA,EAAU,GAAA,EAAK,WAAA,EAAa,kCAAA,EAAmC;AAAA,MACjE,EAAE,QAAA,EAAU,GAAA,EAAK,WAAA,EAAa,aAAA,EAAc;AAAA,MAC5C,EAAE,QAAA,EAAU,GAAA,EAAK,WAAA,EAAa,gBAAA,EAAiB;AAAA,MAC/C,EAAE,QAAA,EAAU,GAAA,EAAK,WAAA,EAAa,UAAA,EAAW;AAAA,MACzC,EAAE,QAAA,EAAU,GAAA,EAAK,WAAA,EAAa,oBAAA;AAAqB,KACrD;AAAA,IACA,mBAAA,EAAqB;AAAA,MACn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AAAA,MACA,6DAAA;AAAA,MACA,wEAAA;AAAA,MACA;AAAA;AACF;AAEJ","file":"formula-spec.js","sourcesContent":["export interface FormulaSpec {\n version: string;\n description: string;\n syntax: {\n fieldReferences: string[];\n arithmeticOperators: { operator: string; description: string }[];\n comparisonOperators: { operator: string; description: string }[];\n logicalOperators: { operator: string; description: string }[];\n other: string[];\n };\n features: {\n name: string;\n description: string;\n minVersion: string;\n examples: string[];\n dependenciesExtracted?: string[];\n }[];\n versionDetection: { feature: string; minVersion: string }[];\n parseResult: {\n description: string;\n interface: string;\n };\n examples: {\n expression: string;\n description: string;\n result?: string;\n }[];\n apiExamples: {\n name: string;\n description: string;\n code: string;\n }[];\n schemaUsage: {\n structure: string;\n fieldTypes: string[];\n rules: string[];\n };\n}\n\nexport const formulaSpec: FormulaSpec = {\n version: '1.1',\n description:\n 'Formula expressions for computed fields. Formulas reference other fields and calculate values automatically.',\n\n syntax: {\n fieldReferences: [\n 'Simple field: fieldName (e.g., price, quantity)',\n 'Nested path: object.property (e.g., stats.damage)',\n 'Array index: array[0] or array[-1] for last element',\n 'Combined: items[0].price, user.addresses[-1].city',\n ],\n arithmeticOperators: [\n { operator: '+', description: 'Addition or string concatenation' },\n { operator: '-', description: 'Subtraction' },\n { operator: '*', description: 'Multiplication' },\n { operator: '/', description: 'Division' },\n { operator: '%', description: 'Modulo (remainder)' },\n ],\n comparisonOperators: [\n { operator: '==', description: 'Equal' },\n { operator: '!=', description: 'Not equal' },\n { operator: '>', description: 'Greater than' },\n { operator: '<', description: 'Less than' },\n { operator: '>=', description: 'Greater or equal' },\n { operator: '<=', description: 'Less or equal' },\n ],\n logicalOperators: [\n { operator: '&&', description: 'Logical AND' },\n { operator: '||', description: 'Logical OR' },\n { operator: '!', description: 'Logical NOT' },\n ],\n other: [\n 'Parentheses: (a + b) * c',\n 'Ternary: condition ? valueIfTrue : valueIfFalse',\n 'Unary minus: -value, a + -b',\n ],\n },\n\n features: [\n {\n name: 'simple_refs',\n description: 'Reference top-level fields by name',\n minVersion: '1.0',\n examples: ['price', 'quantity', 'baseDamage'],\n dependenciesExtracted: ['[\"price\"]', '[\"quantity\"]', '[\"baseDamage\"]'],\n },\n {\n name: 'arithmetic',\n description: 'Basic math operations (+, -, *, /)',\n minVersion: '1.0',\n examples: ['price * 1.1', 'a + b - c', 'quantity * price'],\n },\n {\n name: 'comparison',\n description: 'Compare values (>, <, >=, <=, ==, !=)',\n minVersion: '1.0',\n examples: ['price > 100', 'x == 10', 'quantity >= 5'],\n },\n {\n name: 'nested_path',\n description: 'Access nested object properties using dot notation',\n minVersion: '1.1',\n examples: ['stats.damage', 'user.profile.name', 'item.metadata.category'],\n dependenciesExtracted: ['[\"stats.damage\"]'],\n },\n {\n name: 'array_index',\n description:\n 'Access array elements by numeric index. Negative indices access from the end',\n minVersion: '1.1',\n examples: [\n 'items[0].price',\n 'inventory[1].quantity',\n 'items[-1].name // last element',\n 'items[-2].price // second to last',\n ],\n dependenciesExtracted: ['[\"items[0].price\"]', '[\"items[-1].name\"]'],\n },\n ],\n\n versionDetection: [\n { feature: 'Simple refs, arithmetic, comparisons', minVersion: '1.0' },\n { feature: 'Nested paths (a.b)', minVersion: '1.1' },\n { feature: 'Array index ([0], [-1])', minVersion: '1.1' },\n ],\n\n parseResult: {\n description:\n 'The parser automatically detects the minimum required version',\n interface: `interface ParseResult {\n ast: ASTNode; // Abstract syntax tree\n dependencies: string[]; // List of field dependencies\n features: string[]; // List of detected features\n minVersion: string; // Minimum required version (\"1.0\" or \"1.1\")\n}`,\n },\n\n examples: [\n {\n expression: 'price * quantity',\n description: 'Calculate total from price and quantity',\n result: 'number',\n },\n {\n expression: 'firstName + \" \" + lastName',\n description: 'Concatenate strings with space',\n result: 'string',\n },\n {\n expression: 'quantity > 0',\n description: 'Check if in stock',\n result: 'boolean',\n },\n {\n expression: 'stock > 0 ? \"Available\" : \"Out of Stock\"',\n description: 'Conditional text based on stock',\n result: 'string',\n },\n {\n expression: 'price * (1 + taxRate)',\n description: 'Price with tax',\n result: 'number',\n },\n {\n expression: 'items[0].price + items[1].price',\n description: 'Sum first two item prices (v1.1)',\n result: 'number',\n },\n ],\n\n apiExamples: [\n {\n name: 'Simple Expression (v1.0)',\n description: 'Parse a basic arithmetic expression',\n code: `parseExpression('price * 1.1')\n// {\n// minVersion: \"1.0\",\n// features: [],\n// dependencies: [\"price\"]\n// }`,\n },\n {\n name: 'Nested Path (v1.1)',\n description: 'Parse expression with nested object access',\n code: `parseExpression('stats.damage * multiplier')\n// {\n// minVersion: \"1.1\",\n// features: [\"nested_path\"],\n// dependencies: [\"stats.damage\", \"multiplier\"]\n// }`,\n },\n {\n name: 'Array Access (v1.1)',\n description: 'Parse expression with array index access',\n code: `parseExpression('items[0].price + items[1].price')\n// {\n// minVersion: \"1.1\",\n// features: [\"array_index\", \"nested_path\"],\n// dependencies: [\"items[0].price\", \"items[1].price\"]\n// }`,\n },\n {\n name: 'Evaluate expressions',\n description: 'Execute formulas with context data',\n code: `evaluate('price * 1.1', { price: 100 })\n// 110\n\nevaluate('stats.damage', { stats: { damage: 50 } })\n// 50\n\nevaluate('items[0].price', { items: [{ price: 10 }] })\n// 10\n\nevaluate('price > 100', { price: 150 })\n// true\n\nevaluate('a + b * c', { a: 1, b: 2, c: 3 })\n// 7`,\n },\n ],\n\n schemaUsage: {\n structure:\n '{ \"x-formula\": { \"version\": 1, \"expression\": \"...\" }, \"readOnly\": true }',\n fieldTypes: ['string', 'number', 'boolean'],\n rules: [\n 'Add x-formula to string, number, or boolean field schema',\n 'readOnly: true is REQUIRED for fields with x-formula',\n 'Expression must reference existing fields in the same table',\n 'Circular dependencies are not allowed (a references b, b references a)',\n 'Referenced fields must exist before the formula field in schema order',\n ],\n },\n};\n"]}
1
+ {"version":3,"sources":["../src/formula-spec.ts"],"names":[],"mappings":";;;AAuDO,IAAM,WAAA,GAA2B;AAAA,EACtC,OAAA,EAAS,KAAA;AAAA,EACT,WAAA,EACE,8GAAA;AAAA,EAEF,MAAA,EA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AM,CAAA;AAAA;AAAA;;AAAA;AAAA;;AAAA;AAAA;;AAAA;AAAA;AAAA,KAAA;AAAA,KAaR;AAAA,IACA;AAAA,MACE,IAAA,EAAM,qCAAA;AAAA,MACN,WAAA,EACE,wEAAA;AAAA,MACF,IAAA,EAAM,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,KAAA;AAAA;AA+BR,GACF;AAAA,EAEA,WAAA,EAAa;AAAA,IACX,SAAA,EACE,0EAAA;AAAA,IACF,UAAA,EAAY,CAAC,QAAA,EAAU,QAAA,EAAU,SAAS,CAAA;AAAA,IAC1C,KAAA,EAAO;AAAA,MACL,0DAAA;AAAA,MACA,sDAAA;AAAA,MACA,6DAAA;AAAA,MACA;AAAA;AACF;AAEJ","file":"formula-spec.js","sourcesContent":["export interface FunctionSpec {\n name: string;\n description: string;\n signature: string;\n returnType: 'string' | 'number' | 'boolean' | 'any';\n examples: string[];\n}\n\nexport interface FormulaSpec {\n version: string;\n description: string;\n syntax: {\n fieldReferences: string[];\n arithmeticOperators: { operator: string; description: string }[];\n comparisonOperators: { operator: string; description: string }[];\n logicalOperators: { operator: string; description: string }[];\n other: string[];\n };\n functions: {\n string: FunctionSpec[];\n numeric: FunctionSpec[];\n boolean: FunctionSpec[];\n array: FunctionSpec[];\n conversion: FunctionSpec[];\n conditional: FunctionSpec[];\n };\n features: {\n name: string;\n description: string;\n minVersion: string;\n examples: string[];\n dependenciesExtracted?: string[];\n }[];\n versionDetection: { feature: string; minVersion: string }[];\n parseResult: {\n description: string;\n interface: string;\n };\n examples: {\n expression: string;\n description: string;\n result?: string;\n }[];\n apiExamples: {\n name: string;\n description: string;\n code: string;\n }[];\n schemaUsage: {\n structure: string;\n fieldTypes: string[];\n rules: string[];\n };\n}\n\nexport const formulaSpec: FormulaSpec = {\n version: '1.1',\n description:\n 'Formula expressions for computed fields. Formulas reference other fields and calculate values automatically.',\n\n syntax: {\n fieldReferences: [\n 'Simple field: fieldName (e.g., price, quantity)',\n 'Nested path: object.property (e.g., stats.damage)',\n 'Array index: array[0] or array[-1] for last element',\n 'Combined: items[0].price, user.addresses[-1].city',\n ],\n arithmeticOperators: [\n { operator: '+', description: 'Addition or string concatenation' },\n { operator: '-', description: 'Subtraction' },\n { operator: '*', description: 'Multiplication' },\n { operator: '/', description: 'Division' },\n { operator: '%', description: 'Modulo (remainder)' },\n ],\n comparisonOperators: [\n { operator: '==', description: 'Equal' },\n { operator: '!=', description: 'Not equal' },\n { operator: '>', description: 'Greater than' },\n { operator: '<', description: 'Less than' },\n { operator: '>=', description: 'Greater or equal' },\n { operator: '<=', description: 'Less or equal' },\n ],\n logicalOperators: [\n { operator: '&&', description: 'Logical AND' },\n { operator: '||', description: 'Logical OR' },\n { operator: '!', description: 'Logical NOT' },\n ],\n other: ['Parentheses: (a + b) * c', 'Unary minus: -value, a + -b'],\n },\n\n functions: {\n string: [\n {\n name: 'concat',\n description: 'Concatenate multiple values into a single string',\n signature: 'concat(value1, value2, ...)',\n returnType: 'string',\n examples: [\n 'concat(firstName, \" \", lastName) // \"John Doe\"',\n 'concat(\"Price: \", price, \" USD\") // \"Price: 100 USD\"',\n ],\n },\n {\n name: 'upper',\n description: 'Convert string to uppercase',\n signature: 'upper(text)',\n returnType: 'string',\n examples: ['upper(name) // \"HELLO\"'],\n },\n {\n name: 'lower',\n description: 'Convert string to lowercase',\n signature: 'lower(text)',\n returnType: 'string',\n examples: ['lower(name) // \"hello\"'],\n },\n {\n name: 'trim',\n description: 'Remove whitespace from both ends of a string',\n signature: 'trim(text)',\n returnType: 'string',\n examples: ['trim(name) // \"hello\" from \" hello \"'],\n },\n {\n name: 'left',\n description: 'Extract characters from the beginning of a string',\n signature: 'left(text, count)',\n returnType: 'string',\n examples: ['left(name, 3) // \"hel\" from \"hello\"'],\n },\n {\n name: 'right',\n description: 'Extract characters from the end of a string',\n signature: 'right(text, count)',\n returnType: 'string',\n examples: ['right(name, 3) // \"llo\" from \"hello\"'],\n },\n {\n name: 'replace',\n description: 'Replace first occurrence of a substring',\n signature: 'replace(text, search, replacement)',\n returnType: 'string',\n examples: ['replace(name, \"o\", \"0\") // \"hell0\" from \"hello\"'],\n },\n {\n name: 'join',\n description: 'Join array elements into a string',\n signature: 'join(array, separator?)',\n returnType: 'string',\n examples: ['join(tags) // \"a,b,c\"', 'join(tags, \" | \") // \"a | b | c\"'],\n },\n ],\n numeric: [\n {\n name: 'round',\n description: 'Round a number to specified decimal places',\n signature: 'round(number, decimals?)',\n returnType: 'number',\n examples: ['round(3.14159, 2) // 3.14', 'round(3.5) // 4'],\n },\n {\n name: 'floor',\n description: 'Round down to the nearest integer',\n signature: 'floor(number)',\n returnType: 'number',\n examples: ['floor(3.7) // 3'],\n },\n {\n name: 'ceil',\n description: 'Round up to the nearest integer',\n signature: 'ceil(number)',\n returnType: 'number',\n examples: ['ceil(3.2) // 4'],\n },\n {\n name: 'abs',\n description: 'Get the absolute value',\n signature: 'abs(number)',\n returnType: 'number',\n examples: ['abs(-5) // 5'],\n },\n {\n name: 'sqrt',\n description: 'Calculate the square root',\n signature: 'sqrt(number)',\n returnType: 'number',\n examples: ['sqrt(16) // 4'],\n },\n {\n name: 'pow',\n description: 'Raise a number to a power',\n signature: 'pow(base, exponent)',\n returnType: 'number',\n examples: ['pow(2, 3) // 8'],\n },\n {\n name: 'min',\n description: 'Get the minimum of multiple values',\n signature: 'min(value1, value2, ...)',\n returnType: 'number',\n examples: ['min(a, b, c) // smallest value'],\n },\n {\n name: 'max',\n description: 'Get the maximum of multiple values',\n signature: 'max(value1, value2, ...)',\n returnType: 'number',\n examples: ['max(a, b, c) // largest value'],\n },\n {\n name: 'log',\n description: 'Calculate the natural logarithm',\n signature: 'log(number)',\n returnType: 'number',\n examples: ['log(10) // 2.302...'],\n },\n {\n name: 'log10',\n description: 'Calculate the base-10 logarithm',\n signature: 'log10(number)',\n returnType: 'number',\n examples: ['log10(100) // 2'],\n },\n {\n name: 'exp',\n description: 'Calculate e raised to a power',\n signature: 'exp(number)',\n returnType: 'number',\n examples: ['exp(1) // 2.718...'],\n },\n {\n name: 'sign',\n description: 'Get the sign of a number (-1, 0, or 1)',\n signature: 'sign(number)',\n returnType: 'number',\n examples: ['sign(-5) // -1', 'sign(0) // 0', 'sign(5) // 1'],\n },\n {\n name: 'length',\n description: 'Get the length of a string or array',\n signature: 'length(value)',\n returnType: 'number',\n examples: ['length(name) // 5 from \"hello\"', 'length(items) // 3'],\n },\n ],\n boolean: [\n {\n name: 'and',\n description: 'Logical AND of two values',\n signature: 'and(a, b)',\n returnType: 'boolean',\n examples: ['and(isActive, hasPermission) // true if both true'],\n },\n {\n name: 'or',\n description: 'Logical OR of two values',\n signature: 'or(a, b)',\n returnType: 'boolean',\n examples: ['or(isAdmin, isOwner) // true if either true'],\n },\n {\n name: 'not',\n description: 'Logical NOT of a value',\n signature: 'not(value)',\n returnType: 'boolean',\n examples: ['not(isDeleted) // true if false'],\n },\n {\n name: 'contains',\n description: 'Check if a string contains a substring',\n signature: 'contains(text, search)',\n returnType: 'boolean',\n examples: ['contains(name, \"ell\") // true for \"hello\"'],\n },\n {\n name: 'startswith',\n description: 'Check if a string starts with a prefix',\n signature: 'startswith(text, prefix)',\n returnType: 'boolean',\n examples: ['startswith(name, \"hel\") // true for \"hello\"'],\n },\n {\n name: 'endswith',\n description: 'Check if a string ends with a suffix',\n signature: 'endswith(text, suffix)',\n returnType: 'boolean',\n examples: ['endswith(name, \"llo\") // true for \"hello\"'],\n },\n {\n name: 'isnull',\n description: 'Check if a value is null or undefined',\n signature: 'isnull(value)',\n returnType: 'boolean',\n examples: ['isnull(optionalField) // true if null/undefined'],\n },\n {\n name: 'includes',\n description: 'Check if an array contains a value',\n signature: 'includes(array, value)',\n returnType: 'boolean',\n examples: [\n 'includes(tags, \"featured\") // true if array contains value',\n ],\n },\n ],\n array: [\n {\n name: 'sum',\n description: 'Calculate the sum of array elements',\n signature: 'sum(array)',\n returnType: 'number',\n examples: ['sum(prices) // total of all prices'],\n },\n {\n name: 'avg',\n description: 'Calculate the average of array elements',\n signature: 'avg(array)',\n returnType: 'number',\n examples: ['avg(scores) // average score'],\n },\n {\n name: 'count',\n description: 'Get the number of elements in an array',\n signature: 'count(array)',\n returnType: 'number',\n examples: ['count(items) // number of items'],\n },\n {\n name: 'first',\n description: 'Get the first element of an array',\n signature: 'first(array)',\n returnType: 'any',\n examples: ['first(items) // first item'],\n },\n {\n name: 'last',\n description: 'Get the last element of an array',\n signature: 'last(array)',\n returnType: 'any',\n examples: ['last(items) // last item'],\n },\n ],\n conversion: [\n {\n name: 'tostring',\n description: 'Convert a value to string',\n signature: 'tostring(value)',\n returnType: 'string',\n examples: ['tostring(42) // \"42\"'],\n },\n {\n name: 'tonumber',\n description: 'Convert a value to number',\n signature: 'tonumber(value)',\n returnType: 'number',\n examples: ['tonumber(\"42\") // 42'],\n },\n {\n name: 'toboolean',\n description: 'Convert a value to boolean',\n signature: 'toboolean(value)',\n returnType: 'boolean',\n examples: ['toboolean(1) // true', 'toboolean(0) // false'],\n },\n ],\n conditional: [\n {\n name: 'if',\n description: 'Return one of two values based on a condition',\n signature: 'if(condition, valueIfTrue, valueIfFalse)',\n returnType: 'any',\n examples: [\n 'if(stock > 0, \"Available\", \"Out of Stock\")',\n 'if(price > 100, price * 0.9, price)',\n ],\n },\n {\n name: 'coalesce',\n description: 'Return the first non-null value',\n signature: 'coalesce(value1, value2, ...)',\n returnType: 'any',\n examples: ['coalesce(nickname, name, \"Anonymous\")'],\n },\n ],\n },\n\n features: [\n {\n name: 'simple_refs',\n description: 'Reference top-level fields by name',\n minVersion: '1.0',\n examples: ['price', 'quantity', 'baseDamage'],\n dependenciesExtracted: ['[\"price\"]', '[\"quantity\"]', '[\"baseDamage\"]'],\n },\n {\n name: 'arithmetic',\n description: 'Basic math operations (+, -, *, /)',\n minVersion: '1.0',\n examples: ['price * 1.1', 'a + b - c', 'quantity * price'],\n },\n {\n name: 'comparison',\n description: 'Compare values (>, <, >=, <=, ==, !=)',\n minVersion: '1.0',\n examples: ['price > 100', 'x == 10', 'quantity >= 5'],\n },\n {\n name: 'nested_path',\n description: 'Access nested object properties using dot notation',\n minVersion: '1.1',\n examples: ['stats.damage', 'user.profile.name', 'item.metadata.category'],\n dependenciesExtracted: ['[\"stats.damage\"]'],\n },\n {\n name: 'array_index',\n description:\n 'Access array elements by numeric index. Negative indices access from the end',\n minVersion: '1.1',\n examples: [\n 'items[0].price',\n 'inventory[1].quantity',\n 'items[-1].name // last element',\n 'items[-2].price // second to last',\n ],\n dependenciesExtracted: ['[\"items[0].price\"]', '[\"items[-1].name\"]'],\n },\n {\n name: 'root_path',\n description:\n 'Absolute path reference starting with /. Always resolves from root data, even inside array item formulas',\n minVersion: '1.1',\n examples: [\n '/taxRate',\n '/config.tax',\n 'price * (1 + /taxRate)',\n 'price * /config.multiplier',\n ],\n dependenciesExtracted: ['[\"/taxRate\"]', '[\"/config.tax\"]'],\n },\n {\n name: 'relative_path',\n description:\n 'Relative path reference starting with ../. Resolves from parent context (root data) when inside array item formulas',\n minVersion: '1.1',\n examples: [\n '../discount',\n '../settings.multiplier',\n 'price * (1 - ../discount)',\n 'price * ../settings.multiplier',\n ],\n dependenciesExtracted: ['[\"../discount\"]', '[\"../settings.multiplier\"]'],\n },\n {\n name: 'function_named_fields',\n description:\n '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',\n minVersion: '1.0',\n examples: [\n 'max(max, 0)',\n 'min(min, 100)',\n 'max(max - field.min, 0)',\n 'round(round * 2)',\n ],\n },\n ],\n\n versionDetection: [\n { feature: 'Simple refs, arithmetic, comparisons', minVersion: '1.0' },\n { feature: 'Function-named fields (max(max, 0))', minVersion: '1.0' },\n { feature: 'Nested paths (a.b)', minVersion: '1.1' },\n { feature: 'Array index ([0], [-1])', minVersion: '1.1' },\n { feature: 'Absolute paths (/field)', minVersion: '1.1' },\n { feature: 'Relative paths (../field)', minVersion: '1.1' },\n ],\n\n parseResult: {\n description:\n 'The parser automatically detects the minimum required version',\n interface: `interface ParseResult {\n ast: ASTNode; // Abstract syntax tree\n dependencies: string[]; // List of field dependencies\n features: string[]; // List of detected features\n minVersion: string; // Minimum required version (\"1.0\" or \"1.1\")\n}`,\n },\n\n examples: [\n {\n expression: 'price * quantity',\n description: 'Calculate total from price and quantity',\n result: 'number',\n },\n {\n expression: 'firstName + \" \" + lastName',\n description: 'Concatenate strings with space',\n result: 'string',\n },\n {\n expression: 'quantity > 0',\n description: 'Check if in stock',\n result: 'boolean',\n },\n {\n expression: 'if(stock > 0, \"Available\", \"Out of Stock\")',\n description: 'Conditional text based on stock',\n result: 'string',\n },\n {\n expression: 'price * (1 + taxRate)',\n description: 'Price with tax',\n result: 'number',\n },\n {\n expression: 'items[0].price + items[1].price',\n description: 'Sum first two item prices (v1.1)',\n result: 'number',\n },\n ],\n\n apiExamples: [\n {\n name: 'Simple Expression (v1.0)',\n description: 'Parse a basic arithmetic expression',\n code: `parseExpression('price * 1.1')\n// {\n// minVersion: \"1.0\",\n// features: [],\n// dependencies: [\"price\"]\n// }`,\n },\n {\n name: 'Nested Path (v1.1)',\n description: 'Parse expression with nested object access',\n code: `parseExpression('stats.damage * multiplier')\n// {\n// minVersion: \"1.1\",\n// features: [\"nested_path\"],\n// dependencies: [\"stats.damage\", \"multiplier\"]\n// }`,\n },\n {\n name: 'Array Access (v1.1)',\n description: 'Parse expression with array index access',\n code: `parseExpression('items[0].price + items[1].price')\n// {\n// minVersion: \"1.1\",\n// features: [\"array_index\", \"nested_path\"],\n// dependencies: [\"items[0].price\", \"items[1].price\"]\n// }`,\n },\n {\n name: 'Evaluate expressions',\n description: 'Execute formulas with context data',\n code: `evaluate('price * 1.1', { price: 100 })\n// 110\n\nevaluate('stats.damage', { stats: { damage: 50 } })\n// 50\n\nevaluate('items[0].price', { items: [{ price: 10 }] })\n// 10\n\nevaluate('price > 100', { price: 150 })\n// true\n\nevaluate('a + b * c', { a: 1, b: 2, c: 3 })\n// 7`,\n },\n {\n name: 'Function-named fields',\n description: 'Fields can have the same name as built-in functions',\n code: `// Built-in functions take precedence in function calls\nevaluate('max(max, 0)', { max: 10 })\n// 10 (max() function, then max field)\n\nevaluate('max(max - field.min, 0)', { max: 100, field: { min: 20 } })\n// 80\n\nevaluate('round(round * 2)', { round: 3.7 })\n// 7\n\n// Field named \"sum\" doesn't conflict with sum() function\nevaluate('sum(values) + sum', { values: [1, 2, 3], sum: 10 })\n// 16`,\n },\n {\n name: 'Evaluate with context (array items)',\n description:\n 'Use evaluateWithContext() for array item formulas with path resolution',\n code: `// Absolute path: /field always resolves from root\nevaluateWithContext('price * (1 + /taxRate)', {\n rootData: { taxRate: 0.1, items: [{ price: 100 }] },\n itemData: { price: 100 },\n currentPath: 'items[0]'\n})\n// 110\n\n// Nested absolute path\nevaluateWithContext('price * /config.multiplier', {\n rootData: { config: { multiplier: 1.5 }, items: [] },\n itemData: { price: 100 },\n currentPath: 'items[0]'\n})\n// 150\n\n// Relative path: ../field resolves from parent (root)\nevaluateWithContext('price * (1 - ../discount)', {\n rootData: { discount: 0.2, items: [] },\n itemData: { price: 100 },\n currentPath: 'items[0]'\n})\n// 80\n\n// itemData takes precedence over rootData for same field\nevaluateWithContext('value + 10', {\n rootData: { value: 100 },\n itemData: { value: 50 },\n currentPath: 'items[0]'\n})\n// 60`,\n },\n ],\n\n schemaUsage: {\n structure:\n '{ \"x-formula\": { \"version\": 1, \"expression\": \"...\" }, \"readOnly\": true }',\n fieldTypes: ['string', 'number', 'boolean'],\n rules: [\n 'Add x-formula to string, number, or boolean field schema',\n 'readOnly: true is REQUIRED for fields with x-formula',\n 'Expression must reference existing fields in the same table',\n 'Circular dependencies are not allowed (a references b, b references a)',\n ],\n },\n};\n"]}
@@ -57,6 +57,12 @@ declare function validateSyntax(expression: string): {
57
57
  position?: number;
58
58
  };
59
59
  declare function evaluate(expression: string, context: Record<string, unknown>): unknown;
60
+ interface EvaluateContextOptions {
61
+ rootData: Record<string, unknown>;
62
+ itemData?: Record<string, unknown>;
63
+ currentPath?: string;
64
+ }
65
+ declare function evaluateWithContext(expression: string, options: EvaluateContextOptions): unknown;
60
66
  type InferredType = 'number' | 'boolean' | 'string' | 'unknown';
61
67
  interface FieldTypes {
62
68
  [fieldName: string]: 'number' | 'string' | 'boolean' | 'object' | 'array';
@@ -87,11 +93,14 @@ interface XFormulaInput {
87
93
  interface SchemaProperty {
88
94
  type?: string;
89
95
  'x-formula'?: XFormulaInput;
96
+ properties?: Record<string, SchemaProperty>;
97
+ items?: SchemaProperty;
90
98
  [key: string]: unknown;
91
99
  }
92
100
  interface JsonSchema {
93
101
  type?: string;
94
102
  properties?: Record<string, SchemaProperty>;
103
+ items?: SchemaProperty;
95
104
  [key: string]: unknown;
96
105
  }
97
106
  interface ExtractedFormula {
@@ -113,4 +122,4 @@ interface SchemaValidationResult {
113
122
  declare function validateFormulaAgainstSchema(expression: string, fieldName: string, schema: JsonSchema): FormulaValidationError | null;
114
123
  declare function validateSchemaFormulas(schema: JsonSchema): SchemaValidationResult;
115
124
 
116
- export { type ASTNode as A, type ExtractedFormula as E, type FieldTypes as F, type InferredType as I, type JsonSchema as J, type ParseResult as P, type SyntaxValidationResult as S, type XFormula as X, parseExpression as a, type ParsedExpression as b, validateFormulaSyntax as c, extractSchemaFormulas as d, evaluate as e, validateFormulaAgainstSchema as f, validateSchemaFormulas as g, type FormulaValidationError as h, inferFormulaType as i, type SchemaValidationResult as j, type FormulaMinorVersion as k, type FormulaFeature as l, type FormulaAnalysis as m, type PathSegment as n, type ParsedPath as o, parseFormula as p, type PathValidationResult as q, type FormulaContext as r, type FormulaResult as s, validateSyntax as v };
125
+ export { type ASTNode as A, type EvaluateContextOptions as E, type FieldTypes as F, type InferredType as I, type JsonSchema as J, type ParseResult as P, type SyntaxValidationResult as S, type XFormula as X, evaluateWithContext as a, parseExpression as b, type ParsedExpression as c, validateFormulaSyntax as d, evaluate as e, extractSchemaFormulas as f, type ExtractedFormula as g, validateFormulaAgainstSchema as h, inferFormulaType as i, validateSchemaFormulas as j, type FormulaValidationError as k, type SchemaValidationResult as l, type FormulaMinorVersion as m, type FormulaFeature as n, type FormulaAnalysis as o, parseFormula as p, type PathSegment as q, type ParsedPath as r, type PathValidationResult as s, type FormulaContext as t, type FormulaResult as u, validateSyntax as v };
@@ -57,6 +57,12 @@ declare function validateSyntax(expression: string): {
57
57
  position?: number;
58
58
  };
59
59
  declare function evaluate(expression: string, context: Record<string, unknown>): unknown;
60
+ interface EvaluateContextOptions {
61
+ rootData: Record<string, unknown>;
62
+ itemData?: Record<string, unknown>;
63
+ currentPath?: string;
64
+ }
65
+ declare function evaluateWithContext(expression: string, options: EvaluateContextOptions): unknown;
60
66
  type InferredType = 'number' | 'boolean' | 'string' | 'unknown';
61
67
  interface FieldTypes {
62
68
  [fieldName: string]: 'number' | 'string' | 'boolean' | 'object' | 'array';
@@ -87,11 +93,14 @@ interface XFormulaInput {
87
93
  interface SchemaProperty {
88
94
  type?: string;
89
95
  'x-formula'?: XFormulaInput;
96
+ properties?: Record<string, SchemaProperty>;
97
+ items?: SchemaProperty;
90
98
  [key: string]: unknown;
91
99
  }
92
100
  interface JsonSchema {
93
101
  type?: string;
94
102
  properties?: Record<string, SchemaProperty>;
103
+ items?: SchemaProperty;
95
104
  [key: string]: unknown;
96
105
  }
97
106
  interface ExtractedFormula {
@@ -113,4 +122,4 @@ interface SchemaValidationResult {
113
122
  declare function validateFormulaAgainstSchema(expression: string, fieldName: string, schema: JsonSchema): FormulaValidationError | null;
114
123
  declare function validateSchemaFormulas(schema: JsonSchema): SchemaValidationResult;
115
124
 
116
- export { type ASTNode as A, type ExtractedFormula as E, type FieldTypes as F, type InferredType as I, type JsonSchema as J, type ParseResult as P, type SyntaxValidationResult as S, type XFormula as X, parseExpression as a, type ParsedExpression as b, validateFormulaSyntax as c, extractSchemaFormulas as d, evaluate as e, validateFormulaAgainstSchema as f, validateSchemaFormulas as g, type FormulaValidationError as h, inferFormulaType as i, type SchemaValidationResult as j, type FormulaMinorVersion as k, type FormulaFeature as l, type FormulaAnalysis as m, type PathSegment as n, type ParsedPath as o, parseFormula as p, type PathValidationResult as q, type FormulaContext as r, type FormulaResult as s, validateSyntax as v };
125
+ export { type ASTNode as A, type EvaluateContextOptions as E, type FieldTypes as F, type InferredType as I, type JsonSchema as J, type ParseResult as P, type SyntaxValidationResult as S, type XFormula as X, evaluateWithContext as a, parseExpression as b, type ParsedExpression as c, validateFormulaSyntax as d, evaluate as e, extractSchemaFormulas as f, type ExtractedFormula as g, validateFormulaAgainstSchema as h, inferFormulaType as i, validateSchemaFormulas as j, type FormulaValidationError as k, type SchemaValidationResult as l, type FormulaMinorVersion as m, type FormulaFeature as n, type FormulaAnalysis as o, parseFormula as p, type PathSegment as q, type ParsedPath as r, type PathValidationResult as s, type FormulaContext as t, type FormulaResult as u, validateSyntax as v };