Version 1
[yaffs-website] / node_modules / grunt-legacy-util / node_modules / lodash / spread.js
1 var apply = require('./_apply'),
2     arrayPush = require('./_arrayPush'),
3     rest = require('./rest'),
4     toInteger = require('./toInteger');
5
6 /** Used as the `TypeError` message for "Functions" methods. */
7 var FUNC_ERROR_TEXT = 'Expected a function';
8
9 /* Built-in method references for those with the same name as other `lodash` methods. */
10 var nativeMax = Math.max;
11
12 /**
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).
15  *
16  * **Note:** This method is based on the [spread operator](https://mdn.io/spread_operator).
17  *
18  * @static
19  * @memberOf _
20  * @category Function
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.
24  * @example
25  *
26  * var say = _.spread(function(who, what) {
27  *   return who + ' says ' + what;
28  * });
29  *
30  * say(['fred', 'hello']);
31  * // => 'fred says hello'
32  *
33  * var numbers = Promise.all([
34  *   Promise.resolve(40),
35  *   Promise.resolve(36)
36  * ]);
37  *
38  * numbers.then(_.spread(function(x, y) {
39  *   return x + y;
40  * }));
41  * // => a Promise of 76
42  */
43 function spread(func, start) {
44   if (typeof func != 'function') {
45     throw new TypeError(FUNC_ERROR_TEXT);
46   }
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);
51
52     if (array) {
53       arrayPush(otherArgs, array);
54     }
55     return apply(func, this, otherArgs);
56   });
57 }
58
59 module.exports = spread;