Registry / serialization / expr-parser

expr-parser

JSON →
library1.0.0jsnpmunverified

expr-parser is a lightweight JavaScript library designed for parsing and evaluating mathematical and object-property expressions from strings. It allows developers to define expressions as strings and then execute them against a given context object, enabling dynamic calculations. The current stable version is 1.0.0, and based on its release history, updates appear infrequent, focusing on stability rather than rapid feature additions. Its core differentiation lies in its minimalist API and straightforward approach to expression evaluation, making it an ideal choice for scenarios requiring basic string-based computation without the overhead or complexity of a full-fledged abstract syntax tree (AST) manipulator or a more expansive templating language. It's particularly suited for environments where controlled, user-defined arithmetic or data access logic is needed.

npm install expr-parser
INSTALL
IMPORT
SIG · EXPR-PARSER
E
expr-parser
serializationjavascriptv1.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Expression
const Expression = require('expr-parser');
import Expression from 'expr-parser';
This package is primarily distributed as CommonJS. For ESM environments, a bundler or transpiler is typically required to consume it, which would transform the `require` call into an appropriate `import` statement. Direct `import` syntax will fail in pure Node.js ESM without configuration.

Demonstrates parsing and evaluating arithmetic expressions with object property access and multiple variables.

const Expression = require('expr-parser'); // Example 1: Basic arithmetic and object property access const exprCalc1 = new Expression('a.value + 12 - (2 * 14 / 4)').parse(); console.log('Result 1:', exprCalc1({ a: { value: 3 } })); // Expected Output 1: 8 (3 + 12 - (2 * 14 / 4) = 3 + 12 - 7 = 8) // Example 2: Using multiple variables and ensuring correct operator precedence const exprCalc2 = new Expression('(user.age + product.price) * discountRate').parse(); console.log('Result 2:', exprCalc2({ user: { age: 25 }, product: { price: 100 }, discountRate: 0.9 })); // Expected Output 2: 112.5 ((25 + 100) * 0.9 = 125 * 0.9 = 112.5)
Debug
Known issues
gotchaEvaluating untrusted user-supplied expressions can lead to arbitrary code execution or denial-of-service vulnerabilities. This parser directly executes parsed expressions, which means malicious input could potentially compromise your application.
fix
Sanitize or validate all input expressions rigorously, or restrict allowed operators/functions to a safe subset. Never pass untrusted user input directly to `new Expression().parse()` without strict validation.
affects: >=1.0.0
gotchaThis parser is designed for simple arithmetic and object property access. It does not support advanced JavaScript features like function calls, array literals, control flow statements (e.g., `if`/`else`), or ternary operators (`? :`) within the expression string itself.
fix
Ensure that your expressions adhere to the supported syntax for basic arithmetic and property lookups. For more complex logic, consider pre-processing expressions or using a more robust scripting engine.
affects: >=1.0.0
gotchaFor extremely complex or deeply nested expressions, performance might degrade due to the nature of string parsing and dynamic evaluation. The library is optimized for simplicity rather than high-performance parsing of very long or intricate expressions.
fix
Profile critical sections of your application that use this parser. If performance becomes a bottleneck, consider pre-parsing expressions if they are static or using a more optimized Abstract Syntax Tree (AST)-based solution designed for high-throughput, complex evaluation.
affects: >=1.0.0
Errors
Common errors & fixes
Syntax Error: Unexpected token
The input expression string is syntactically incorrect or uses features not supported by the parser (e.g., calling functions or using control flow).
fix
Review the expression string for typos, incorrect operator usage, or unsupported JavaScript syntax. Refer to the library's test cases or documentation for supported expression patterns.
Cannot read property 'value' of undefined
The context object provided during evaluation (`exprCalc(context)`) does not contain properties or nested objects that the expression attempts to access (e.g., `a.value` when `a` is missing or undefined).
fix
Ensure the context object passed to the evaluated expression contains all necessary variables and properties referenced by the expression at the correct nesting level.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
3 hits · last 30 days
node
2
Bingbot
1
Resources
expr-parser — npm install expr-parser · libregistry