1 var apply = require('./_apply'),
2 arrayPush = require('./_arrayPush'),
3 rest = require('./rest'),
4 toInteger = require('./toInteger');
6 /** Used as the `TypeError` message for "Functions" methods. */
7 var FUNC_ERROR_TEXT = 'Expected a function';
9 /* Built-in method references for those with the same name as other `lodash` methods. */
10 var nativeMax = Math.max;
13 * Creates a function that invokes `func` with the `this` binding of the created
14 * function and an array of arguments much like [`Function#apply`](https://es5.github.io/#x15.3.4.3).
16 * **Note:** This method is based on the [spread operator](https://mdn.io/spread_operator).
21 * @param {Function} func The function to spread arguments over.
22 * @param {number} [start=0] The start position of the spread.
23 * @returns {Function} Returns the new function.
26 * var say = _.spread(function(who, what) {
27 * return who + ' says ' + what;
30 * say(['fred', 'hello']);
31 * // => 'fred says hello'
33 * var numbers = Promise.all([
34 * Promise.resolve(40),
38 * numbers.then(_.spread(function(x, y) {
41 * // => a Promise of 76
43 function spread(func, start) {
44 if (typeof func != 'function') {
45 throw new TypeError(FUNC_ERROR_TEXT);
47 start = start === undefined ? 0 : nativeMax(toInteger(start), 0);
48 return rest(function(args) {
49 var array = args[start],
50 otherArgs = args.slice(0, start);
53 arrayPush(otherArgs, array);
55 return apply(func, this, otherArgs);
59 module.exports = spread;