Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

spread.js 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var apply = require('./_apply'),
  2. arrayPush = require('./_arrayPush'),
  3. baseRest = require('./_baseRest'),
  4. castSlice = require('./_castSlice'),
  5. toInteger = require('./toInteger');
  6. /** Error message constants. */
  7. var FUNC_ERROR_TEXT = 'Expected a function';
  8. /* Built-in method references for those with the same name as other `lodash` methods. */
  9. var nativeMax = Math.max;
  10. /**
  11. * Creates a function that invokes `func` with the `this` binding of the
  12. * create function and an array of arguments much like
  13. * [`Function#apply`](http://www.ecma-international.org/ecma-262/7.0/#sec-function.prototype.apply).
  14. *
  15. * **Note:** This method is based on the
  16. * [spread operator](https://mdn.io/spread_operator).
  17. *
  18. * @static
  19. * @memberOf _
  20. * @since 3.2.0
  21. * @category Function
  22. * @param {Function} func The function to spread arguments over.
  23. * @param {number} [start=0] The start position of the spread.
  24. * @returns {Function} Returns the new function.
  25. * @example
  26. *
  27. * var say = _.spread(function(who, what) {
  28. * return who + ' says ' + what;
  29. * });
  30. *
  31. * say(['fred', 'hello']);
  32. * // => 'fred says hello'
  33. *
  34. * var numbers = Promise.all([
  35. * Promise.resolve(40),
  36. * Promise.resolve(36)
  37. * ]);
  38. *
  39. * numbers.then(_.spread(function(x, y) {
  40. * return x + y;
  41. * }));
  42. * // => a Promise of 76
  43. */
  44. function spread(func, start) {
  45. if (typeof func != 'function') {
  46. throw new TypeError(FUNC_ERROR_TEXT);
  47. }
  48. start = start == null ? 0 : nativeMax(toInteger(start), 0);
  49. return baseRest(function(args) {
  50. var array = args[start],
  51. otherArgs = castSlice(args, 0, start);
  52. if (array) {
  53. arrayPush(otherArgs, array);
  54. }
  55. return apply(func, this, otherArgs);
  56. });
  57. }
  58. module.exports = spread;