1 var baseEach = require('../internal/baseEach'),
2 invokePath = require('../internal/invokePath'),
3 isArrayLike = require('../internal/isArrayLike'),
4 isKey = require('../internal/isKey'),
5 restParam = require('../function/restParam');
8 * Invokes the method at `path` of each element in `collection`, returning
9 * an array of the results of each invoked method. Any additional arguments
10 * are provided to each invoked method. If `methodName` is a function it's
11 * invoked for, and `this` bound to, each element in `collection`.
15 * @category Collection
16 * @param {Array|Object|string} collection The collection to iterate over.
17 * @param {Array|Function|string} path The path of the method to invoke or
18 * the function invoked per iteration.
19 * @param {...*} [args] The arguments to invoke the method with.
20 * @returns {Array} Returns the array of results.
23 * _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
24 * // => [[1, 5, 7], [1, 2, 3]]
26 * _.invoke([123, 456], String.prototype.split, '');
27 * // => [['1', '2', '3'], ['4', '5', '6']]
29 var invoke = restParam(function(collection, path, args) {
31 isFunc = typeof path == 'function',
33 result = isArrayLike(collection) ? Array(collection.length) : [];
35 baseEach(collection, function(value) {
36 var func = isFunc ? path : ((isProp && value != null) ? value[path] : undefined);
37 result[++index] = func ? func.apply(value, args) : invokePath(value, path, args);
42 module.exports = invoke;