Skip to content

Instantly share code, notes, and snippets.

@snoj
Created November 4, 2017 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snoj/ee4b5d33958415a903608a274a4c4298 to your computer and use it in GitHub Desktop.
Save snoj/ee4b5d33958415a903608a274a4c4298 to your computer and use it in GitHub Desktop.
For when you're stuck in the middle
Function.prototype.bindMap = function(context) {
function nullfilter(v) { return v != null; }
function isnotfalse(v) { return v !== false; }
function getUndefinedIndex(v, i, a) { return typeof a[i] == 'undefined' ? i : false; }
var self = this;
var args = Array.prototype.slice.call(arguments, 1);
var unargs = args.map(getUndefinedIndex).filter(isnotfalse);
return function() {
var iargs = Array.prototype.slice.call(arguments, 0);
unargs.forEach(function(i, ii) {
args[i] = iargs[ii];
});
return self.apply(context, args);
};
};
function fargs(a,b,c,d,e,f) {
console.log(a,b,c,d,e,f);
}
var partfargs = fargs.bindMap(null, 1, undefined, 'mid', undefined, 'end', undefined);
partfargs("second", 3, Number.POSITIVE_INFINITY);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment