diff --git a/packages/node-krl-stdlib/src/index.js b/packages/node-krl-stdlib/src/index.js index 27fc4e8a2..9b0d42b31 100644 --- a/packages/node-krl-stdlib/src/index.js +++ b/packages/node-krl-stdlib/src/index.js @@ -3,6 +3,60 @@ var _ = require("lodash"); var stdlib = {}; +var defVarArgOp = function(op, reducer){ + stdlib[op] = function(){ + if(arguments.length === 0){ + return; + } + var r = arguments[0]; + var i; + for(i = 1; i < arguments.length; i++){ + r = reducer(r, arguments[i]); + } + return r; + }; +}; + +defVarArgOp("||", function(r, a){ + return r || a; +}); +defVarArgOp("&&", function(r, a){ + return r && a; +}); +defVarArgOp("<", function(r, a){ + return r < a; +}); +defVarArgOp(">", function(r, a){ + return r > a; +}); +defVarArgOp("<=", function(r, a){ + return r <= a; +}); +defVarArgOp(">=", function(r, a){ + return r >= a; +}); +defVarArgOp("==", function(r, a){ + return r === a; +}); +defVarArgOp("!=", function(r, a){ + return r !== a; +}); +defVarArgOp("+", function(r, a){ + return r + a; +}); +defVarArgOp("-", function(r, a){ + return r - a; +}); +defVarArgOp("*", function(r, a){ + return r * a; +}); +defVarArgOp("/", function(r, a){ + return r / a; +}); +defVarArgOp("%", function(r, a){ + return r % a; +}); + stdlib["+"] = function(){ if(arguments.length === 0){ return; diff --git a/packages/node-krl-stdlib/src/tests.js b/packages/node-krl-stdlib/src/tests.js index 74fd5cec0..7cb89856b 100644 --- a/packages/node-krl-stdlib/src/tests.js +++ b/packages/node-krl-stdlib/src/tests.js @@ -15,6 +15,29 @@ var testFn = function(t, fn, args, expected, message){ t.deepEquals(stdlib[fn].apply(null, args), expected, message); }; +test("general operators", function(t){ + var tf = _.partial(testFn, t); + tf("+", [], void 0); + tf("+", [1], 1); + tf("+", [1, 2], 3); + tf("+", [1, 2, 3], 6); + tf("-", [1, 2, 3], -4); + + tf("||", [false, false], false); + tf("||", [true, false], true); + tf("&&", [true, false], false); + tf("&&", [true, true], true); + + tf("<", [1, 3], true); + tf("<", [3, 1], false); + + tf("*", [5, 2], 10); + tf("/", [4, 2], 2); + tf("%", [4, 2], 0); + + t.end(); +}); + test("general operators", function(t){ t.equals(stdlib.as(1, "String"), "1");