Last active
August 29, 2015 14:14
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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