react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / js-yaml / node_modules / argparse / node_modules / lodash / collection / invoke.js
80742 viewsvar baseEach = require('../internal/baseEach'),1invokePath = require('../internal/invokePath'),2isArrayLike = require('../internal/isArrayLike'),3isKey = require('../internal/isKey'),4restParam = require('../function/restParam');56/**7* Invokes the method at `path` of each element in `collection`, returning8* an array of the results of each invoked method. Any additional arguments9* are provided to each invoked method. If `methodName` is a function it is10* invoked for, and `this` bound to, each element in `collection`.11*12* @static13* @memberOf _14* @category Collection15* @param {Array|Object|string} collection The collection to iterate over.16* @param {Array|Function|string} path The path of the method to invoke or17* the function invoked per iteration.18* @param {...*} [args] The arguments to invoke the method with.19* @returns {Array} Returns the array of results.20* @example21*22* _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');23* // => [[1, 5, 7], [1, 2, 3]]24*25* _.invoke([123, 456], String.prototype.split, '');26* // => [['1', '2', '3'], ['4', '5', '6']]27*/28var invoke = restParam(function(collection, path, args) {29var index = -1,30isFunc = typeof path == 'function',31isProp = isKey(path),32result = isArrayLike(collection) ? Array(collection.length) : [];3334baseEach(collection, function(value) {35var func = isFunc ? path : ((isProp && value != null) ? value[path] : null);36result[++index] = func ? func.apply(value, args) : invokePath(value, path, args);37});38return result;39});4041module.exports = invoke;424344