A minimalist (4955 bytes) expression compiler and evaluator.
https://arnaudbuchholz.github.io/punyexpr/samples/calc.html
<script src="https://cdn.jsdelivr.net/npm/punyexpr/dist/punyexpr.js"></script>
const incValue = punyexpr('value + 1')
incValue({ value: 1 }) // 2
Use with punybind@>=1.2.0
const safebind = punybind.use({
compiler: punyexpr
})
// Use safebind to bind HTML
-
Regular expressions are not secure and are not allowed by default
-
Leverage the option
{ regex: true }
to enable regular expressions using the default JavaScript implementation:
const unsecure = punyexpr('value.match(/a+b/)', { regex: true })
- Or plug any custom regular expression builder using
{ regex: (pattern, flags) => { /* */ } }
const unsecure = punyexpr('value.match(/a+b/)', { regex: (pattern, flags) => new RegExp(pattern, flags) })
- Check the source for the (altered and) implemented grammar,
in particular the following are not supported :- Bitwise, async and coalesce operations
new
andthis
- Object literals
- See the tests for supported expressions.
- The implementation is compliant with Content Security Policy.