You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.3 KiB

4 years ago
  1. /**
  2. * lodash 3.6.1 (Custom Build) <https://lodash.com/>
  3. * Build: `lodash modern modularize exports="npm" -o ./`
  4. * Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/>
  5. * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
  6. * Copyright 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  7. * Available under MIT license <https://lodash.com/license>
  8. */
  9. /** Used as the `TypeError` message for "Functions" methods. */
  10. var FUNC_ERROR_TEXT = 'Expected a function';
  11. /* Native method references for those with the same name as other `lodash` methods. */
  12. var nativeMax = Math.max;
  13. /**
  14. * Creates a function that invokes `func` with the `this` binding of the
  15. * created function and arguments from `start` and beyond provided as an array.
  16. *
  17. * **Note:** This method is based on the [rest parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters).
  18. *
  19. * @static
  20. * @memberOf _
  21. * @category Function
  22. * @param {Function} func The function to apply a rest parameter to.
  23. * @param {number} [start=func.length-1] The start position of the rest parameter.
  24. * @returns {Function} Returns the new function.
  25. * @example
  26. *
  27. * var say = _.restParam(function(what, names) {
  28. * return what + ' ' + _.initial(names).join(', ') +
  29. * (_.size(names) > 1 ? ', & ' : '') + _.last(names);
  30. * });
  31. *
  32. * say('hello', 'fred', 'barney', 'pebbles');
  33. * // => 'hello fred, barney, & pebbles'
  34. */
  35. function restParam(func, start) {
  36. if (typeof func != 'function') {
  37. throw new TypeError(FUNC_ERROR_TEXT);
  38. }
  39. start = nativeMax(start === undefined ? (func.length - 1) : (+start || 0), 0);
  40. return function() {
  41. var args = arguments,
  42. index = -1,
  43. length = nativeMax(args.length - start, 0),
  44. rest = Array(length);
  45. while (++index < length) {
  46. rest[index] = args[start + index];
  47. }
  48. switch (start) {
  49. case 0: return func.call(this, rest);
  50. case 1: return func.call(this, args[0], rest);
  51. case 2: return func.call(this, args[0], args[1], rest);
  52. }
  53. var otherArgs = Array(start + 1);
  54. index = -1;
  55. while (++index < start) {
  56. otherArgs[index] = args[index];
  57. }
  58. otherArgs[start] = rest;
  59. return func.apply(this, otherArgs);
  60. };
  61. }
  62. module.exports = restParam;