@revisium/formula 0.3.0 → 0.5.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/README.md CHANGED
@@ -46,6 +46,13 @@ evaluate('items[0].price + items[1].price', { items: [{ price: 10 }, { price: 20
46
46
  evaluate('price > 100', { price: 150 });
47
47
  // true
48
48
 
49
+ // Fields named like functions
50
+ evaluate('max(max, 0)', { max: 10 });
51
+ // 10 (max() function is called with field "max" as argument)
52
+
53
+ evaluate('max(max - field.min, 0)', { max: 100, field: { min: 20 } });
54
+ // 80
55
+
49
56
  // Type inference
50
57
  import { inferFormulaType } from '@revisium/formula';
51
58
 
@@ -72,6 +79,25 @@ validateFormulaAgainstSchema('price * quantity', 'total', schema);
72
79
 
73
80
  validateFormulaAgainstSchema('price > 100', 'total', schema);
74
81
  // { field: 'total', error: "Type mismatch: formula returns 'boolean' but field expects 'number'" }
82
+
83
+ // Array item formulas with path resolution
84
+ import { evaluateWithContext } from '@revisium/formula';
85
+
86
+ // Absolute path: /field always resolves from root data
87
+ evaluateWithContext('price * (1 + /taxRate)', {
88
+ rootData: { taxRate: 0.1, items: [{ price: 100 }] },
89
+ itemData: { price: 100 },
90
+ currentPath: 'items[0]'
91
+ });
92
+ // 110
93
+
94
+ // Relative path: ../field resolves from parent (root)
95
+ evaluateWithContext('price * (1 - ../discount)', {
96
+ rootData: { discount: 0.2, items: [] },
97
+ itemData: { price: 100 },
98
+ currentPath: 'items[0]'
99
+ });
100
+ // 80
75
101
  ```
76
102
 
77
103
  ## API
@@ -83,6 +109,7 @@ validateFormulaAgainstSchema('price > 100', 'total', schema);
83
109
  | `parseFormula` | Low-level parser returning AST, dependencies, features |
84
110
  | `validateSyntax` | Validate expression syntax |
85
111
  | `evaluate` | Evaluate expression with context |
112
+ | `evaluateWithContext` | Evaluate with automatic `/` and `../` path resolution |
86
113
  | `inferFormulaType` | Infer return type of expression |
87
114
 
88
115
  ### Expression API
@@ -116,14 +143,19 @@ validateFormulaAgainstSchema('price > 100', 'total', schema);
116
143
  | `obj.field` | Nested object | `stats.damage` |
117
144
  | `arr[N]` | Array index | `items[0].price` |
118
145
  | `arr[-1]` | Last element | `items[-1]` |
146
+ | `/field` | Absolute path (from root) | `/taxRate`, `/config.tax` |
147
+ | `../field` | Relative path (parent scope) | `../discount`, `../settings.multiplier` |
119
148
 
120
149
  ## Version Detection
121
150
 
122
151
  | Feature | Min Version |
123
152
  |---------|-------------|
124
153
  | Simple refs (`field`) | 1.0 |
154
+ | Function-named fields | 1.0 |
125
155
  | Nested paths (`a.b`) | 1.1 |
126
156
  | Array index (`[0]`, `[-1]`) | 1.1 |
157
+ | Absolute paths (`/field`) | 1.1 |
158
+ | Relative paths (`../field`) | 1.1 |
127
159
 
128
160
  ## License
129
161