@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.
@@ -33,10 +33,301 @@ var formulaSpec = {
33
33
  { operator: "||", description: "Logical OR" },
34
34
  { operator: "!", description: "Logical NOT" }
35
35
  ],
36
- other: [
37
- "Parentheses: (a + b) * c",
38
- "Ternary: condition ? valueIfTrue : valueIfFalse",
39
- "Unary minus: -value, a + -b"
36
+ other: ["Parentheses: (a + b) * c", "Unary minus: -value, a + -b"]
37
+ },
38
+ functions: {
39
+ string: [
40
+ {
41
+ name: "concat",
42
+ description: "Concatenate multiple values into a single string",
43
+ signature: "concat(value1, value2, ...)",
44
+ returnType: "string",
45
+ examples: [
46
+ 'concat(firstName, " ", lastName) // "John Doe"',
47
+ 'concat("Price: ", price, " USD") // "Price: 100 USD"'
48
+ ]
49
+ },
50
+ {
51
+ name: "upper",
52
+ description: "Convert string to uppercase",
53
+ signature: "upper(text)",
54
+ returnType: "string",
55
+ examples: ['upper(name) // "HELLO"']
56
+ },
57
+ {
58
+ name: "lower",
59
+ description: "Convert string to lowercase",
60
+ signature: "lower(text)",
61
+ returnType: "string",
62
+ examples: ['lower(name) // "hello"']
63
+ },
64
+ {
65
+ name: "trim",
66
+ description: "Remove whitespace from both ends of a string",
67
+ signature: "trim(text)",
68
+ returnType: "string",
69
+ examples: ['trim(name) // "hello" from " hello "']
70
+ },
71
+ {
72
+ name: "left",
73
+ description: "Extract characters from the beginning of a string",
74
+ signature: "left(text, count)",
75
+ returnType: "string",
76
+ examples: ['left(name, 3) // "hel" from "hello"']
77
+ },
78
+ {
79
+ name: "right",
80
+ description: "Extract characters from the end of a string",
81
+ signature: "right(text, count)",
82
+ returnType: "string",
83
+ examples: ['right(name, 3) // "llo" from "hello"']
84
+ },
85
+ {
86
+ name: "replace",
87
+ description: "Replace first occurrence of a substring",
88
+ signature: "replace(text, search, replacement)",
89
+ returnType: "string",
90
+ examples: ['replace(name, "o", "0") // "hell0" from "hello"']
91
+ },
92
+ {
93
+ name: "join",
94
+ description: "Join array elements into a string",
95
+ signature: "join(array, separator?)",
96
+ returnType: "string",
97
+ examples: ['join(tags) // "a,b,c"', 'join(tags, " | ") // "a | b | c"']
98
+ }
99
+ ],
100
+ numeric: [
101
+ {
102
+ name: "round",
103
+ description: "Round a number to specified decimal places",
104
+ signature: "round(number, decimals?)",
105
+ returnType: "number",
106
+ examples: ["round(3.14159, 2) // 3.14", "round(3.5) // 4"]
107
+ },
108
+ {
109
+ name: "floor",
110
+ description: "Round down to the nearest integer",
111
+ signature: "floor(number)",
112
+ returnType: "number",
113
+ examples: ["floor(3.7) // 3"]
114
+ },
115
+ {
116
+ name: "ceil",
117
+ description: "Round up to the nearest integer",
118
+ signature: "ceil(number)",
119
+ returnType: "number",
120
+ examples: ["ceil(3.2) // 4"]
121
+ },
122
+ {
123
+ name: "abs",
124
+ description: "Get the absolute value",
125
+ signature: "abs(number)",
126
+ returnType: "number",
127
+ examples: ["abs(-5) // 5"]
128
+ },
129
+ {
130
+ name: "sqrt",
131
+ description: "Calculate the square root",
132
+ signature: "sqrt(number)",
133
+ returnType: "number",
134
+ examples: ["sqrt(16) // 4"]
135
+ },
136
+ {
137
+ name: "pow",
138
+ description: "Raise a number to a power",
139
+ signature: "pow(base, exponent)",
140
+ returnType: "number",
141
+ examples: ["pow(2, 3) // 8"]
142
+ },
143
+ {
144
+ name: "min",
145
+ description: "Get the minimum of multiple values",
146
+ signature: "min(value1, value2, ...)",
147
+ returnType: "number",
148
+ examples: ["min(a, b, c) // smallest value"]
149
+ },
150
+ {
151
+ name: "max",
152
+ description: "Get the maximum of multiple values",
153
+ signature: "max(value1, value2, ...)",
154
+ returnType: "number",
155
+ examples: ["max(a, b, c) // largest value"]
156
+ },
157
+ {
158
+ name: "log",
159
+ description: "Calculate the natural logarithm",
160
+ signature: "log(number)",
161
+ returnType: "number",
162
+ examples: ["log(10) // 2.302..."]
163
+ },
164
+ {
165
+ name: "log10",
166
+ description: "Calculate the base-10 logarithm",
167
+ signature: "log10(number)",
168
+ returnType: "number",
169
+ examples: ["log10(100) // 2"]
170
+ },
171
+ {
172
+ name: "exp",
173
+ description: "Calculate e raised to a power",
174
+ signature: "exp(number)",
175
+ returnType: "number",
176
+ examples: ["exp(1) // 2.718..."]
177
+ },
178
+ {
179
+ name: "sign",
180
+ description: "Get the sign of a number (-1, 0, or 1)",
181
+ signature: "sign(number)",
182
+ returnType: "number",
183
+ examples: ["sign(-5) // -1", "sign(0) // 0", "sign(5) // 1"]
184
+ },
185
+ {
186
+ name: "length",
187
+ description: "Get the length of a string or array",
188
+ signature: "length(value)",
189
+ returnType: "number",
190
+ examples: ['length(name) // 5 from "hello"', "length(items) // 3"]
191
+ }
192
+ ],
193
+ boolean: [
194
+ {
195
+ name: "and",
196
+ description: "Logical AND of two values",
197
+ signature: "and(a, b)",
198
+ returnType: "boolean",
199
+ examples: ["and(isActive, hasPermission) // true if both true"]
200
+ },
201
+ {
202
+ name: "or",
203
+ description: "Logical OR of two values",
204
+ signature: "or(a, b)",
205
+ returnType: "boolean",
206
+ examples: ["or(isAdmin, isOwner) // true if either true"]
207
+ },
208
+ {
209
+ name: "not",
210
+ description: "Logical NOT of a value",
211
+ signature: "not(value)",
212
+ returnType: "boolean",
213
+ examples: ["not(isDeleted) // true if false"]
214
+ },
215
+ {
216
+ name: "contains",
217
+ description: "Check if a string contains a substring",
218
+ signature: "contains(text, search)",
219
+ returnType: "boolean",
220
+ examples: ['contains(name, "ell") // true for "hello"']
221
+ },
222
+ {
223
+ name: "startswith",
224
+ description: "Check if a string starts with a prefix",
225
+ signature: "startswith(text, prefix)",
226
+ returnType: "boolean",
227
+ examples: ['startswith(name, "hel") // true for "hello"']
228
+ },
229
+ {
230
+ name: "endswith",
231
+ description: "Check if a string ends with a suffix",
232
+ signature: "endswith(text, suffix)",
233
+ returnType: "boolean",
234
+ examples: ['endswith(name, "llo") // true for "hello"']
235
+ },
236
+ {
237
+ name: "isnull",
238
+ description: "Check if a value is null or undefined",
239
+ signature: "isnull(value)",
240
+ returnType: "boolean",
241
+ examples: ["isnull(optionalField) // true if null/undefined"]
242
+ },
243
+ {
244
+ name: "includes",
245
+ description: "Check if an array contains a value",
246
+ signature: "includes(array, value)",
247
+ returnType: "boolean",
248
+ examples: [
249
+ 'includes(tags, "featured") // true if array contains value'
250
+ ]
251
+ }
252
+ ],
253
+ array: [
254
+ {
255
+ name: "sum",
256
+ description: "Calculate the sum of array elements",
257
+ signature: "sum(array)",
258
+ returnType: "number",
259
+ examples: ["sum(prices) // total of all prices"]
260
+ },
261
+ {
262
+ name: "avg",
263
+ description: "Calculate the average of array elements",
264
+ signature: "avg(array)",
265
+ returnType: "number",
266
+ examples: ["avg(scores) // average score"]
267
+ },
268
+ {
269
+ name: "count",
270
+ description: "Get the number of elements in an array",
271
+ signature: "count(array)",
272
+ returnType: "number",
273
+ examples: ["count(items) // number of items"]
274
+ },
275
+ {
276
+ name: "first",
277
+ description: "Get the first element of an array",
278
+ signature: "first(array)",
279
+ returnType: "any",
280
+ examples: ["first(items) // first item"]
281
+ },
282
+ {
283
+ name: "last",
284
+ description: "Get the last element of an array",
285
+ signature: "last(array)",
286
+ returnType: "any",
287
+ examples: ["last(items) // last item"]
288
+ }
289
+ ],
290
+ conversion: [
291
+ {
292
+ name: "tostring",
293
+ description: "Convert a value to string",
294
+ signature: "tostring(value)",
295
+ returnType: "string",
296
+ examples: ['tostring(42) // "42"']
297
+ },
298
+ {
299
+ name: "tonumber",
300
+ description: "Convert a value to number",
301
+ signature: "tonumber(value)",
302
+ returnType: "number",
303
+ examples: ['tonumber("42") // 42']
304
+ },
305
+ {
306
+ name: "toboolean",
307
+ description: "Convert a value to boolean",
308
+ signature: "toboolean(value)",
309
+ returnType: "boolean",
310
+ examples: ["toboolean(1) // true", "toboolean(0) // false"]
311
+ }
312
+ ],
313
+ conditional: [
314
+ {
315
+ name: "if",
316
+ description: "Return one of two values based on a condition",
317
+ signature: "if(condition, valueIfTrue, valueIfFalse)",
318
+ returnType: "any",
319
+ examples: [
320
+ 'if(stock > 0, "Available", "Out of Stock")',
321
+ "if(price > 100, price * 0.9, price)"
322
+ ]
323
+ },
324
+ {
325
+ name: "coalesce",
326
+ description: "Return the first non-null value",
327
+ signature: "coalesce(value1, value2, ...)",
328
+ returnType: "any",
329
+ examples: ['coalesce(nickname, name, "Anonymous")']
330
+ }
40
331
  ]
41
332
  },
42
333
  features: [
@@ -77,12 +368,50 @@ var formulaSpec = {
77
368
  "items[-2].price // second to last"
78
369
  ],
79
370
  dependenciesExtracted: ['["items[0].price"]', '["items[-1].name"]']
371
+ },
372
+ {
373
+ name: "root_path",
374
+ description: "Absolute path reference starting with /. Always resolves from root data, even inside array item formulas",
375
+ minVersion: "1.1",
376
+ examples: [
377
+ "/taxRate",
378
+ "/config.tax",
379
+ "price * (1 + /taxRate)",
380
+ "price * /config.multiplier"
381
+ ],
382
+ dependenciesExtracted: ['["/taxRate"]', '["/config.tax"]']
383
+ },
384
+ {
385
+ name: "relative_path",
386
+ description: "Relative path reference starting with ../. Resolves from parent context (root data) when inside array item formulas",
387
+ minVersion: "1.1",
388
+ examples: [
389
+ "../discount",
390
+ "../settings.multiplier",
391
+ "price * (1 - ../discount)",
392
+ "price * ../settings.multiplier"
393
+ ],
394
+ dependenciesExtracted: ['["../discount"]', '["../settings.multiplier"]']
395
+ },
396
+ {
397
+ name: "function_named_fields",
398
+ 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",
399
+ minVersion: "1.0",
400
+ examples: [
401
+ "max(max, 0)",
402
+ "min(min, 100)",
403
+ "max(max - field.min, 0)",
404
+ "round(round * 2)"
405
+ ]
80
406
  }
81
407
  ],
82
408
  versionDetection: [
83
409
  { feature: "Simple refs, arithmetic, comparisons", minVersion: "1.0" },
410
+ { feature: "Function-named fields (max(max, 0))", minVersion: "1.0" },
84
411
  { feature: "Nested paths (a.b)", minVersion: "1.1" },
85
- { feature: "Array index ([0], [-1])", minVersion: "1.1" }
412
+ { feature: "Array index ([0], [-1])", minVersion: "1.1" },
413
+ { feature: "Absolute paths (/field)", minVersion: "1.1" },
414
+ { feature: "Relative paths (../field)", minVersion: "1.1" }
86
415
  ],
87
416
  parseResult: {
88
417
  description: "The parser automatically detects the minimum required version",
@@ -110,7 +439,7 @@ var formulaSpec = {
110
439
  result: "boolean"
111
440
  },
112
441
  {
113
- expression: 'stock > 0 ? "Available" : "Out of Stock"',
442
+ expression: 'if(stock > 0, "Available", "Out of Stock")',
114
443
  description: "Conditional text based on stock",
115
444
  result: "string"
116
445
  },
@@ -173,6 +502,58 @@ evaluate('price > 100', { price: 150 })
173
502
 
174
503
  evaluate('a + b * c', { a: 1, b: 2, c: 3 })
175
504
  // 7`
505
+ },
506
+ {
507
+ name: "Function-named fields",
508
+ description: "Fields can have the same name as built-in functions",
509
+ code: `// Built-in functions take precedence in function calls
510
+ evaluate('max(max, 0)', { max: 10 })
511
+ // 10 (max() function, then max field)
512
+
513
+ evaluate('max(max - field.min, 0)', { max: 100, field: { min: 20 } })
514
+ // 80
515
+
516
+ evaluate('round(round * 2)', { round: 3.7 })
517
+ // 7
518
+
519
+ // Field named "sum" doesn't conflict with sum() function
520
+ evaluate('sum(values) + sum', { values: [1, 2, 3], sum: 10 })
521
+ // 16`
522
+ },
523
+ {
524
+ name: "Evaluate with context (array items)",
525
+ description: "Use evaluateWithContext() for array item formulas with path resolution",
526
+ code: `// Absolute path: /field always resolves from root
527
+ evaluateWithContext('price * (1 + /taxRate)', {
528
+ rootData: { taxRate: 0.1, items: [{ price: 100 }] },
529
+ itemData: { price: 100 },
530
+ currentPath: 'items[0]'
531
+ })
532
+ // 110
533
+
534
+ // Nested absolute path
535
+ evaluateWithContext('price * /config.multiplier', {
536
+ rootData: { config: { multiplier: 1.5 }, items: [] },
537
+ itemData: { price: 100 },
538
+ currentPath: 'items[0]'
539
+ })
540
+ // 150
541
+
542
+ // Relative path: ../field resolves from parent (root)
543
+ evaluateWithContext('price * (1 - ../discount)', {
544
+ rootData: { discount: 0.2, items: [] },
545
+ itemData: { price: 100 },
546
+ currentPath: 'items[0]'
547
+ })
548
+ // 80
549
+
550
+ // itemData takes precedence over rootData for same field
551
+ evaluateWithContext('value + 10', {
552
+ rootData: { value: 100 },
553
+ itemData: { value: 50 },
554
+ currentPath: 'items[0]'
555
+ })
556
+ // 60`
176
557
  }
177
558
  ],
178
559
  schemaUsage: {
@@ -182,8 +563,7 @@ evaluate('a + b * c', { a: 1, b: 2, c: 3 })
182
563
  "Add x-formula to string, number, or boolean field schema",
183
564
  "readOnly: true is REQUIRED for fields with x-formula",
184
565
  "Expression must reference existing fields in the same table",
185
- "Circular dependencies are not allowed (a references b, b references a)",
186
- "Referenced fields must exist before the formula field in schema order"
566
+ "Circular dependencies are not allowed (a references b, b references a)"
187
567
  ]
188
568
  }
189
569
  };
@@ -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,M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A;AAAA,MACA,6DAAA;AAAA,MACA,wEAAA;AAAA,MACA;AAAA;AACF;AAEJ","file":"formula-spec.cjs","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,M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AAA,EAAM,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.cjs","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"]}
@@ -1,3 +1,10 @@
1
+ interface FunctionSpec {
2
+ name: string;
3
+ description: string;
4
+ signature: string;
5
+ returnType: 'string' | 'number' | 'boolean' | 'any';
6
+ examples: string[];
7
+ }
1
8
  interface FormulaSpec {
2
9
  version: string;
3
10
  description: string;
@@ -17,6 +24,14 @@ interface FormulaSpec {
17
24
  }[];
18
25
  other: string[];
19
26
  };
27
+ functions: {
28
+ string: FunctionSpec[];
29
+ numeric: FunctionSpec[];
30
+ boolean: FunctionSpec[];
31
+ array: FunctionSpec[];
32
+ conversion: FunctionSpec[];
33
+ conditional: FunctionSpec[];
34
+ };
20
35
  features: {
21
36
  name: string;
22
37
  description: string;
@@ -50,4 +65,4 @@ interface FormulaSpec {
50
65
  }
51
66
  declare const formulaSpec: FormulaSpec;
52
67
 
53
- export { type FormulaSpec, formulaSpec };
68
+ export { type FormulaSpec, type FunctionSpec, formulaSpec };
@@ -1,3 +1,10 @@
1
+ interface FunctionSpec {
2
+ name: string;
3
+ description: string;
4
+ signature: string;
5
+ returnType: 'string' | 'number' | 'boolean' | 'any';
6
+ examples: string[];
7
+ }
1
8
  interface FormulaSpec {
2
9
  version: string;
3
10
  description: string;
@@ -17,6 +24,14 @@ interface FormulaSpec {
17
24
  }[];
18
25
  other: string[];
19
26
  };
27
+ functions: {
28
+ string: FunctionSpec[];
29
+ numeric: FunctionSpec[];
30
+ boolean: FunctionSpec[];
31
+ array: FunctionSpec[];
32
+ conversion: FunctionSpec[];
33
+ conditional: FunctionSpec[];
34
+ };
20
35
  features: {
21
36
  name: string;
22
37
  description: string;
@@ -50,4 +65,4 @@ interface FormulaSpec {
50
65
  }
51
66
  declare const formulaSpec: FormulaSpec;
52
67
 
53
- export { type FormulaSpec, formulaSpec };
68
+ export { type FormulaSpec, type FunctionSpec, formulaSpec };