Created
February 25, 2019 23:02
-
-
Save kraftwerk28/b75713f3996ac4bb31b09f4f496a9627 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
'use strict'; | |
/** | |
* Binds function to positioned arguments | |
* posargs must be like { 2: one_arg, 100: another_arg, 0: another_arg } | |
* @param {Function} fn | |
* @param {Object} posargs | |
*/ | |
const positionalBind = (fn, posargs) => (...args) => { | |
const indexes = Object.keys(posargs).map(_ => +_).sort(); | |
const realArgs = []; | |
while (indexes.length) { | |
realArgs.push( | |
indexes[0] === realArgs.length | |
? posargs[indexes.shift()] | |
: args.shift()); | |
} | |
return fn(...realArgs, ...args); | |
}; | |
function fun(a, b, c, d, e) { | |
console.log(arguments); | |
}; | |
const bnd = positionalBind(fun, { 1: 12, 3: 21, 0: 33 }); | |
bnd(44, 55); // [33, 12, 44, 21, 55] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Without "While() {}"