expressionist

Programmatically generate expression trees in Node.js or plain browser Javascript.

Expressionist

Generate your own DSLs using native Javascript syntax. It lets you do cool stuff like delay the execution of a bunch of methods until the methods have been defined. This makes it easy to pass expressions around and let various adapters decide how they want to execute the expression.

Expressionist lets you programmatically generate expression trees in Node.js and then evaluate it after you have defined operation mapppings.

Work in progress.

npm install expressionist
var WhereExpression = require('expressionist')([
  'or', 'and', 'eql', 'notEql', 'in'
]);
 
var _ = WhereExpression.start;
 
var res = 
  _('country')
  .eql('Canada')
  .or(
    _('country').eql('USA')
    .and(_('job').notEql('spy'))
    .and(_('company').notEql('nsa'))
  );
 
var pretty = res.pretty();
 
console.log(util.inspect(pretty, null, 10));
 
/* 
Outputs:
 
  { or:
   [ { eql: [ 'country', 'Canada' ] },
     { and:
        [ { eql: [ 'country', 'USA' ] },
          { notEql: [ 'job', 'spy' ] },
          { notEql: [ 'company', 'nsa' ] } ] } ] }
 
*/
 
 
var map = {
  orfunction (operands) {
    return ' (' + operands.join(' OR ') + '';
  },
  andfunction (operands) {
    return ' (' + operands.join(' AND ') + '';
  },
  eqlfunction (operands) {
    return operands[0] + ' = "' + operands[1] + '"';
  },
  notEqlfunction (operands) {
    return operands[0] + ' != "' + operands[1] + '"';
  }
};
 
var res = expr.evaluate(map);
 
console.log(res);
 
/*
Outpus:
 
 (country = "Canada" OR  (country = "USA" AND job != "spy" AND company != "nsa") )
*/
  • Syntax sugar.
  • Operator precedence.
  • Browser friendly build.
  • Support this:
var MathExpr = require('expressionist')({
  eqlfunction (lr) { return l === right; },
  gtfunction (lr) { return l > r; }
});
// build expression (...) and then 
var result = expr.eval();
  • Nicer stringified representations and ability to transform to different representations.

MIT: http://olalonde.mit-license.org