Files
30-seconds-of-code/snippets/overArgs.md
30secondsofcode d1f222bc62 Travis build: 1467
2018-01-28 12:55:37 +00:00

473 B

overArgs

Creates a function that invokes the provided function with its arguments transformed.

Use Array.map() to apply transforms to args in combination with the spread operator (...) to pass the transformed arguments to fn.

const overArgs = (fn, transforms) => (...args) => fn(...args.map((val, i) => transforms[i](val)));
var func = overArgs(
  function(x, y) {
    return [x, y];
  },
  [square, doubled]
);
func(9, 3); // [81, 6]