0.0.9 • Published 1 year ago

@liquidform/formula-lang v0.0.9

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
1 year ago

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].label

Functions

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
  • - subtraction 7 -2
  • % modulus 5 % 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.

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago