Socket
Socket
Sign inDemoInstall

simple-eval

Package Overview
Dependencies
1
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    simple-eval

Simple JavaScript expression evaluator


Version published
Weekly downloads
443K
increased by1.24%
Maintainers
1
Install size
390 kB
Created
Weekly downloads
 

Readme

Source

simple-eval

Simple JavaScript expression evaluator.

Install

yarn add simple-eval

or if npm is package manager of your choice

npm install simple-eval --save

Usage

import simpleEval from 'simple-eval';

simpleEval('2 + 4 * 10 + -4'); //  38
simpleEval('Math.floor(Math.PI * 10)'); // exception 
simpleEval('Math.floor(Math.PI * 10)', { Math }); // 31, works because we provided Math
simpleEval('foo.bar.baz ? 10 : Math.random()', {
  Math,
  foo: {
    bar: {
      baz: false,
    }
  }
}); // some random number, as returned by Math.random()

By default, simple-eval uses jsep, but you're free to use any ESTree compliant parser such as acorn, @babel/parser, or esprima.

Caveats

Although a number of use cases is supported, do note that this library does not aim to be a drop-in replacement for eval. By supporting a limited set of instructions, it's arguably safer than eval, albeit it's still not supposed to be used instead of proper sandbox. In particular, all kind of declarations and assignments are prohibited.

LICENSE

MIT

Keywords

FAQs

Last updated on 17 Jul 2022

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc