rest.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. var baseRest = require('./_baseRest'),
  2. toInteger = require('./toInteger');
  3. /** Error message constants. */
  4. var FUNC_ERROR_TEXT = 'Expected a function';
  5. /**
  6. * Creates a function that invokes `func` with the `this` binding of the
  7. * created function and arguments from `start` and beyond provided as
  8. * an array.
  9. *
  10. * **Note:** This method is based on the
  11. * [rest parameter](https://mdn.io/rest_parameters).
  12. *
  13. * @static
  14. * @memberOf _
  15. * @since 4.0.0
  16. * @category Function
  17. * @param {Function} func The function to apply a rest parameter to.
  18. * @param {number} [start=func.length-1] The start position of the rest parameter.
  19. * @returns {Function} Returns the new function.
  20. * @example
  21. *
  22. * var say = _.rest(function(what, names) {
  23. * return what + ' ' + _.initial(names).join(', ') +
  24. * (_.size(names) > 1 ? ', & ' : '') + _.last(names);
  25. * });
  26. *
  27. * say('hello', 'fred', 'barney', 'pebbles');
  28. * // => 'hello fred, barney, & pebbles'
  29. */
  30. function rest(func, start) {
  31. if (typeof func != 'function') {
  32. throw new TypeError(FUNC_ERROR_TEXT);
  33. }
  34. start = start === undefined ? start : toInteger(start);
  35. return baseRest(func, start);
  36. }
  37. module.exports = rest;