{"id":26128,"library":"punyexpr","title":"punyexpr","description":"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.","status":"active","version":"1.3.1","language":"javascript","source_language":"en","source_url":"https://github.com/ArnaudBuchholz/punyexpr","tags":["javascript","typescript"],"install":[{"cmd":"npm install punyexpr","lang":"bash","label":"npm"},{"cmd":"yarn add punyexpr","lang":"bash","label":"yarn"},{"cmd":"pnpm add punyexpr","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Default export is not available; must use named import. CommonJS also destructures.","wrong":"const punyexpr = require('punyexpr');\nconsole.log(punyexpr('1+1')({}));","symbol":"punyexpr","correct":"import { punyexpr } from 'punyexpr'"},{"note":"Since v1.3.0, 'compile' is separately exported as a named symbol.","wrong":"import { punyexpr } from 'punyexpr';\nconst compile = punyexpr.compile;","symbol":"punyexpr.compile","correct":"import { compile } from 'punyexpr'"},{"note":"This is a symbol (built-in) used to override property access.","wrong":"import { punyexpr } from 'punyexpr';\nconst propertyOf = punyexpr.propertyOf;","symbol":"punyexpr.propertyOf","correct":"import { propertyOf } from 'punyexpr'"}],"quickstart":{"code":"import { punyexpr } from 'punyexpr';\n\n// Compile an expression\nconst inc = punyexpr('value + 1');\n\n// Evaluate with a context\nconsole.log(inc({ value: 1 })); // 2\n\n// Compile with options (regex disabled by default)\nconst withRegex = punyexpr('name.match(/^A/i)', { regex: true });\nconsole.log(withRegex({ name: 'Alice' })); // ['Alice']\n\n// Use propertyOf hook (since v1.3.0)\nconst ctx = {\n  [Symbol.for('punyexpr.propertyOf')]: (value, prop) =>\n    value === undefined ? undefined : value[prop]\n};\nconsole.log(punyexpr('a.b')(ctx)); // undefined (a is undefined)","lang":"typescript","description":"Shows basic usage: compile and evaluate, custom regex option, and the propertyOf hook."},"warnings":[{"fix":"Pass { regex: true } or a custom regex builder in the options.","message":"Regular expressions are disabled by default to prevent ReDoS attacks. Enabling them requires explicit opt-in via { regex: true }.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Replace 'import punyexpr from \"punyexpr\"' with 'import { punyexpr } from \"punyexpr\"'.","message":"The default export (punyexpr.default) is deprecated and was never officially supported. Use named import 'punyexpr' instead.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Update to punyexpr@1.2.0 or later and adjust any type annotations if needed.","message":"In versions prior to 1.2.0, TypeScript type definitions were incorrect or missing. Upgrading may require fixes in type usage.","severity":"breaking","affected_versions":"<1.2.0"},{"fix":"Avoid unsupported syntax in expressions. See documentation for supported grammar.","message":"The library does not support 'new', 'this', object literals, bitwise operators, async/await, or nullish coalescing. Expressions using these will throw a compile error.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use destructuring: const { punyexpr } = require('punyexpr');","message":"When using CommonJS, require('punyexpr') returns an object with a 'punyexpr' property (named export). Do not use require('punyexpr').punyexpr directly as it may confuse bundlers.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Enable regex by passing { regex: true } in compile options: punyexpr('value.match(/pattern/)', { regex: true })","cause":"Regular expression is not allowed by default; the expression parser treats /.../ as division.","error":"TypeError: (intermediate value).match is not a function"},{"fix":"Replace '??' with a ternary or logical OR: e.g., 'a ?? b' becomes 'a !== null && a !== undefined ? a : b'","cause":"Nullish coalescing operator (??) is not supported.","error":"Error: Punyexpr compilation error: Unexpected token '?' (at column ...)"},{"fix":"Use a propertyOf hook or guard with conditionals: e.g., 'a?.b' becomes 'a && a.b' if you know a is truthy.","cause":"Optional chaining (?.) is not supported.","error":"SyntaxError: Unexpected token '.' (at column ...)"},{"fix":"Update to punyexpr@1.3.0 or later, and use the correct symbol: punyexpr[Symbol.for('punyexpr.propertyOf')]","cause":"Using propertyOf with a version earlier than 1.3.0 or misspelling (punyexp vs punyexpr).","error":"TypeError: punyexp.propertyOf is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}