-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Copy pathbaseInvoke.js
31 lines (27 loc) · 1.11 KB
/
baseInvoke.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
define(['./baseEach', './isLength'], function(baseEach, isLength) {
/** Used as a safe reference for `undefined` in pre-ES5 environments. */
var undefined;
/**
* The base implementation of `_.invoke` which requires additional arguments
* to be provided as an array of arguments rather than individually.
*
* @private
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function|string} methodName The name of the method to invoke or
* the function invoked per iteration.
* @param {Array} [args] The arguments to invoke the method with.
* @returns {Array} Returns the array of results.
*/
function baseInvoke(collection, methodName, args) {
var index = -1,
isFunc = typeof methodName == 'function',
length = collection ? collection.length : 0,
result = isLength(length) ? Array(length) : [];
baseEach(collection, function(value) {
var func = isFunc ? methodName : (value != null && value[methodName]);
result[++index] = func ? func.apply(value, args) : undefined;
});
return result;
}
return baseInvoke;
});