What is expression-eval?
The expression-eval npm package is a simple expression evaluator that can parse and evaluate mathematical expressions. It is useful for evaluating expressions dynamically at runtime, making it a handy tool for applications that require dynamic calculations or formula evaluations.
What are expression-eval's main functionalities?
Basic Arithmetic Evaluation
This feature allows you to evaluate basic arithmetic expressions. The code sample demonstrates how to parse and evaluate a simple arithmetic expression.
const { Parser } = require('expression-eval');
const parser = new Parser();
const expr = parser.parse('2 + 3 * (4 - 1)');
const result = expr.evaluate();
console.log(result); // Output: 11
Variable Support
This feature allows you to include variables in your expressions. The code sample shows how to parse an expression with variables and evaluate it by providing the variable values.
const { Parser } = require('expression-eval');
const parser = new Parser();
const expr = parser.parse('a * b + c');
const variables = { a: 2, b: 3, c: 4 };
const result = expr.evaluate(variables);
console.log(result); // Output: 10
Function Support
This feature allows you to use functions within your expressions. The code sample demonstrates how to parse and evaluate an expression that includes the square root function.
const { Parser } = require('expression-eval');
const parser = new Parser();
const expr = parser.parse('sqrt(a^2 + b^2)');
const variables = { a: 3, b: 4 };
const result = expr.evaluate(variables);
console.log(result); // Output: 5
Custom Functions
This feature allows you to define and use custom functions in your expressions. The code sample shows how to parse and evaluate an expression with a custom function.
const { Parser } = require('expression-eval');
const parser = new Parser();
const expr = parser.parse('customFunc(a, b)');
const variables = { a: 2, b: 3, customFunc: (a, b) => a + b };
const result = expr.evaluate(variables);
console.log(result); // Output: 5
Other packages similar to expression-eval
mathjs
Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser, which can evaluate expressions, perform symbolic computation, and more. Compared to expression-eval, math.js offers a broader range of mathematical functions and capabilities, including support for matrices, units, and complex numbers.
jsep
JSEP (JavaScript Expression Parser) is a JavaScript library for parsing JavaScript expressions. It generates an abstract syntax tree (AST) from an expression string, which can then be evaluated or manipulated. While jsep focuses on parsing expressions, it does not include an evaluation engine, making it more suitable for use cases where you need to parse and then manually evaluate or transform expressions.
expr-eval
Expr-eval is another JavaScript expression parser and evaluator. It is similar to expression-eval in terms of functionality, allowing you to parse and evaluate mathematical expressions with support for variables and functions. Expr-eval is known for its simplicity and ease of use, making it a good alternative to expression-eval for basic expression evaluation needs.
expression-eval
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:
import { parse, eval } from 'expression-eval';
const { parse, eval } = require('expression-eval');
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');
const value = eval(ast, {a: 2, b: 2, c: 5});
Alternatively, use evalAsync
for asynchronous evaluation.
Compilation
import { compile } from 'expression-eval';
const fn = compile('foo.bar + 10');
fn({foo: {bar: 'baz'}});
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.