fexp-js - Functional Expressions in JavaScript
Simple modular expressions described in a portal format (JSON).
Syntax
The syntax:
[[!]<name>, [param1[, param2[, ..., paramN]]]]
Evaluation
import { evaluate } from "@alpaca-travel/fexp-js";
import lang from "@alpaca-travel/fexp-js-lang";
const expression = JSON.parse(`["==", ["get", "foo"], "bar"]`);
const result = evaluate(expr, lang, { foo: "bar" });
console.log(result);
Core functions
- Equality: ==, !=, <, >, <=, >=
- Context: get
- Deep Equality: equal/equals, !equal/!equals
- Existence: has/have/exist/exists/empty, !has/!have/!exist/!exists/!empty
- Membership: in/!in
- Types: typeof, to-boolean, to-string, to-number, to-regex
- Regular Expressions: "regex-test"
- Combining: all/any/none
- String manipulation: concat, uppercase, lowercase
- Math: +, -, *, /, floor, ceil
- more..
Declaring new functions
const foobar = ([a, b]) => a * b;
const expression = JSON.parse(`["foobar", ["get", "foo"], ["get", "bar"]]`);
const result = evaluate(expression, { ...lang, foobar }, { foo: 2, bar: 3 });
console.log(result);