Created
May 10, 2018 16:06
-
-
Save aonghusonia/1d066cfc3690b256ccd003d1e92afe7e to your computer and use it in GitHub Desktop.
vargs node variable number of arguments with a callback
This file contains 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
const varg = (fn, arity) => (...args) => { | |
const l = args.length < arity ? args.length : arity; | |
return typeof args[l - 1] === 'function' | |
? fn( | |
...args.slice(0, -1), | |
...new Array(arity - l).fill(undefined), | |
args[l - 1] | |
) | |
: fn(...args, ...new Array(arity - args.length).fill(undefined)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment