A minimalist (5144 bytes), zero-dependency expression compiler and evaluator for JavaScript. Current stable version is 1.3.1, released on an ongoing basis with fixes and features. It compiles expression strings into reusable functions that evaluate against a context object, supporting arithmetic, string operations, member access, function calls, and ternary expressions. It does NOT support bitwise, async, coalesce, new, this, or object literals. Regular expressions are disabled by default for security (ReDoS) and can be enabled via an option. The library includes TypeScript type definitions and is compliant with Content Security Policy (CSP). It is a safer alternative to eval() or new Function() for dynamic expressions.
npm install punyexprNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows basic usage: compile and evaluate, custom regex option, and the propertyOf hook.
Pass { regex: true } or a custom regex builder in the options.Replace 'import punyexpr from "punyexpr"' with 'import { punyexpr } from "punyexpr"'.Update to punyexpr@1.2.0 or later and adjust any type annotations if needed.
Avoid unsupported syntax in expressions. See documentation for supported grammar.
Use destructuring: const { punyexpr } = require('punyexpr');Enable regex by passing { regex: true } in compile options: punyexpr('value.match(/pattern/)', { regex: true })Replace '??' with a ternary or logical OR: e.g., 'a ?? b' becomes 'a !== null && a !== undefined ? a : b'
Use a propertyOf hook or guard with conditionals: e.g., 'a?.b' becomes 'a && a.b' if you know a is truthy.
Update to punyexpr@1.3.0 or later, and use the correct symbol: punyexpr[Symbol.for('punyexpr.propertyOf')]No dependency data recorded yet.