A safe parse for simple js expression.
import evaluate from 'simple-evaluate';
evaluate(null, '12 + 1 > 14');
evaluate({ a }, 'a + 1 > 14');
- Math operator
+ - * / %
- ternary expression
a ? b : c
- Comparison
> < >= <= == != === !==
- Logical
&& || !
- Negation
You can run those expression, for example:
evaluate({}, '!a > 0');
evaluate({}, 'a > 0 || a < -12 || 12 + 2*(4 + 4) < 12');
evaluate({ a: 1 }, '-a * 2');
$.
stand for the root value all context
, value path only support '.', and not support function call.
since 1.2.0,
$.
is optional. That mean$.a.b
is equal toa.b
String and boolean support, string start with ' | "
, just the same as javascript expression.
Boolean use two key words, true | false
.
You can use template string, just like the javascript syntax, for example.
evaluate({ a: 22 }, "`I am ${ a >= 18 ? 'adult' : 'child' }`").should.equal('I am adult');
- Funcion call
So, you can not run those expression
evaluation({}, 'a(1) > 0');