@alpaca-travel/fexp-js

1.0.2-alpha.0 • Public • Published

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";

// Expressions are just JSON strings
const expression = JSON.parse(`["==", ["get", "foo"], "bar"]`);

// Evaluate the expression against context
const result = evaluate(expr, lang, { foo: "bar" });
console.log(result); // Output; true

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

// My own function
const foobar = ([a, b]) => a * b;

// Example expression with our own function
const expression = JSON.parse(`["foobar", ["get", "foo"], ["get", "bar"]]`);
const result = evaluate(expression, { ...lang, foobar }, { foo: 2, bar: 3 });
console.log(result); // 6

Dependencies (0)

    Dev Dependencies (7)

    Package Sidebar

    Install

    npm i @alpaca-travel/fexp-js

    Weekly Downloads

    0

    Version

    1.0.2-alpha.0

    License

    MIT

    Unpacked Size

    6.63 kB

    Total Files

    4

    Last publish

    Collaborators

    • cammanderson