Created
November 4, 2017 21:08
-
-
Save snoj/ee4b5d33958415a903608a274a4c4298 to your computer and use it in GitHub Desktop.
For when you're stuck in the middle
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
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