This is a modern TypeScript port of the expr-eval library, completely rewritten with contemporary build tools and development practices. Originally based on expr-eval 2.0.2, this version has been restructured with a modular architecture, TypeScript support, and comprehensive testing using Vitest.
Parses and evaluates mathematical expressions. It's a safer and more math-oriented alternative to using JavaScript's eval function for mathematical expressions.
It has built-in support for common math operators and functions. Additionally, you can add your own JavaScript functions. Expressions can be evaluated directly, or compiled into native JavaScript functions.
npm install @pro-fa/expr-evalconst Parser = require('@pro-fa/expr-eval').Parser;
const parser = new Parser();
let expr = parser.parse('2 * x + 1');
console.log(expr.evaluate({ x: 3 })); // 7
// or
Parser.evaluate('6 * x', { x: 7 }) // 42| Document | Description |
|---|---|
| Parser | Parser class API, constructor options, and methods |
| Expression | Expression object methods: evaluate, substitute, simplify, variables, symbols, toString, toJSFunction |
| Expression Syntax | Operator precedence, unary operators, pre-defined functions, string manipulation, array literals, function definitions, constants |
| TypeScript Port Enhancements | New features: undefined support, coalesce operator, optional chaining, SQL case blocks, object construction, promises, and more |
| Language Service | IDE integration: code completions, hover information, syntax highlighting, Monaco Editor integration |
| Performance Testing | Benchmarks, performance grades, and optimization guidance |
- Mathematical Expressions - Full support for arithmetic, comparison, and logical operators
- Built-in Functions - Trigonometry, logarithms, min/max, array operations, string manipulation
- Custom Functions - Add your own JavaScript functions
- Variable Support - Evaluate expressions with dynamic variable values
- Expression Compilation - Convert expressions to native JavaScript functions
- TypeScript Support - Full type definitions included
- Undefined Support - Graceful handling of undefined values
- Coalesce Operator -
??operator for null/undefined fallback - SQL Case Blocks - SQL-style CASE/WHEN/THEN/ELSE expressions
- Object Construction - Create objects and arrays in expressions
- Language Service - IDE integration with completions, hover info, and highlighting
cd <project-directory>
npm install
npm test# Run all benchmarks
npm run bench
# Run specific categories
npm run bench:parsing # Parser performance
npm run bench:evaluation # Evaluation performance
npm run bench:memory # Memory usageSee docs/performance.md for detailed performance documentation.
See LICENSE.txt for license information.