Last active
December 20, 2015 19:29
-
-
Save nathggns/6183733 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* This function allows you to prefill "parts" of a functions | |
* arguments. | |
* | |
* @param {Function} func The function to call | |
* @param {mixed} An argument | |
* @param {mixed} ... | |
* | |
* @return {Function} Function with arguments that're prefilled | |
* | |
* Example: | |
* | |
* var waitForOneSecond = util.partial(setTimeout, null, 1000); | |
* | |
* waitForOneSecond(function() { | |
* // ... | |
* }); | |
*/ | |
partial: function() { | |
var args = Array.prototype.slice.call(arguments); | |
var func = args.shift(); | |
return function() { | |
var passed = Array.prototype.slice.call(arguments); | |
var funcArgs = args.map(function(arg) { | |
if (arg === null) { | |
arg = passed.shift(); | |
} | |
return arg; | |
}); | |
funcArgs = funcArgs.concat(passed); | |
return func.apply(this, funcArgs); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment