Skip to content

Instantly share code, notes, and snippets.

@nathggns
Last active December 20, 2015 19:29
Show Gist options
  • Save nathggns/6183733 to your computer and use it in GitHub Desktop.
Save nathggns/6183733 to your computer and use it in GitHub Desktop.
/**
* 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