Giter VIP home page Giter VIP logo

expr4js's Introduction

expr4js

expr4js is a JavaScript module for expression parsing and evaluation.

Build

  • Test: npm test

  • Dev: npm run dev

  • Prod: npm run build

  • Play in the local sandbox: sandbox

  • Sandbox

Allowed operators

  • Relational and comparison operators: >, <, >=, <=, ==, !=
  • Arithmetic operatos: *, /, -, +
  • Logical operators: and, or
  • Member access: .

Usage

const expr4js = require('../dist/expr4.js').default;

const exprParser = new expr4js();

const model = {
    car: {
        color: 'green'
    }
};

const expr = 'car.color == "green"';

const r = exprParser.parse(expr);

if (r === null) {
    console.log(exprParser.getLastError().getMessage());
} else {
    console.log(`<${expr}>: ` + r.execute(model));
}

Examples

Basic example

var myexpr = jsexpr.parse(expression);
if(myexpr) {
  var result = myexpr.execute(scope);
  console.log(result);
}

var expr = jsexpr.parse('1 == 1');

if(expr) {
  console.log(expr.execute()); // Will return 'TRUE'
}

var expr = jsexpr.parse('1 == 2');

if(expr) {
  console.log(expr.execute()); // Will return 'FALSE'
}

var expr = jsexpr.parse('id == 12345');

if(expr) {
  console.log(expr.execute({
    id: 12345
  })); // Will return 'TRUE'
}

var expr = jsexpr.parse('subscriber.id == 99');

if(expr) {
  console.log(expr.execute({
    subscriber: {
      id: 99
    }
  })); // Will return 'TRUE'
}

var expr = jsexpr.parse('subscriber.id == 99 and subscriber.name == "Ivan"');

if(expr) {
  console.log(expr.execute({
    subscriber: {
      id: 99,
      name: 'Ivan'
    }
  })); // Will return 'TRUE'
}

var expr = jsexpr.parse('subscriber.test() == "99:Ivan"');

if(expr) {
  console.log(expr.execute({
    subscriber: {
      id: 99,
      name: 'Ivan',
      test: function() {
        return this.id + ':' + this.name;
      }
    }
  })); // Will return 'TRUE'
}

 /*
    Filtering array of objects.
    Output will be:
    {
      age: "19",
      id: "2042",
      name: "Pavel"
    }
 */
 var expr = jsexpr.parse("age < 30 and name == 'Pavel'");
 
 var data = [
   {"name": "Pavel", "age": "30", "id": "13423"},
   {"name": "Roman", "age": "23", "id": "323"},
   {"name": "Ivan", "age": "33", "id": "232221"},
   {"name": "John", "age": "19", "id": "894"},
   {"name": "Steven", "age": "42", "id": "776"},
   {"name": "Pavel", "age": "19", "id": "2042"}
 ];
       
if(expr) {
  var len = data.length;
  for(var i = 0 ; i < len; i++) {
    if(expr.execute(data[i]) === true) {
      console.log(data[i]);
    }
  }
}
```

expr4js's People

Contributors

dependabot[bot] avatar paveldanilin avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.