shepherdwind / simple-evaluate Goto Github PK
View Code? Open in Web Editor NEWA safe parse for simple js expression
Home Page: https://www.yuque.com/eward/bt8ny4/bgc0gx
License: MIT License
A safe parse for simple js expression
Home Page: https://www.yuque.com/eward/bt8ny4/bgc0gx
License: MIT License
We stumbled upon another small issue for our evaluations with more complex formulae, hope you can look into it again:
'v1_3==v1_1?1:0+v2_2==v2_1?1:0+v3_1==v3_1?1:0+v4_3==v4_1?1:0'
expected: 1, received: 1 ✅
the same with parentheses
'(v1_3==v1_1?1:0)+(v2_2==v2_1?1:0)+(v3_1==v3_1?1:0)+(v4_3==v4_1?1:0)'
expected: 1, received: 4 ⛔
all conditions match, no parentheses
'v1_3==v1_3?1:0+v2_2==v2_2?1:0+v3_1==v3_1?1:0+v4_3==v4_3?1:0'
expected: 4, received: 1 ⛔
parentheses but all blocks are true
'(g1_1==g1_1?1:0)+(g2_1==g2_1?1:0)+(g3_1==g3_1?1:0)+(g4_1==g4_1?1:0)+(g5_1==g5_1?1:0)'
expected: 5, received: 5 ✅
not all match
'(g1_1==g1_2?1:0)+(g2_1==g2_1?1:0)+(g3_1==g3_4?1:0)+(g4_1==g4_1?1:0)+(g5_1==g5_1?1:0)'
expected: 3, received: 5 ⛔
Thanks in advance!
This would be helpful for some environments if possible, thanks.
Hi there!
Evaluating this formula (-0.02*(1+5+3+1+5+3+1))+(0.07*(3+5+3+3+3+1+3+0))+(0.15*(3+1+5+1+5+3+1))-4.36
will fail with the following error:
Error: operation must be string, but get {"left":{"left":{"left":{"left":{"left":{"left":"1","right":"5","operation":"+"},"right":"3","operation":"+"},"right":"1","operation":"+"},"right":"5","operation":"+"},"right":"3","operation":"+"},"right":"1","operation":"+","grouped":true}
The reason seems to be the additional / unnecessary parentheses if I'm not mistaken;
evaluating it like this: -0.02*(1+5+3+1+5+3+1)+0.07*(3+5+3+3+3+1+3+0)+0.15*(3+1+5+1+5+3+1)-4.36
works fine.
Sometimes additional parentheses are useful for clarity, especially for our team members working with large json files. Ideally this should not throw errors.
Hello,
I would love to see support for templating strings.
This would allow basic support for file templates and easier syntax for evaluating strings.
evaluate({a:22}, "'I am ' + a + ' years old today'")
vs
evaluate({a:22}, "\`I am ${a} years old today\`")
If you still keep the other restrictions, that's fine, but allowing simple expressions within ${} would be extremely useful.
When using simple modulo operations like '4 % 2'
I always get undefined
as result.
It seems that the modulo operation is missing as case in the big switch expressions in compiler.ts
and shunting-yard.ts
switch (node.operation) {
case '*':
return left * right;
case '/':
return left / right;
case '%': <-- this one is missing
return left % right;
Me again :)
I found an issue regarding the evaluation of ternary operator expressions:
5 == 5 ? 4 : 1
results in 4, good
1 == 1 ? 1 : 0
results in 1, good
0 == 0 ? 0 : 1
results in 1, unexpected; apparently 0 is not equal to 0
just 0 == 0
results in true though, so it must be something with the ternary operator
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.