_overRest.js 1.1KB

12345678910111213141516171819202122232425262728293031323334353637
  1. var apply = require('./_apply');
  2. /* Built-in method references for those with the same name as other `lodash` methods. */
  3. var nativeMax = Math.max;
  4. /**
  5. * A specialized version of `baseRest` which transforms the rest array.
  6. *
  7. * @private
  8. * @param {Function} func The function to apply a rest parameter to.
  9. * @param {number} [start=func.length-1] The start position of the rest parameter.
  10. * @param {Function} transform The rest array transform.
  11. * @returns {Function} Returns the new function.
  12. */
  13. function overRest(func, start, transform) {
  14. start = nativeMax(start === undefined ? (func.length - 1) : start, 0);
  15. return function() {
  16. var args = arguments,
  17. index = -1,
  18. length = nativeMax(args.length - start, 0),
  19. array = Array(length);
  20. while (++index < length) {
  21. array[index] = args[start + index];
  22. }
  23. index = -1;
  24. var otherArgs = Array(start + 1);
  25. while (++index < start) {
  26. otherArgs[index] = args[index];
  27. }
  28. otherArgs[start] = transform(array);
  29. return apply(func, this, otherArgs);
  30. };
  31. }
  32. module.exports = overRest;