Skip to content

Instantly share code, notes, and snippets.

@Gwash3189
Last active August 29, 2015 14:14
Show Gist options
  • Save Gwash3189/0411a43bfa2616b1687b to your computer and use it in GitHub Desktop.
Save Gwash3189/0411a43bfa2616b1687b to your computer and use it in GitHub Desktop.
Pass unlimited amount of functions to compose to create a new function that builds on previous values.
var reverseForEach = function(arr, fun) {
for (var i = arr.length - 1; i > -1; --i) {
fun(arr[i], i, arr);
}
}
var compose = function() {
var funcs = Object.keys(arguments);
var outterArgs = arguments;
return function() {
var value;
var innerArgs = arguments;
reverseForEach(funcs, function(funcName) {
if (value !== undefined) {
value = outterArgs[funcName].call(null, value);
} else {
value = outterArgs[funcName].apply(null, innerArgs);
}
});
return value;
};
};
var Not = function(x) {
return !x;
}
var createNotFunctions = function(is){
is.Not = {};
Object.keys(is).forEach(function(prop){
if(prop !== "Not"){
is.Not[prop] = function() {
return compose(Not, is[prop]).apply(null, arguments);
}
}
});
return is;
}
var is = {
Undefined: function(value) {
return value === undefined;
},
Defined: function() {
return compose(Not, is.Undefined).apply(null, arguments);
}
};
module.exports = createNotFunctions(is);
var is = require("./compose.js");
console.log(is.Undefined(undefined)); // true
console.log(is.Defined(undefined)); // false
console.log(is.Not.Defined(undefined)); // true
console.log(is.Not.Undefined(undefined)); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment