I'm building a node app in which users are (ideally) able to define styling - for geographic data - using a series of JSON objects:
{
"style":
{
"test": "year",
"condition": "<= 1954 AND >= 1936",
"color": "red"
}
}
In the case above, I like to evaluate that style as
if (year <= 1954 && year >= 1936){
object.color = red;
}
Is there an easy way to parse + evaluate such expressions/build them from such an object? I'm especially interested in letting people string together plex expressions built using <=, >=, ||, && etc.
I'd like to avoid using eval(), if possible.
I'm building a node app in which users are (ideally) able to define styling - for geographic data - using a series of JSON objects:
{
"style":
{
"test": "year",
"condition": "<= 1954 AND >= 1936",
"color": "red"
}
}
In the case above, I like to evaluate that style as
if (year <= 1954 && year >= 1936){
object.color = red;
}
Is there an easy way to parse + evaluate such expressions/build them from such an object? I'm especially interested in letting people string together plex expressions built using <=, >=, ||, && etc.
I'd like to avoid using eval(), if possible.
Share Improve this question edited Jul 4, 2013 at 3:58 sirKitty asked Jul 4, 2013 at 3:23 sirKittysirKitty 1432 silver badges11 bronze badges 2- 1 You might want to check out my project language called Bianca: github./aaditmshah/bianca. It parses a program and spits out a JSON AST, which is kind of the exact opposite of what your program does. Nevertheless it may give you some valuable insights. – Aadit M Shah Commented Jul 4, 2013 at 4:27
- I definitely will check it out! Thanks! – sirKitty Commented Jul 4, 2013 at 4:42
2 Answers
Reset to default 6If you don't wish to use eval, you would have to write your own little parser and create a definition language like this:
"condition": ["and", ["<=", 1954], [">=", 1936]],
This is a partial implementation you could consider:
function do_and(args, value)
{
for (var i = 0; i < args.length; ++i) {
if (!evaluate(args[i], value)) {
return false;
}
}
return true;
}
function evaluate(condition, value)
{
switch (condition[0]) {
case "and":
return do_and(condition.slice(1), value);
case "<=":
return value <= condition[1];
case ">=":
return value >= condition[1];
}
}
This is how you would use it:
var style = {
"test": "year",
"condition": ["and", ["<=", 1954], [">=", 1936]],
"color": "red"
}, context = {
"year": 1940
};
if (evaluate(style.condition, context[style.test])) {
console.log(style.color); // "red"
}
Demo
Something like
var obj = JSON.parse(str);
switch (obj.style.operator){
case '=':
if (window[obj.style.condition] === obj.style){//assuming that the conditions are global
object.color = obj.style;
}
break;
...
}