3 ms·
Here's the thing: I implemented exactly this for a side project where I needed users to be able to express simple logic for custom data handling rules. Here are
by eevilspock 5y ago
Here's the thing: I implemented exactly this for a side project where I needed users to be able to express simple logic for custom data handling rules. Here are some examples:
level = new Expression(['map', ['char', ['tail', 2], 0], {'=': 1, '-': 2}])
body = new Expression(['parse', ['trim', ['body']]])
name = new Expression(['replace', ['trim', ['body', 1]], /[ \t\r\n]+/, ' '])
target = new Expression(['first', ['body', 2], ['body', 3], ''])
The arg to `new Expression` is a Javascript object (i.e. can be sourced from JSON). The first element in each nested array is a function name/operator. It was absolutely trivial to implement. All the functions are implemented in a few lines of code within cases of a switch statement, and nested expressions are handled recursively.
For me this is just a temporary throwaway solution while I work on other more critical stuff first. I will replace it with an s-expression based DSL later. Like JSONLogic, it won't be Turing complete.
It just seems to me that JSONLogic is making a huge deal for what's really just a small reusable library.