Micro Expressions for NodeJS
Lightweight and easily extensible string-based expression engine
Goals & Design
I created this tiny project to read (and parse) collections from JSON files that contained simple conditionals. As my architecture grew, I started using the same expressions for almost anything – even to query several databases. Keeping one simplistic language throughout my ecosystem simplified both development and maintenance for me.
Installation
Install using npm:
$ npm install micro-expressions
Usage
Newly created MicroExpressions take one argument: the expression
.
The input is being parsed automagically and turned into functions that take a data container as argument for rendering.
const MicroExpression = ; expression ;
Use render_with( data_container )
to render all templates using data_container
as context.
expression;
As MicroExpression is using lodash's get helper, it supports object paths in dot notation:
'foo.bar < 10';
Extending
To create micro expressions with your own set of operators, you can extend the MicroExpression class and overwrite the default behavior by declaring an override_operators()
and/or a get( expression )
method.
const MicroExpression = ; { ... } { ... }
Overriding Operators
The override_operators
method takes no arguments, but must return an object with the operators as keys and according callbacks as values. The callback takes 2 arguments: left
and right
, representing both sides of the operator. If your operator e.g. doesn't have a left side, you must still declare a left parameter, but can ignore the passed argument.
{ return !right; }
Overriding Templating
The get
method takes one argument: expression. All operands are passed to this method before the entire expression is being evaluated.
In its simplest form, you can override get
like so:
const MicroExpression = ; { return dataexpression || expression; }
.render
and .render_with
Hooking into You can declare a method on_render
that gets passed the current rendering results as single argument.
Examples
In the following example, I used the MicroExpression class to parse conditions loaded from a JSON file:
[steps.json]
[index.js]
let message = 'maybe.';const steps = ;const answer = 'no'; _; console;
The output will be:
The outcome is negative
Here is an example of extending MicroExpression:
{ return left === right left !== right !right Math < 10 }
Make sure you check out the subclass MongoExpression and the tests to see examples of implementation.