ianchi / espression Goto Github PK
View Code? Open in Web Editor NEWECMAScript Expressions parser and static eval
Home Page: https://ianchi.github.io/ESpression-tester/
License: MIT License
ECMAScript Expressions parser and static eval
Home Page: https://ianchi.github.io/ESpression-tester/
License: MIT License
The example in README doesn't run. It seems that ES5Eval
had been renamed to ES5StaticEval
. BTW, is there a complete doc for the API?
https://ianchi.github.io/ESpression-tester/
Context
{"a": 1}
Expression
a.match()
Parsed AST
"can't access property \"apply\", s is undefined"
Two problems:
can't call function, a.match is undefined
.My use case is that I would like to allow admin users of a service to define expressions that would evaluate objects to either pass or reject them. The expression would be evaluated server-side.
From the readme, it's not really clear whether passing an expression through this library is intended to make evaluating such an expression safe?
https://github.com/eight04/espression-bundle
https://github.com/eight04/expression-eval-bundle
filename\library | espression | expression-eval |
---|---|---|
.js | 55KB | 24KB |
.min.js | 28KB | 7KB |
BTW, what I need is jsep with regexp literal. Should I just use EricSmekens/jsep#78 instead?
Thanks for awesome library.
I've found that there's no async function support, similar to donmccurdy/expression-eval. any plans to add it?
I used to use jsep
and I am switching to ESpression
. I had my own eval function, which is already provided by ESpression
, but I also had a function to find the names of variables used in an AST. This is useful to reevaluate some expressions when the referenced variables change. Could this feature be useful for other developers and be implemented in the module? Maybe by providing an optional parameter to parse
that could collect some information, or call a handler for each visited node.
For information, this was my implementation to walk the AST and gather variable names:
function _vars(set, ...nodes)
{
for (let node of nodes)
switch (node.type) {
case 'Literal':
case 'ThisExpression':
break;
case 'Compound':
_vars(set, ...node.body);
break;
case 'ArrayExpression':
_vars(set, ...node.elements);
break;
case 'UnaryExpression':
_vars(set, node.argument);
break;
case 'LogicalExpression':
case 'BinaryExpression':
_vars(set, node.left, node.right);
break;
case 'ConditionalExpression':
_vars(set, node.test, node.consequent, node.alternate);
break;
case 'CallExpression':
_vars(set, node.callee, ...node.arguments);
break;
case 'Identifier':
set.add(node.name);
break;
case 'MemberExpression':
switch (node.object.type) {
case 'Identifier':
set.add(node.object.name);
break;
case 'ThisExpression':
set.add(node.property.name);
break;
case 'MemberExpression':
_vars(set, node.object);
break;
default:
throw new Error(`Unhandled member expression ${node.object.type}`);
}
break;
default:
throw new Error(`unhandled node type ${node.type}`);
}
}
export function vars(ast)
{
const set = new Set();
_vars(set, ast);
return set;
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.