|
| 1 | +# TypeScript Port Enhancements |
| 2 | + |
| 3 | +This is a modern TypeScript port of the expr-eval library, completely rewritten with contemporary build tools and development practices. Originally based on [expr-eval 2.0.2](http://silentmatt.com/javascript-expression-evaluator/), this version has been restructured with a modular architecture, TypeScript support, and comprehensive testing using Vitest. The library almost maintains backward compatibility while providing enhanced features and improved maintainability. |
| 4 | + |
| 5 | +This port adds the following enhancements over the original: |
| 6 | + |
| 7 | +## Support for json() function |
| 8 | + |
| 9 | +This will return a JSON string: |
| 10 | + |
| 11 | +```js |
| 12 | +json([1, 2, 3]) |
| 13 | +``` |
| 14 | + |
| 15 | +## Support for undefined |
| 16 | + |
| 17 | +The concept of JavaScript's undefined has been added to the parser. |
| 18 | + |
| 19 | +### undefined keyword |
| 20 | + |
| 21 | +The undefined keyword has been added to the parser allowing it to be used in expressions. |
| 22 | + |
| 23 | +```js |
| 24 | +x > 3 ? undefined : x |
| 25 | +x == undefined ? 1 : 2 |
| 26 | +``` |
| 27 | + |
| 28 | +### Setting expression variables to undefined |
| 29 | + |
| 30 | +If you set a local variable to undefined, expr-eval would generate an error saying that your variable was unrecognized. |
| 31 | + |
| 32 | +For example: |
| 33 | + |
| 34 | +```js |
| 35 | +/* myCustomFn() returns undefined */ |
| 36 | +x = myCustomFn(); x > 3 |
| 37 | +/* Error: unrecognized variable: x */ |
| 38 | +``` |
| 39 | + |
| 40 | +This has been fixed, you can now set expression variables to undefined and they will resolve correctly. |
| 41 | + |
| 42 | +### Operators/functions gracefully support undefined |
| 43 | + |
| 44 | +All operators and built-in functions have been extended to gracefully support undefined. Generally speaking if one of the input values is undefined then the operator/function returns undefined. So `2 + undefined` is `undefined`, `max(0, 1, undefined)` is `undefined`, etc. |
| 45 | + |
| 46 | +Logical operators act just like JavaScript, so `3 > undefined` is `false`. |
| 47 | + |
| 48 | +## Coalesce Operator |
| 49 | + |
| 50 | +The coalesce operator `??` has been added; `x ?? y` will evaluate to y if x is: |
| 51 | + |
| 52 | +* `undefined` |
| 53 | +* `null` |
| 54 | +* Infinity (divide by zero) |
| 55 | +* NaN |
| 56 | + |
| 57 | +Examples: |
| 58 | + |
| 59 | +```js |
| 60 | +var parser = new Parser(); |
| 61 | +var obj = { x: undefined, y: 10, z: 0 }; |
| 62 | +parser.evaluate('x ?? 0', obj); // 0 |
| 63 | +parser.evaluate('y ?? 0', obj); // 10 |
| 64 | +parser.evaluate('x ?? 1 * 3', obj); // (undefined ?? 1) * 3 = 3 |
| 65 | +parser.evaluate('y ?? 1 * 3', obj); // (10 ?? 1) * 3 = 30 |
| 66 | +parser.evaluate('10 / z', obj); // Infinity |
| 67 | +parser.evaluate('10 / z ?? 0', obj); // 0 |
| 68 | +parser.evaluate('sqrt -1'); // NaN |
| 69 | +parser.evaluate('sqrt -1 ?? 0'); // 0 |
| 70 | +``` |
| 71 | + |
| 72 | +## Not In Operator |
| 73 | + |
| 74 | +The `not in` operator has been added. |
| 75 | + |
| 76 | +`"a" not in ["a", "b", "c"]` |
| 77 | + |
| 78 | +is equivalent to |
| 79 | + |
| 80 | +`not ("a" in ["a", "b", "c"])` |
| 81 | + |
| 82 | +## Optional Chaining for Property Access |
| 83 | + |
| 84 | +Structure/array property references now act like `?.`, meaning if the entire property chain does not exist then instead of throwing an error the value of the property is undefined. |
| 85 | + |
| 86 | +For example: |
| 87 | + |
| 88 | +```js |
| 89 | +var parser = new Parser(); |
| 90 | +var obj = { thingy: { array: [{ value: 10 }] } }; |
| 91 | +parser.evaluate('thingy.array[0].value', obj); // 10 |
| 92 | +parser.evaluate('thingy.array[1].value', obj); // undefined |
| 93 | +parser.evaluate('thingy.doesNotExist[0].childArray[1].notHere.alsoNotHere', obj); // undefined |
| 94 | +parser.evaluate('thingy.array[0].value.doesNotExist', obj); // undefined |
| 95 | +``` |
| 96 | + |
| 97 | +This can be combined with the coalesce operator to gracefully fall back on a default value if some part of a long property reference is `undefined`. |
| 98 | + |
| 99 | +```js |
| 100 | +var parser = new Parser(); |
| 101 | +var obj = { thingy: { array: [{ value: 10 }] } }; |
| 102 | +parser.evaluate('thingy.array[1].value ?? 0', obj); // 0 |
| 103 | +``` |
| 104 | + |
| 105 | +## String Concatenation Using + |
| 106 | + |
| 107 | +The + operator can now be used to concatenate strings. |
| 108 | + |
| 109 | +```js |
| 110 | +var parser = new Parser(); |
| 111 | +var obj = { thingy: { array: [{ value: 10 }] } }; |
| 112 | +parser.evaluate('"abc" + "def" + "ghi"', obj); // 'abcdefghi' |
| 113 | +``` |
| 114 | + |
| 115 | +## Support for Promises in Custom Functions |
| 116 | + |
| 117 | +Custom functions can return promises. When this happens evaluate will return a promise that when resolved contains the expression value. |
| 118 | + |
| 119 | +```js |
| 120 | +const parser = new Parser(); |
| 121 | + |
| 122 | +parser.functions.doIt = value => value + value; |
| 123 | +parser.evaluate('doIt(2) + 3'); // 7 |
| 124 | + |
| 125 | +parser.functions.doIt = value => |
| 126 | + new Promise((resolve) => setTimeout(() => resolve(value + value), 100)); |
| 127 | +await parser.evaluate('doIt(2) + 3'); // 7 |
| 128 | +``` |
| 129 | + |
| 130 | +## Support for Custom Variable Name Resolution |
| 131 | + |
| 132 | +Custom logic can be provided to resolve unrecognized variable names. The parser has a resolve callback that will be called any time a variable name is not recognized. This can return an object that either indicates that the variable name is an alias for another variable or it can return the variable value. |
| 133 | + |
| 134 | +```js |
| 135 | +const parser = new Parser(); |
| 136 | +const obj = { variables: { a: 5, b: 1 } }; |
| 137 | +parser.resolve = token => token === '$v' ? { alias: 'variables' } : undefined; |
| 138 | +parser.evaluate('$v.a + variables.b', obj); // 6 |
| 139 | + |
| 140 | +parser.resolve = token => |
| 141 | + token.startsWith('$') ? { value: obj.variables[token.substring(1)] } : undefined; |
| 142 | +assert.strictEqual(parser.evaluate('$a + $b'), 6); |
| 143 | +``` |
| 144 | + |
| 145 | +## SQL Style Case Blocks |
| 146 | + |
| 147 | +> **NOTE:** `toJSFunction()` is not supported for expressions that use case blocks. |
| 148 | +
|
| 149 | +SQL style case blocks are now supported, for both cases which evaluate a value against other values (a switch style case) and cases which test for the first truthy when (if/else/if style cases). |
| 150 | + |
| 151 | +### Switch-style case |
| 152 | + |
| 153 | +```js |
| 154 | +const parser = new Parser(); |
| 155 | +const expr = ` |
| 156 | + case x |
| 157 | + when 1 then 'one' |
| 158 | + when 1+1 then 'two' |
| 159 | + when 1+1+1 then 'three' |
| 160 | + else 'too-big' |
| 161 | + end |
| 162 | +`; |
| 163 | +parser.evaluate(expr, { x: 1 }); // 'one' |
| 164 | +parser.evaluate(expr, { x: 2 }); // 'two' |
| 165 | +parser.evaluate(expr, { x: 3 }); // 'three' |
| 166 | +parser.evaluate(expr, { x: 4 }); // 'too-big' |
| 167 | +``` |
| 168 | + |
| 169 | +### If/else-style case |
| 170 | + |
| 171 | +```js |
| 172 | +const parser = new Parser(); |
| 173 | +const expr = ` |
| 174 | + case |
| 175 | + when x == 1 then 'one' |
| 176 | + when x == 1+1 then 'two' |
| 177 | + when x == 1+1+1 then 'three' |
| 178 | + else 'too-big' |
| 179 | + end |
| 180 | +`; |
| 181 | +parser.evaluate(expr, { x: 1 }); // 'one' |
| 182 | +parser.evaluate(expr, { x: 2 }); // 'two' |
| 183 | +parser.evaluate(expr, { x: 3 }); // 'three' |
| 184 | +parser.evaluate(expr, { x: 4 }); // 'too-big' |
| 185 | +``` |
| 186 | + |
| 187 | +## Object Construction |
| 188 | + |
| 189 | +Objects can be created using JavaScript syntax. This allows for expressions that return object values and for object arguments to be passed to custom functions. |
| 190 | + |
| 191 | +```js |
| 192 | +const parser = new Parser(); |
| 193 | +const expr = `{ |
| 194 | + a: x * 3, |
| 195 | + b: { |
| 196 | + /*this x is a property and not the x on the input object*/ |
| 197 | + x: "first" + "_" + "second", |
| 198 | + y: min(x, 0), |
| 199 | + }, |
| 200 | + c: [0, 1, 2, x], |
| 201 | +}`; |
| 202 | +parser.evaluate(expr, { x: 3 }); |
| 203 | +/* |
| 204 | +{ |
| 205 | + a: 15, |
| 206 | + b: { |
| 207 | + x: 'first_second', |
| 208 | + z: 0 |
| 209 | + }, |
| 210 | + c: [0, 1, 2, 3] |
| 211 | +} |
| 212 | +*/ |
| 213 | +``` |
| 214 | + |
| 215 | +## As Operator (Type Conversion) |
| 216 | + |
| 217 | +An as operator has been added to support type conversion. **This operator is disabled by default and must be explicitly enabled by setting `operators.conversion` to true in the options.** It can be used to perform value conversion. By default is of limited value; it only supports converting values to numbers, int/integer (by rounding the number), and boolean. The intent is to allow integration of more sophisticated value conversion packages such as numeral.js and moment for conversion of other values. |
| 218 | + |
| 219 | +```js |
| 220 | +const parser = new Parser({ operators: { conversion: true } }); |
| 221 | +parser.evaluate('"1.6" as "number"'); // 1.6 |
| 222 | +parser.evaluate('"1.6" as "int"'); // 2 |
| 223 | +parser.evaluate('"1.6" as "integer"'); // 2 |
| 224 | +parser.evaluate('"1.6" as "boolean"'); // true |
| 225 | +``` |
| 226 | + |
| 227 | +The default `as` implementation can be overridden by replacing `parser.binaryOps.as`. |
| 228 | + |
| 229 | +```js |
| 230 | +const parser = new Parser({ operators: { conversion: true } }); |
| 231 | +parser.binaryOps.as = (a, _b) => a + '_suffix'; |
| 232 | +parser.evaluate('"abc" as "suffix"'); // 'abc_suffix' |
| 233 | +``` |
0 commit comments