
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
@doars/interpret
Advanced tools
Interpret a subset JavaScript expression without using the eval function or Function constructor. Allowing it to be used in combination with a strict Content Security Policy that does not contain the unsafe-eval option.
The interpreter is written for the @doars/doars library, but can be used elsewhere as well. The features it support are meant to be simple and not allow for much complexity similar to what a formulae in a spreadsheet can do.
Even though the library does not use the eval function or Function constructor security is still an important concern when interpreting any code. Do not provide any functions via the context parameter that could cause harm, and you should not run any expression that might contain user input. So do take the accompanying risks into consideration before using this library.
Install the package from NPM, then import and use it.
npm i @doars/interpret
// Import library.
import { interpret, parse, run } from '@doars/interpret'
// Interpret expression.
const resultOne = interpret(
'(hello == 3) ? "there" : general', // Expression.
{ hello: 4, general: 'kenobi' } // Context.
)
// resultOne = 'kenobi'
// Or interpret in separate steps.
// Parse the expression first.
const node = parse('(hello == 3) ? "there" : general')
// Then run the node.
const resultTwo = run(node, { hello: 4, general: 'kenobi' })
// resultTwo = 'kenobi'
Exported functions:
interpret Interpret an expression.
@param {string} expression Expression to interpret.@param {Object} context Context of the expression.@returns {Array} results of the expression.parse Parse an expression.
@param {string} expression Expression to parse.@returns {Object} The parsed expression.run Run a parsed expression.
@param {Object} node Parsed expression.@param {Object} context Context of the expression.@returns {Array} results of the expression.The following node types are exported as variables: ARRAY, ASSIGN, BINARY, CALL, CONDITION, IDENTIFIER, LITERAL, MEMBER, OBJECT, PROPERTY, SEQUENCE, UNARY, UPDATE.
interpretis simply a short hand forrun(parse(expression), context).
The interpret does not support all JavaScript features. However any expression valid to be run by this library should also be valid JavaScript code. That being said the interpreter might ignore some syntax errors that are usually not allowed.
hello, hello.there, hello[there] and hello['there']. Any identifiers need te be given via the context parameter when running the expression.hello(), hello(there) and hello('there', 'general', 'kenobi'). Any functions need te be given via the context parameter when running the expression.hello(); world(). The result of each expression is returned, hence the interpret and run functions always return an array.As well as several value types and most operators. See an overview below for more information.
null.undefined.false and true.'hello' and "there".1 and 12.3.[], ['hello'] ['hello', 'there'].{}, { hello: 'there' }, { hello: 'there', general: 'kenobi' }, { [hello]: 'there' }, { hello } and { hello, there }.2 ** 3, as well as *, /, %, +, and -.false || true, as well as && and ??.true == false, as well as !=, ===, and !==.1 > 0, as well as >, <=, and >=.true ? 0 : 1.+1 as well as -1 and !false.--hello as well as hello--, ++hello and hello++.hello = 'there'.hello **= 2 as well as *=, /=, %=, +=, and -=.hello ||= 'there' as well as &&= and ??=.{ hello: {} }.FAQs
Simple JavaScript expression interpreter.
We found that @doars/interpret demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.