mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 15:57:48 +00:00
Consolidate invoke modules.
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
import apply from './apply.js';
|
||||
import castPath from './castPath.js';
|
||||
import last from '../last.js';
|
||||
import parent from './parent.js';
|
||||
import toKey from './toKey.js';
|
||||
|
||||
/**
|
||||
* The base implementation of `invoke` without support for individual
|
||||
* method arguments.
|
||||
*
|
||||
* @private
|
||||
* @param {Object} object The object to query.
|
||||
* @param {Array|string} path The path of the method to invoke.
|
||||
* @param {Array} args The arguments to invoke the method with.
|
||||
* @returns {*} Returns the result of the invoked method.
|
||||
*/
|
||||
function baseInvoke(object, path, args) {
|
||||
path = castPath(path, object);
|
||||
object = parent(object, path);
|
||||
const func = object == null ? object : object[toKey(last(path))];
|
||||
return func == null ? undefined : apply(func, object, args);
|
||||
}
|
||||
|
||||
export default baseInvoke;
|
||||
17
invoke.js
17
invoke.js
@@ -1,4 +1,8 @@
|
||||
import baseInvoke from './.internal/baseInvoke.js';
|
||||
import apply from './apply.js';
|
||||
import castPath from './castPath.js';
|
||||
import last from '../last.js';
|
||||
import parent from './parent.js';
|
||||
import toKey from './toKey.js';
|
||||
|
||||
/**
|
||||
* Invokes the method at `path` of `object`.
|
||||
@@ -7,17 +11,20 @@ import baseInvoke from './.internal/baseInvoke.js';
|
||||
* @category Object
|
||||
* @param {Object} object The object to query.
|
||||
* @param {Array|string} path The path of the method to invoke.
|
||||
* @param {...*} [args] The arguments to invoke the method with.
|
||||
* @param {Array} [args] The arguments to invoke the method with.
|
||||
* @returns {*} Returns the result of the invoked method.
|
||||
* @example
|
||||
*
|
||||
* const object = { 'a': [{ 'b': { 'c': [1, 2, 3, 4] } }] };
|
||||
*
|
||||
* invoke(object, 'a[0].b.c.slice', 1, 3);
|
||||
* invoke(object, 'a[0].b.c.slice', [1, 3]);
|
||||
* // => [2, 3]
|
||||
*/
|
||||
function invoke(object, path, ...args) {
|
||||
return baseInvoke(object, path, args);
|
||||
function invoke(object, path, args) {
|
||||
path = castPath(path, object);
|
||||
object = parent(object, path);
|
||||
const func = object == null ? object : object[toKey(last(path))];
|
||||
return func == null ? undefined : apply(func, object, args);
|
||||
}
|
||||
|
||||
export default invoke;
|
||||
|
||||
10
invokeMap.js
10
invokeMap.js
@@ -1,6 +1,6 @@
|
||||
import apply from './.internal/apply.js';
|
||||
import baseEach from './.internal/baseEach.js';
|
||||
import baseInvoke from './.internal/baseInvoke.js';
|
||||
import invoke from './invoke.js';
|
||||
import isArrayLike from './isArrayLike.js';
|
||||
|
||||
/**
|
||||
@@ -14,23 +14,23 @@ import isArrayLike from './isArrayLike.js';
|
||||
* @param {Array|Object} collection The collection to iterate over.
|
||||
* @param {Array|Function|string} path The path of the method to invoke or
|
||||
* the function invoked per iteration.
|
||||
* @param {...*} [args] The arguments to invoke each method with.
|
||||
* @param {Array} [args] The arguments to invoke each method with.
|
||||
* @returns {Array} Returns the array of results.
|
||||
* @example
|
||||
*
|
||||
* invokeMap([[5, 1, 7], [3, 2, 1]], 'sort');
|
||||
* // => [[1, 5, 7], [1, 2, 3]]
|
||||
*
|
||||
* invokeMap([123, 456], String.prototype.split, '');
|
||||
* invokeMap([123, 456], String.prototype.split, ['']);
|
||||
* // => [['1', '2', '3'], ['4', '5', '6']]
|
||||
*/
|
||||
function invokeMap(collection, path, ...args) {
|
||||
function invokeMap(collection, path, args) {
|
||||
let index = -1;
|
||||
const isFunc = typeof path == 'function';
|
||||
const result = isArrayLike(collection) ? Array(collection.length) : [];
|
||||
|
||||
baseEach(collection, value => {
|
||||
result[++index] = isFunc ? apply(path, value, args) : baseInvoke(value, path, args);
|
||||
result[++index] = isFunc ? apply(path, value, args) : invoke(value, path, args);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import baseInvoke from './.internal/baseInvoke.js';
|
||||
import invoke from './invoke.js';
|
||||
|
||||
/**
|
||||
* Creates a function that invokes the method at `path` of a given object.
|
||||
@@ -7,7 +7,7 @@ import baseInvoke from './.internal/baseInvoke.js';
|
||||
* @since 3.7.0
|
||||
* @category Util
|
||||
* @param {Array|string} path The path of the method to invoke.
|
||||
* @param {...*} [args] The arguments to invoke the method with.
|
||||
* @param {Array} [args] The arguments to invoke the method with.
|
||||
* @returns {Function} Returns the new invoker function.
|
||||
* @example
|
||||
*
|
||||
@@ -22,8 +22,8 @@ import baseInvoke from './.internal/baseInvoke.js';
|
||||
* map(objects, method(['a', 'b']));
|
||||
* // => [2, 1]
|
||||
*/
|
||||
function method(path, ...args) {
|
||||
return object => baseInvoke(object, path, args);
|
||||
function method(path, args) {
|
||||
return object => invoke(object, path, args);
|
||||
}
|
||||
|
||||
export default method;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import baseInvoke from './.internal/baseInvoke.js';
|
||||
import invoke from './invoke.js';
|
||||
|
||||
/**
|
||||
* The opposite of `method`; this method creates a function that invokes
|
||||
@@ -8,7 +8,7 @@ import baseInvoke from './.internal/baseInvoke.js';
|
||||
* @since 3.7.0
|
||||
* @category Util
|
||||
* @param {Object} object The object to query.
|
||||
* @param {...*} [args] The arguments to invoke the method with.
|
||||
* @param {Array} [args] The arguments to invoke the method with.
|
||||
* @returns {Function} Returns the new invoker function.
|
||||
* @example
|
||||
*
|
||||
@@ -21,8 +21,8 @@ import baseInvoke from './.internal/baseInvoke.js';
|
||||
* map([['a', '2'], ['c', '0']], methodOf(object));
|
||||
* // => [2, 0]
|
||||
*/
|
||||
function methodOf(object, ...args) {
|
||||
return path => baseInvoke(object, path, args);
|
||||
function methodOf(object, args) {
|
||||
return path => invoke(object, path, args);
|
||||
}
|
||||
|
||||
export default methodOf;
|
||||
|
||||
Reference in New Issue
Block a user