Comparing version 0.0.4 to 0.0.5
25
index.js
var parser = require('./generated/funql-parser'); | ||
var makeCompiler = function (handlers) { | ||
var trim = function (str) { | ||
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); | ||
}; | ||
var makeCompiler = function (_handlers) { | ||
var handlers = {}; | ||
Object.keys(_handlers).forEach(function (originalKey) { | ||
originalKey.split(',').map(function (splitKey) { | ||
return trim(splitKey); | ||
}).forEach(function (trimKey) { | ||
handlers[trimKey] = _handlers[originalKey]; | ||
}); | ||
}); | ||
var compileNodes = function (context, nodes, extendContext) { | ||
@@ -35,3 +47,3 @@ return nodes.map(function (node) { | ||
return function (source) { | ||
return function (source, context) { | ||
var ast; | ||
@@ -43,3 +55,10 @@ if (typeof source === 'string') { | ||
} | ||
return compileNode({}, ast); | ||
context = context || {}; | ||
if (handlers.__wrap__) { | ||
ast = { | ||
type: '__wrap__', | ||
value: ast | ||
}; | ||
} | ||
return compileNode(context, ast); | ||
}; | ||
@@ -46,0 +65,0 @@ }; |
{ | ||
"name": "funql", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "FUNctional Query Language", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -56,2 +56,35 @@ /* global describe, it, before */ | ||
var argsCompile = funql.compiler({ | ||
call_add: function (args, compile, context) { | ||
args = args[1].args; | ||
return args.reduce(function (prev, curr, i) { | ||
return compile(prev) + compile(curr); | ||
}); | ||
}, | ||
name: function (value, compile, context) { | ||
return context[value]; | ||
} | ||
}); | ||
var wrapCompile = funql.compiler({ | ||
__wrap__: function (ast, compile) { | ||
return 'wrap(' + compile(ast) + ')'; | ||
}, | ||
name: function (value, compile) { | ||
return value; | ||
} | ||
}); | ||
var multiTypeCompile = funql.compiler({ | ||
call: function (args, compile) { | ||
return compile(args).join(''); | ||
}, | ||
arguments: function (args, compile) { | ||
return '(' + compile(args).join(',') + ')'; | ||
}, | ||
'name, number': function (value, compile) { | ||
return value; | ||
} | ||
}); | ||
describe('funql compiler', function () { | ||
@@ -73,2 +106,17 @@ it('should compile with identity compiler', function () { | ||
}); | ||
it('should allow passing context to compiler', function () { | ||
var source = 'add(x,y)'; | ||
var result = argsCompile(source, {x: 1, y: 2}); | ||
expect(result).to.equal(3); | ||
}); | ||
it('should allow __wrap__ pseudo node handler to wrap ast', function () { | ||
var source = 'x'; | ||
var result = wrapCompile(source); | ||
expect(result).to.equal('wrap(x)'); | ||
}); | ||
it('should allow multiple types per handler', function () { | ||
var source = 'foo(x,1)'; | ||
var result = multiTypeCompile(source); | ||
expect(result).to.equal(source); | ||
}); | ||
it('should compile an ast', function () { | ||
@@ -75,0 +123,0 @@ var source = 'foo(bar)'; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
66215
1977