Detalhes do pacote

jsepgen

LapwingLabs2451.0.6

Tiny module to generate code from the jsep AST

ast, generator, escodegen, tiny

readme (leia-me)

jsepgen

Tiny module (15 LOC) to generate code from the jsep AST.

Example

Convert all variables and functions to an object with the same key

var jsep = require('jsep');
var gen = require('gen');

// parse the expression into an AST
var ast = jsep('-A1 + SUM(A5, A6) + " %"')

// replace variables and functions
var expr = gen(ast, function(node) {
  if (node.type != 'Identifier') return node;
  node.name = '_.' + node.name;
})

expr // ((-_.A1 + _.SUM(_.A5, _.A6)) + "%")

It's pretty easy to turn this into a function:

var fn = new Function('_', 'return ' + expr);

var total = fn({
  A1: 2,
  A5: 5,
  A6: 6,
  SUM: function(a, b) { return a + b }
})

total // 9%

Installation

npm install jsepgen

Contact

License

MIT

changelog (log de mudanças)

1.0.6 / 2015-09-06

  • add precommit hook

1.0.5 / 2015-09-06

  • fix regression and add tests for arrays

1.0.4 / 2015-09-04

  • add support for arrays

1.0.3 / 2015-07-23

  • Merge pull request #1 from lapwinglabs/fix/array-syntax
  • add test for array vs object property syntax
  • correctly support array-like member expressions

1.0.2 / 2015-06-18

  • add a quick test
  • add support for rewriting portions of the AST

1.0.1 / 2015-06-17

  • fix module

1.0.1 / 2010-01-03

  • Initial release