chore(release): 5.1.0 [skip ci]

# [5.1.0](https://github.com/node-casbin/expression-eval/compare/v5.0.2...v5.1.0) (2024-10-24)

### Features

* improve package.json ([ba888a9](https://github.com/node-casbin/expression-eval/commit/ba888a96c51b921aa79f0a4cf4823f191a861ebd))
2 files changed
tree: fdcd41d102a8b9bf2f7fa971daa1e340f0f9958d
  1. .github/
  2. .eslintrc.json
  3. .gitignore
  4. .releaserc.json
  5. CHANGELOG.md
  6. index.ts
  7. LICENSE
  8. package.json
  9. README.md
  10. test.js
  11. tsconfig.json
  12. tslint.json
  13. yarn.lock
README.md

expression-eval

Latest NPM release Minzipped size License CI

JavaScript expression parsing and evaluation.

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

Apache 2.0 License.