New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

evalculist

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

evalculist

scoped eval for simple js expressions

  • 0.2.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

evalculist.js

Build Status

evalculist provides a way to evaluate arbitrary javascript expressions in a controlled way, without exposing global variables. It accomplishes this by parsing the expressions such that variables become strings that are passed to variable, accessor, and assignment functions.

foo = bar.baz

becomes

assignment("foo", accessor(variable("bar"), "baz"))

Usage

Using evalculist.new

const localVars = {
  a: { b: 1 },
  c: 2
};
const evaluate = evalculist.new({
  variable: (name) => localVars[name] || Math[name],
  accessor: (object, key) => object[key],
  assignment: (name, value) => (localVars[name] = value)
});
const result = evaluate('pow(a.b + 1, c)');
// result === 4

Using evalculist.newFromContext

const context = {
  a: { b: 1 },
  c: 2,
  pow: Math.pow
};
const evaluate = evalculist.newFromContext(context);
const result = evaluate('pow(a.b + 1, c)');
// result === 4

Known Bugs

  • Object literals do not work if the keys are not enclosed in quotes (e.g. {a:1} does not work, but {"a":1} does).
  • Keywords do not work (e.g. if,for,var, etc.).

License MIT

FAQs

Package last updated on 19 Nov 2017

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc