1.0.0 • Published 5 years ago

@momsfriendlydevco/eval v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

@momsfriendlydevco/eval

Safe expression evaluator.

This function is an implementation of the Shunting-Yard Alorithm which parses a regular JavaScript expression, runs it though a Reverse Polish Notation parser and returns a result.

The output of this function is the evaluated result of the line, along with any optionally supplied context.

Examples

var eval = require('@momsfriendlydevco/eval');

// Simple evaluations
eval('1'); //= 1
eval('1 + 1'); //= 2
eval('"a" + "b" + "c"'); //= "abc"


// Using an optional context
eval('foo + bar', {foo: 1, bar: 2}); //= 3
eval('foo(bar)', {foo: a => a, bar: 10}); //= 10
eval('add(10, 20)', {add: (a, b) => a + b}); //= 30

API

eval(expression, context, options)

Evaluate a JavaScript style expression and return the result. Context is an object of optional variables to allow access to or functions to support. Default options can also be set in eval.defaults.

Options are:

OptionTypeDefaultDescription
reObjectSee codeList of Regular expressions used to parse the expression
re.operandBoolean or RegExpfalseThe regular expression used to parse operands, if specifying your own operands this needs to be falsy to force recompile
operandsObjectSee codeList of Operands to support

eval.defaults

Object of default values to use when calling eval().

eval.get(target, path)

Internal function to retrieve a dotted notation path in an expression. Similar to the Lodash Get function.

eval.compileOperands(operands)

Compiler to return the optimized Regular Expression of the operands structure. Called automatically if settings.re.operands is falsy or on initalization of the module. Invoke this function manually and stash the result in defaults if defining a custom operand chain.

eval.defaults.re.operand = eval.compileOperands(eval.defaults.operands);

Why?

There are a number of reasons this module is necessary instead of using plain eval():

  1. Its safe - Its eval, have you used it? Its not meant to be used for anything. Eval is massivly unsafe and unless used exactly right can cause major harm
  2. Babel safe - This module does not use any JS parsing, it uses its own internal lexing + parsing engine
  3. Context is sandboxed - Only exposed variables and functions are accessible within an expression, nothing else is accepted
  4. Implements only a subset of the JS stanard - Advanced JS concepts like Classes and Assignments are configurable (eventually - see the Todo list)

Todo

Lexical features of JavaScript and their support:

PrecedenceTypeSyntaxSupported
21Parenthesis(, )Yes
20Member accessa.b.cYes
20New with arg listnew A()
20Functionsa(b, c)Yes
18Postfixesa--, b++
17Logical NOT!a
17Bitwise NOT~a
17Unary plus+a
17Unary negation-a
17Typeoftypeof a
17Voidvoid
17Deletedelete a
17Awaitawait a()
16Exponentialsa ** b
15Multiplicationa * bYes
15Divisiona / bYes
15Remaindera % bYes
14Additiona + bYes
14Subtractiona - bYes
13Bitwise shifta << b, a >> b, a >>> b
12Lower than (or equal)a < b, a <= bYes
12Higher than (or equal)a > b, a >= bYes
12Ina in b
12Instance ofa instanceof b
11(In)Equalitya == b, a != b, a === b, a !== bYes
10Bitwise ANDa & b
9Bitwise XORa ^ b
8Bitwise ORa \| b
7Nullish coalescinga ?? bYes
6Logical ANDa && bYes
5Logical ORa || bYes
4Ternarya ? b : c
3Assignmenta = b
2Yieldyield a or yield* a
1Comma listsa, b, c