SIGN IN SIGN UP

A modern JavaScript utility library delivering modularity, performance, & extras.

0 0 43 JavaScript
2016-11-14 20:54:18 -08:00
define(['./_apply', './_baseEach', './_baseInvoke', './_baseRest', './isArrayLike'], function(apply, baseEach, baseInvoke, baseRest, isArrayLike) {
2015-12-16 17:52:15 -08:00
2015-01-08 00:37:01 -08:00
/**
2015-12-16 17:50:42 -08:00
* Invokes the method at `path` of each element in `collection`, returning
2015-12-16 17:49:35 -08:00
* an array of the results of each invoked method. Any additional arguments
2016-07-24 09:52:04 -07:00
* are provided to each invoked method. If `path` is a function, it's invoked
* for, and `this` bound to, each element in `collection`.
2015-01-08 00:37:01 -08:00
*
* @static
* @memberOf _
2016-03-26 00:00:01 -07:00
* @since 4.0.0
2015-01-08 00:37:01 -08:00
* @category Collection
2015-12-16 17:53:20 -08:00
* @param {Array|Object} collection The collection to iterate over.
2015-12-16 17:49:35 -08:00
* @param {Array|Function|string} path The path of the method to invoke or
2015-01-08 00:37:01 -08:00
* the function invoked per iteration.
2015-12-16 17:53:20 -08:00
* @param {...*} [args] The arguments to invoke each method with.
2015-01-08 00:37:01 -08:00
* @returns {Array} Returns the array of results.
* @example
*
2015-12-16 17:53:20 -08:00
* _.invokeMap([[5, 1, 7], [3, 2, 1]], 'sort');
2015-01-08 00:37:01 -08:00
* // => [[1, 5, 7], [1, 2, 3]]
*
2015-12-16 17:53:20 -08:00
* _.invokeMap([123, 456], String.prototype.split, '');
2015-01-08 00:37:01 -08:00
* // => [['1', '2', '3'], ['4', '5', '6']]
*/
2016-07-24 09:52:04 -07:00
var invokeMap = baseRest(function(collection, path, args) {
2015-12-16 17:49:07 -08:00
var index = -1,
2015-12-16 17:49:35 -08:00
isFunc = typeof path == 'function',
2015-12-16 17:50:05 -08:00
result = isArrayLike(collection) ? Array(collection.length) : [];
2015-12-16 17:49:07 -08:00
baseEach(collection, function(value) {
2016-11-14 20:54:18 -08:00
result[++index] = isFunc ? apply(path, value, args) : baseInvoke(value, path, args);
2015-12-16 17:49:07 -08:00
});
return result;
});
2015-01-08 00:37:01 -08:00
2015-12-16 17:53:20 -08:00
return invokeMap;
2015-01-08 00:37:01 -08:00
});