You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

eval-expression

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eval-expression

Evaluate an expression and get what you expect.

1.0.0
latest
npmnpm
Version published
Weekly downloads
31
-35.42%
Maintainers
1
Weekly downloads
 
Created
Source

Build statusDavid DM

eval-expression

Evaluate an expression and get what you expect.

Be warned: Take all precausions which apply to using eval. eval-expression is no safer, no more performant and no easier to debug.

But it is more predictable. And, just as eval, it sometimes is useful for rapid prototyping.

Installation

> npm install eval-expression

Usage

var evalExpression = require("eval-expression");


// `eval` fails with function expressions – `evalExpression` handles them.
var sayHello = evalExpression('function () {console.log("Hello!");}');
sayHello();  // Logs: Hello!

// `eval` fails with object literals – `evalExpression` does what you expect.
var fruit = evalExpression('{sort: "pear"}');
fruit;  // Outputs: {sort: "pear"}


// You can access variables in the current scope.
console.log(evalExpression("fruit"));  // Outputs: {sort: "pear"}


// `evalExpression` is only intended to evaluate expressions, not other
// statements.
var tasty;

// So don't do this:
evalExpression('if (fruit.sort == "pear") {tasty = true}');  // Throws

// Do this instead:
tasty = evalExpression('fruit.sort == "pear" ? true : false');  // Outputs: true

// …or even:
tasty = evalExpression('fruit.sort == "pear"');  // Outputs: true

API

evalExpression(expression)

expression

Type: String
Required

A single expression. It will be evaluated in an enclosed scope.

Why use it

eval doesn't always work as you'd expect. See these cases:

License

MIT © Tomek Wiszniewski.

Keywords

eval

FAQs

Package last updated on 03 Mar 2015

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