Skip to content

olalonde/expressionist

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Expressionist

Build Status NPM version

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.

Install

npm install expressionist

Usage

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 = {
  or: function (operands) {
    return ' (' + operands.join(' OR ') + ') ';
  },
  and: function (operands) {
    return ' (' + operands.join(' AND ') + ') ';
  },
  eql: function (operands) {
    return operands[0] + ' = "' + operands[1] + '"';
  },
  notEql: function (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") )
*/

Roadmap

  • Syntax sugar.
  • Operator precedence.
  • Browser friendly build.
  • Support this:
var MathExpr = require('expressionist')({
  eql: function (l, r) { return l === right; },
  gt: function (l, r) { return l > r; }
});
// build expression (...) and then
var result = expr.eval();
  • Nicer stringified representations and ability to transform to different representations.

License

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

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published