Evaluate arbitrary expressions using a Javascript-like syntax.
Supported expressions:
- Arithmetic - evaluates to a number
- String - evaluates to a string
- Logical - evaluates to a boolean
npm install @trs/expression
# or
yarn add @trs/expression
# or
pnpm add @trs/expression
Expression strings can be evaluated to a single value.
import { evaluate } from '@trs/expression';
const ast = evaluate('1 + 2 == 3');
assert(ast === true);
Expression strings can be parsed into an expression tree.
import { parse } from '@trs/expression';
const ast = parse('1 + 2 == 3');
assert(ast == {
type: "BinaryExpression",
... etc
});
Expression trees can be evaluated to a single value.
import { evaluate } from '@trs/expression';
const result = evaluate({
type: "BinaryExpression",
operator: "==",
left: {
type: "BinaryExpression",
operator: "+",
left: {
type: "Literal",
kind: "number",
value: "1"
},
right: {
type: "Literal",
kind: "number",
value: "2"
}
},
right: {
type: "Literal",
kind: "number",
value: "3"
}
});
assert(result === true);