2.1.1 • Published 3 months ago

static-eval v2.1.1

Weekly downloads
1,513,498
License
MIT
Repository
github
Last release
3 months ago

static-eval

evaluate statically-analyzable expressions

testling badge

build status

security

static-eval is like eval. It is intended for use in build scripts and code transformations, doing some evaluation at build time—it is NOT suitable for handling arbitrary untrusted user input. Malicious user input can execute arbitrary code.

example

var evaluate = require('static-eval');
var parse = require('esprima').parse;

var src = process.argv[2];
var ast = parse(src).body[0].expression;

console.log(evaluate(ast));

If you stick to simple expressions, the result is statically analyzable:

$ node '7*8+9'
65
$ node eval.js '[1,2,3+4*5-(5*11)]'
[ 1, 2, -32 ]

but if you use statements, undeclared identifiers, or syntax, the result is no longer statically analyzable and evaluate() returns undefined:

$ node eval.js '1+2+3*n'
undefined
$ node eval.js 'x=5; x*2'
undefined
$ node eval.js '5-4*3'
-7

You can also declare variables and functions to use in the static evaluation:

var evaluate = require('static-eval');
var parse = require('esprima').parse;

var src = '[1,2,3+4*10+n,foo(3+5),obj[""+"x"].y]';
var ast = parse(src).body[0].expression;

console.log(evaluate(ast, {
    n: 6,
    foo: function (x) { return x * 100 },
    obj: { x: { y: 555 } }
}));

methods

var evaluate = require('static-eval');

evaluate(ast, vars={})

Evaluate the esprima-parsed abstract syntax tree object ast with an optional collection of variables vars to use in the static expression resolution.

If the expression contained in ast can't be statically resolved, evaluate() returns undefined.

install

With npm do:

npm install static-eval

license

MIT

static-module@balena/jellyfish-jellyscriptlilu@infinitebrahmanuniverse/nolb-stati@cascadeos/dynamic-dynscript@everything-registry/sub-chunk-2822snbc-paas-front-cpfmcpaas-front-cpfmp149-tablemuhammara-recipemggauharnewbot@wmfs/cardscript-post-processor@wmfs/cardscript-cleanerangular-templatecache-extract@andrew_l/lilu@andy3520/jsonpath@arve.knudsen/static-module@astronautlabs/jsonpathbespoken-jsonpathbetter-jsonpathasksuite-core@axway/jsonpath@conscia/jsonpathcompylr@davidmarek/jsonpathconstantifybrstar@gitbook-ng/sleuthnode_kinectqp-jsonpathqmui_webprefixnotereact-lightbox-pack-18supportp147-tablep148-tablepifferosheetifysnapmaker-lubansneezesyncpeng-jsonpathroadiejsroadiejs-importrookie-corescopedifyrincewind-precompile-transformsleuthsnowpack-plugin-designspekchain_digger@crossed/webpack@djencks/asciidoctor-report-supportdash-ag-griddata-expression@anonybit-modules/videoreconstructionatom-selected-requires@fsslc/jsonpath@earthyorange/jsonpath@evxn/jsonpath@devorso/dcountdown@graphql-mock/codegen-types-map@jscad/desktop@kumologica/sdk@khoazero123/hummus-recipe@jpweeks/static-module@livereach/jsonpatheslint-plugin-no-constant-expressionsgear-expression@maestro-framework/jsonpathg5-knockout@mauriciocc/hummus-recipegenerator-bootstrap-boilerplate-templateglslifyglslify-live@kui-shell/jsonpathfindup-package-jsonflobotgraphql-codegen-types-map@jacobbubu/sneezehasten@pown/recon@pown/enginehummus-recipe@ramonornela/jsonpathjsonpathjsonpath-bespokenjsonpath-extensionjsonpath-litejsonpath-ngjsonpath-nodejsonpath-pajsonpath-projsonpath-wdf@refactorpro/pixi-virtual-joystickjscad-desktopjallaify
2.1.1

3 months ago

2.1.0

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

6 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago

0.2.4

9 years ago

0.2.3

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago

0.0.0

11 years ago