1.0.2-alpha.0 • Published 4 years ago

@alpaca-travel/fexp-js v1.0.2-alpha.0

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

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
1.0.1-alpha.0

4 years ago

1.0.2-alpha.0

4 years ago

1.0.0-alpha.0

4 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago