Skip to content

Instantly share code, notes, and snippets.

@aonghusonia
Created May 10, 2018 16:06
Show Gist options
  • Save aonghusonia/1d066cfc3690b256ccd003d1e92afe7e to your computer and use it in GitHub Desktop.
Save aonghusonia/1d066cfc3690b256ccd003d1e92afe7e to your computer and use it in GitHub Desktop.
vargs node variable number of arguments with a callback
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