expression-eval

WebJar for expression-eval

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

expression-eval
Last Version

Last Version

3.1.2
Release Date

Release Date

Type

Type

jar
Description

Description

expression-eval
WebJar for expression-eval
Project URL

Project URL

https://www.webjars.org
Source Code Management

Source Code Management

https://github.com/donmccurdy/expression-eval

Download expression-eval

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/expression-eval/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>expression-eval</artifactId>
    <version>3.1.2</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/expression-eval/
implementation 'org.webjars.npm:expression-eval:3.1.2'
// https://jarcasting.com/artifacts/org.webjars.npm/expression-eval/
implementation ("org.webjars.npm:expression-eval:3.1.2")
'org.webjars.npm:expression-eval:jar:3.1.2'
<dependency org="org.webjars.npm" name="expression-eval" rev="3.1.2">
  <artifact name="expression-eval" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='expression-eval', version='3.1.2')
)
libraryDependencies += "org.webjars.npm" % "expression-eval" % "3.1.2"
[org.webjars.npm/expression-eval "3.1.2"]

Dependencies

compile (1)

Group / Artifact Type Version
org.webjars.npm : jsep jar [0.3.0,0.4)

Project Modules

There are no modules declared in this project.

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. Always use caution when combining user input and dynamic evaluation, and avoid it where possible.

For example:

const ast = expr.parse('foo[bar](baz)()');
expr.eval(ast, {
  foo: String,
  bar: 'constructor',
  baz: 'console.log("im in ur logs");'
});
// Prints: "im in ur logs"

The kinds of expressions that can expose vulnerabilities can be more subtle than this, and are sometimes possible even in cases where users only provide primitive values as inputs to pre-defined expressions.

License

MIT License.

Versions

Version
3.1.2