Add _.spread.

This commit is contained in:
Nicolas Chambrier
2015-02-07 20:28:34 +01:00
committed by jdalton
parent ba4da24984
commit e91a662491
2 changed files with 89 additions and 1 deletions

View File

@@ -7579,6 +7579,45 @@
return createWrapper(func, REARG_FLAG, null, null, null, indexes);
}
/**
* Creates a function accepting an array as argument, that invokes `func`
* with arguments taken from this array, like `Function#apply`.
* This works obviously well with `Promise.all`.
*
* @static
* @memberOf _
* @category Function
* @param {Function} The function to alter.
* @param {*} [thisArg] The `this` binding of `func`.
* @returns {*} Returns the new function.
* @example
*
* var spread = _.spread(function (who, what) {
* return who + ' says ' + what;
* });
*
* spread(['John', 'hello']) // => 'John says hello'
*
* // With Promise
* var numbers = Promise.all([
* Promise.resolve(42),
* Promise.resolve(33)
* ])
*
* numbers.then(_.spread(function (a, b) { return a + b })) // Promise of 75
* // instead of...
* numbers.then(function (nums) { return nums[0] + nums[1] })
*/
function spread (func, thisArg) {
if (typeof func !== 'function') {
throw new TypeError(FUNC_ERROR_TEXT);
}
return function(array) {
return func.apply(thisArg, array);
};
}
/**
* Creates a function that only invokes `func` at most once per every `wait`
* milliseconds. The created function comes with a `cancel` method to cancel
@@ -10752,6 +10791,7 @@
lodash.slice = slice;
lodash.sortBy = sortBy;
lodash.sortByAll = sortByAll;
lodash.spread = spread;
lodash.take = take;
lodash.takeRight = takeRight;
lodash.takeRightWhile = takeRightWhile;