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 complex expressions built using <=, >=, ||, && etc.
I'd like to avoid using eval(), if possible.
If 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"
}
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;
...
}