🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

expression-eval

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
e

expression-eval

JavaScript expression parsing and evaluation.

5.0.1
latest
100

Supply Chain Security

100

Vulnerability

95

Quality

76

Maintenance

100

License

Repository has been archived by the owner.
Version published
Weekly downloads
261K
4.26%
Maintainers
1
Weekly downloads
 
Created
Issues
0

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

FAQs

Package last updated on 19 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts