I wanted a way to load in flow from configuration as well as to perform free form transformation of object->object. Seemed a good fit for Lambda functions.
Looking at the existing libraries they were either heavy weight (5to6) or didn't have support for ...rest, shorthanding, and Source->Code->JSON
So I took some work on a basic Lambda expression parser I'd done in the past and expanded on it.
Install
npm install lambda-30
Testing
npm test
Usage
Basic
var L = require('lambda-30').Lambda;
var f = L('(a, b)=>a+b');
var val = f(5, 8);
// val is 13
f = L('$.a+$.b'); // No params supplied used $ as self reference
val = f({a: 3, b: 5});
// val is 8
Options
var Lambda = require('lambda-30').Lambda;
var L = new Lambda({selfSymbol: 'self'});
var f = L.compile('self.a+self.b');
var val = f({a: 3, b: 5});
// val is 8
Multi-line
var L = require('lambda-30').Lambda;
// note: long string form used for convience only
var f = L(`(a, b)=>{
var c = 2;
var sum = a+b;
var avg = sum / c;
return {
count: c,
sum: sum,
avg: avg
};
}`);
...rest
var L = require('lambda-30').Lambda;
// note: long string form used for convience only
var f = L(`(...nums)=>{
var c = nums.length;
var sum = nums.reduce(function(curr, next){
return curr + next;
}, 0);
var avg = sum / c;
return {
count: c,
sum: sum,
avg: avg
};
}`);
toJSON
Really this should be called toString but I didn't want to override the default toString implementation.
var L = require('lambda-30').Lambda;
var expected = '(a, b)=>{return a+b}';
var e = L(expected);
var js = e.toJSON();
assert(js===expected);
var e = Lambda(function($){
return $.a+$.b;
});
var s = e.toJSON();
var expected = '($)=>{\n return $.a+$.b;\n }';
assert(s===expected);