07d7dd3cc8b21aa1f95f62b67da1b9e95afc6db6
[yaffs-website] / node_modules / uncss / node_modules / lodash / invokeMap.js
1 var apply = require('./internal/apply'),
2     baseEach = require('./internal/baseEach'),
3     baseInvoke = require('./internal/baseInvoke'),
4     isArrayLike = require('./isArrayLike'),
5     isKey = require('./internal/isKey'),
6     rest = require('./rest');
7
8 /**
9  * Invokes the method at `path` of each element in `collection`, returning
10  * an array of the results of each invoked method. Any additional arguments
11  * are provided to each invoked method. If `methodName` is a function it's
12  * invoked for, and `this` bound to, each element in `collection`.
13  *
14  * @static
15  * @memberOf _
16  * @category Collection
17  * @param {Array|Object} collection The collection to iterate over.
18  * @param {Array|Function|string} path The path of the method to invoke or
19  *  the function invoked per iteration.
20  * @param {...*} [args] The arguments to invoke each method with.
21  * @returns {Array} Returns the array of results.
22  * @example
23  *
24  * _.invokeMap([[5, 1, 7], [3, 2, 1]], 'sort');
25  * // => [[1, 5, 7], [1, 2, 3]]
26  *
27  * _.invokeMap([123, 456], String.prototype.split, '');
28  * // => [['1', '2', '3'], ['4', '5', '6']]
29  */
30 var invokeMap = rest(function(collection, path, args) {
31   var index = -1,
32       isFunc = typeof path == 'function',
33       isProp = isKey(path),
34       result = isArrayLike(collection) ? Array(collection.length) : [];
35
36   baseEach(collection, function(value) {
37     var func = isFunc ? path : ((isProp && value != null) ? value[path] : undefined);
38     result[++index] = func ? apply(func, value, args) : baseInvoke(value, path, args);
39   });
40   return result;
41 });
42
43 module.exports = invokeMap;