{"_id":"static-eval2","_rev":"112150","name":"static-eval2","description":"evaluate statically-analyzable expressions","dist-tags":{"latest":"2.0.1"},"maintainers":[{"name":"goto-bus-stop","email":""}],"time":{"modified":"2021-06-03T10:44:12.000Z","created":"2019-02-15T14:50:37.514Z","2.0.1":"2019-02-15T14:50:37.514Z"},"users":{},"author":{"name":"James Halliday","email":"mail@substack.net","url":"http://substack.net"},"repository":{"type":"git","url":"git://github.com/goto-bus-stop/static-eval.git"},"versions":{"2.0.1":{"name":"static-eval2","version":"2.0.1","description":"evaluate statically-analyzable expressions","main":"index.js","dependencies":{"escodegen":"^1.8.1"},"devDependencies":{"esprima":"^2.7.3","tape":"^4.6.0"},"scripts":{"test":"tape test/*.js"},"testling":{"files":"test/*.js","browsers":["ie/8..latest","ff/latest","chrome/latest","opera/latest","safari/latest"]},"repository":{"type":"git","url":"git://github.com/goto-bus-stop/static-eval.git"},"homepage":"https://github.com/goto-bus-stop/static-eval","keywords":["static","eval","expression","esprima","ast","abstract","syntax","tree","analysis"],"author":{"name":"James Halliday","email":"mail@substack.net","url":"http://substack.net"},"license":"MIT","gitHead":"19ee7ab4e4b1f74a67322b23d0222e4a574fa049","bugs":{"url":"https://github.com/goto-bus-stop/static-eval/issues"},"_id":"static-eval2@2.0.1","_nodeVersion":"11.9.0","_npmVersion":"6.7.0","dist":{"shasum":"a241d9c3ad1d08ac8598ca28bc0ef408ec76e4a0","size":4079,"noattachment":false,"key":"/static-eval2/-/static-eval2-2.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/static-eval2/download/static-eval2-2.0.1.tgz"},"maintainers":[{"name":"goto-bus-stop","email":""}],"_npmUser":{"name":"goto-bus-stop","email":"renee@kooi.me"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/static-eval2_2.0.1_1550242237277_0.1054566740440317"},"_hasShrinkwrap":false,"publish_time":1550242237514,"_cnpm_publish_time":1550242237514}},"readme":"# static-eval\n\nevaluate statically-analyzable expressions\n\n[![testling badge](https://ci.testling.com/substack/static-eval.png)](https://ci.testling.com/substack/static-eval)\n\n[![build status](https://secure.travis-ci.org/substack/static-eval.png)](http://travis-ci.org/substack/static-eval)\n\n# example\n\n``` js\nvar evaluate = require('static-eval');\nvar parse = require('esprima').parse;\n\nvar src = process.argv[2];\nvar ast = parse(src).body[0].expression;\n\nconsole.log(evaluate(ast));\n```\n\nIf you stick to simple expressions, the result is statically analyzable:\n\n```\n$ node '7*8+9'\n65\n$ node eval.js '[1,2,3+4*5-(5*11)]'\n[ 1, 2, -32 ]\n```\n\nbut if you use statements, undeclared identifiers, or syntax, the result is no\nlonger statically analyzable and `evaluate()` returns `undefined`:\n\n```\n$ node eval.js '1+2+3*n'\nundefined\n$ node eval.js 'x=5; x*2'\nundefined\n$ node eval.js '5-4*3'\n-7\n```\n\nYou can also declare variables and functions to use in the static evaluation:\n\n``` js\nvar evaluate = require('static-eval');\nvar parse = require('esprima').parse;\n\nvar src = '[1,2,3+4*10+n,foo(3+5),obj[\"\"+\"x\"].y]';\nvar ast = parse(src).body[0].expression;\n\nconsole.log(evaluate(ast, {\n    n: 6,\n    foo: function (x) { return x * 100 },\n    obj: { x: { y: 555 } }\n}));\n```\n\n# methods\n\n``` js\nvar evaluate = require('static-eval');\n```\n\n## evaluate(ast, vars={})\n\nEvaluate the [esprima](https://npmjs.org/package/esprima)-parsed abstract syntax\ntree object `ast` with an optional collection of variables `vars` to use in the\nstatic expression resolution.\n\nIf the expression contained in `ast` can't be statically resolved, `evaluate()`\nreturns undefined.\n\n# install\n\nWith [npm](https://npmjs.org) do:\n\n```\nnpm install static-eval\n```\n\n# license\n\nMIT\n","_attachments":{},"homepage":"https://github.com/goto-bus-stop/static-eval","bugs":{"url":"https://github.com/goto-bus-stop/static-eval/issues"},"license":"MIT"}