Skip to content

nicompte/jexl-3000

 
 

Repository files navigation

jexl-3000

jexl-3000 is a fork from github.com/mozilla/jexl-rs that adds:

  • a set of transforms
  • parsing cache
  • map/reduce/filter and similar transforms, that take an expression as input

It does not aim to be compatible with the jexl specification.

Usage

From rust

Add jexl-3000 to your dependencies:

[dependencies]
jexl-3000 = "0.0.1"

Import and use:

use jexl_3000::build_evaluator;
use serde_json::json;

fn main() {
    let evaluator = build_evaluator;
    let context = json!({
        "users": [
            {"name": "Bob", "age": 22},
            {"name": "Alicia", "age": 23},
            {"name": "Peter": "age": 19}
        ]
    });
    let res = evaluator.eval_in_context("{meanAge: users | pick('age') | mean | toInt, youngest: users | sortByAttribute('age') | first}", &context);
    assert_eq!(res, json!({"meanAge": 21, "youngest": "Peter"}))
}

From node

Add jexl-3000 to your package.json:

npm i jexl-3000

Import and use:

const Evaluator = require('jexl-3000').Evaluator;
const evaluator = new Evaluator();
const context = {
    users: [
        {name: 'Bob', age: 22},
        {name: 'Alicia', age: 23},
        {name: 'Peter', age: 19},
    ]
};
const res = evaluator.evaluate("{meanAge: users | pick('age') | mean | toInt, youngest: users | sortByAttribute('age') | first}", context);

From python

from jexl_python import Evaluator

evaluator = Evaluator()

context =  {
    users: [
        {name: 'Bob', age: 22},
        {name: 'Alicia', age: 23},
        {name: 'Peter', age: 19},
    ]
}

evaluator.evaluate("{meanAge: users | pick('age') | mean | toInt, youngest: users | sortByAttribute('age') | first}", context)

From wasm

TODO: add doc

Languages

  • Rust 73.5%
  • JavaScript 24.1%
  • HTML 1.5%
  • Scheme 0.3%
  • Shell 0.2%
  • C++ 0.2%
  • Other 0.2%