0.0.9 • Published 2 years ago
@liquidform/formula-lang v0.0.9
An expression parser and executor for LiquidForm's formula language
LiquidForm's expressions are a subset of JavaScript expression syntax.
They're used for field validation, computed fields and text templates.
Literals
- numbers:
2,-3.6 - strings:
'single quoted',"double quoted" - booleans:
true,false
Identifiers
The lang supports identifiers and property chains. Bracket notation is also supported.
Identifiers are resolved from the context.properties object passed to the evaluateExpression(expression:String, context:Context):any method.
Examples
person.addresses[0].street
listOfOptions[selectedOptionId].labelFunctions
Functions can be called to compute values. They must be present in the context.functions object passed to the evaluateExpression(expression:String, context:Context):any method.
Examples
evaluateExpression(
'Math.max(valueA, valueB)',
{
functions : {Math},
properties : {
valueA : 12,
valueB : 13
}
});Arithmetics
*multiplication:2 * 3 * 22/division:81 / 9+addition:2 + 2-subtraction7 -2%modulus5 % 2
Comparison
==strict equality!=strict inequality>greater than<less than>=equal or greater than<=equal or less than
Logical
&&logical and||logical or
Ternary operator
//syntax
condition ? trueResult : falseResult
//example
carMakeField == 'Tesla' ? 'electric' : 'petrol'Unary operator
!false //== true The undefined value
The syntax tries to steer away from undefined and throws a ParseError when it encounters one.