2.1.0 • Published 2 years ago
js-expr-runner v2.1.0
js-expr-runner
js-expr-runner allows you to parse an expression string and run it in an isolated context
install
npm install js-expr-runner
example
const exp = require('js-expr-runner')
const run = (expression, context) => {
const runner = exp.getRunner(expression)
return runner.run(context).value
}
console.log(run('a + 3 * fn(20)', {
a: 50,
fn: n => n * 2
}))
// 170
// SECURITY
console.log(run('window', {}))
// undefined
console.log(run('global', {}))
// undefined
console.log(run('console', {}))
// undefined
Restrictions
In js-expr-runner you can use anything js allows to use in js (ES5) expressions (in the right part of an assignment operator) except bitwise operations and in-place functions (lambdas are also forbidden)
In addition to ES5 you can also use "?.", "??" operators and spread in arrays "...a, 1,2, ...b" and call arguments "a(...b, c)"