
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");
var sayHello = evalExpression('function () {console.log("Hello!");}');
sayHello();
var fruit = evalExpression('{sort: "pear"}');
fruit;
console.log(evalExpression("fruit"));
var tasty;
evalExpression('if (fruit.sort == "pear") {tasty = true}');
tasty = evalExpression('fruit.sort == "pear" ? true : false');
tasty = evalExpression('fruit.sort == "pear"');
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.