rearg.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. var baseFlatten = require('./_baseFlatten'),
  2. createWrapper = require('./_createWrapper'),
  3. rest = require('./rest');
  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. * @since 3.0.0
  15. * @category Function
  16. * @param {Function} func The function to rearrange arguments for.
  17. * @param {...(number|number[])} indexes The arranged argument 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 rearg = rest(function(func, indexes) {
  29. return createWrapper(func, REARG_FLAG, undefined, undefined, undefined, baseFlatten(indexes, 1));
  30. });
  31. module.exports = rearg;