rearg.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. var baseFlatten = require('../internal/baseFlatten'),
  2. createWrapper = require('../internal/createWrapper'),
  3. restParam = require('./restParam');
  4. /** Used to compose bitmasks for wrapper metadata. */
  5. var REARG_FLAG = 256;
  6. /**
  7. * Creates a function that invokes `func` with arguments arranged according
  8. * to the specified indexes where the argument value at the first index is
  9. * provided as the first argument, the argument value at the second index is
  10. * provided as the second argument, and so on.
  11. *
  12. * @static
  13. * @memberOf _
  14. * @category Function
  15. * @param {Function} func The function to rearrange arguments for.
  16. * @param {...(number|number[])} indexes The arranged argument indexes,
  17. * specified as individual indexes or arrays of indexes.
  18. * @returns {Function} Returns the new function.
  19. * @example
  20. *
  21. * var rearged = _.rearg(function(a, b, c) {
  22. * return [a, b, c];
  23. * }, 2, 0, 1);
  24. *
  25. * rearged('b', 'c', 'a')
  26. * // => ['a', 'b', 'c']
  27. *
  28. * var map = _.rearg(_.map, [1, 0]);
  29. * map(function(n) {
  30. * return n * 3;
  31. * }, [1, 2, 3]);
  32. * // => [3, 6, 9]
  33. */
  34. var rearg = restParam(function(func, indexes) {
  35. return createWrapper(func, REARG_FLAG, undefined, undefined, undefined, baseFlatten(indexes));
  36. });
  37. module.exports = rearg;