expression-eval

JavaScript expression parsing and evaluation.

The expression-eval npm package is no longer maintained. The package was originally published as part of a now-completed personal project, and I do not have incentives to continue maintenance.
Last updated 8 years ago by donmccurdy .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install expression-eval 
SYNC missed versions from official npm registry.

expression-eval

Latest NPM release Minzipped size License CI

JavaScript expression parsing and evaluation.

⚠️ UNMAINTAINED: The expression-eval npm package is no longer maintained. The package was originally published as part of a now-completed personal project, and I do not have incentives to continue maintenance. Please feel free to use the code, but be aware that support and updates will not be available.

⚠️ SECURITY NOTICE: As mentioned under Security below, this library does not attempt to provide a secure sandbox for evaluation. Evaluation involving user inputs (expressions or values) may lead to unsafe behavior. If your project requires a secure sandbox, consider alternatives such as vm2.

Powered by jsep.

Installation

Install:

npm install --save expression-eval

Import:

// ES6
import { parse, eval } from 'expression-eval';
// CommonJS
const { parse, eval } = require('expression-eval');
// UMD / standalone script
const { parse, eval } = window.expressionEval;

API

Parsing

import { parse } from 'expression-eval';
const ast = parse('1 + foo');

The result of the parse is an AST (abstract syntax tree), like:

{
  "type": "BinaryExpression",
  "operator": "+",
  "left": {
    "type": "Literal",
    "value": 1,
    "raw": "1"
  },
  "right": {
    "type": "Identifier",
    "name": "foo"
  }
}

Evaluation

import { parse, eval } from 'expression-eval';
const ast = parse('a + b / c'); // abstract syntax tree (AST)
const value = eval(ast, {a: 2, b: 2, c: 5}); // 2.4

Alternatively, use evalAsync for asynchronous evaluation.

Compilation

import { compile } from 'expression-eval';
const fn = compile('foo.bar + 10');
fn({foo: {bar: 'baz'}}); // 'baz10'

Alternatively, use compileAsync for asynchronous compilation.

Security

Although this package does avoid the use of eval(), it cannot guarantee that user-provided expressions, or user-provided inputs to evaluation, will not modify the state or behavior of your application. This library does not attempt to provide a secure sandbox for evaluation. Evaluation of arbitrary user inputs (expressions or values) may lead to unsafe behavior. If your project requires a secure sandbox, consider alternatives such as vm2.

License

MIT License.

Current Tags

  • 5.0.1                                ...           latest (3 years ago)

22 Versions

  • 5.0.1 [deprecated]           ...           3 years ago
  • 5.0.0 [deprecated]           ...           5 years ago
  • 4.0.0 [deprecated]           ...           6 years ago
  • 3.1.2 [deprecated]           ...           6 years ago
  • 3.1.1 [deprecated]           ...           7 years ago
  • 3.1.0 [deprecated]           ...           7 years ago
  • 3.0.4 [deprecated]           ...           7 years ago
  • 3.0.3 [deprecated]           ...           7 years ago
  • 3.0.2 [deprecated]           ...           7 years ago
  • 3.0.1 [deprecated]           ...           7 years ago
  • 3.0.0 [deprecated]           ...           7 years ago
  • 2.1.0 [deprecated]           ...           7 years ago
  • 2.0.0 [deprecated]           ...           8 years ago
  • 1.4.0 [deprecated]           ...           8 years ago
  • 1.3.1 [deprecated]           ...           8 years ago
  • 1.3.0 [deprecated]           ...           8 years ago
  • 1.2.3 [deprecated]           ...           9 years ago
  • 1.2.2 [deprecated]           ...           9 years ago
  • 1.2.1 [deprecated]           ...           9 years ago
  • 1.2.0 [deprecated]           ...           9 years ago
  • 1.1.0 [deprecated]           ...           9 years ago
  • 1.0.1 [deprecated]           ...           10 years ago
Maintainers (1)
Downloads
Total 79
Today 0
This Week 0
This Month 57
Last Day 0
Last Week 25
Last Month 22
Dependencies (1)
Dev Dependencies (1)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE