Skip to content

Commit 563059c

Browse files
committed
Consolidate invoke modules.
1 parent 168322f commit 563059c

File tree

5 files changed

+25
-42
lines changed

5 files changed

+25
-42
lines changed

.internal/baseInvoke.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

invoke.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import baseInvoke from './.internal/baseInvoke.js';
1+
import apply from './apply.js';
2+
import castPath from './castPath.js';
3+
import last from '../last.js';
4+
import parent from './parent.js';
5+
import toKey from './toKey.js';
26

37
/**
48
* Invokes the method at `path` of `object`.
@@ -7,17 +11,20 @@ import baseInvoke from './.internal/baseInvoke.js';
711
* @category Object
812
* @param {Object} object The object to query.
913
* @param {Array|string} path The path of the method to invoke.
10-
* @param {...*} [args] The arguments to invoke the method with.
14+
* @param {Array} [args] The arguments to invoke the method with.
1115
* @returns {*} Returns the result of the invoked method.
1216
* @example
1317
*
1418
* const object = { 'a': [{ 'b': { 'c': [1, 2, 3, 4] } }] };
1519
*
16-
* invoke(object, 'a[0].b.c.slice', 1, 3);
20+
* invoke(object, 'a[0].b.c.slice', [1, 3]);
1721
* // => [2, 3]
1822
*/
19-
function invoke(object, path, ...args) {
20-
return baseInvoke(object, path, args);
23+
function invoke(object, path, args) {
24+
path = castPath(path, object);
25+
object = parent(object, path);
26+
const func = object == null ? object : object[toKey(last(path))];
27+
return func == null ? undefined : apply(func, object, args);
2128
}
2229

2330
export default invoke;

invokeMap.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import apply from './.internal/apply.js';
22
import baseEach from './.internal/baseEach.js';
3-
import baseInvoke from './.internal/baseInvoke.js';
3+
import invoke from './invoke.js';
44
import isArrayLike from './isArrayLike.js';
55

66
/**
@@ -14,23 +14,23 @@ import isArrayLike from './isArrayLike.js';
1414
* @param {Array|Object} collection The collection to iterate over.
1515
* @param {Array|Function|string} path The path of the method to invoke or
1616
* the function invoked per iteration.
17-
* @param {...*} [args] The arguments to invoke each method with.
17+
* @param {Array} [args] The arguments to invoke each method with.
1818
* @returns {Array} Returns the array of results.
1919
* @example
2020
*
2121
* invokeMap([[5, 1, 7], [3, 2, 1]], 'sort');
2222
* // => [[1, 5, 7], [1, 2, 3]]
2323
*
24-
* invokeMap([123, 456], String.prototype.split, '');
24+
* invokeMap([123, 456], String.prototype.split, ['']);
2525
* // => [['1', '2', '3'], ['4', '5', '6']]
2626
*/
27-
function invokeMap(collection, path, ...args) {
27+
function invokeMap(collection, path, args) {
2828
let index = -1;
2929
const isFunc = typeof path == 'function';
3030
const result = isArrayLike(collection) ? Array(collection.length) : [];
3131

3232
baseEach(collection, value => {
33-
result[++index] = isFunc ? apply(path, value, args) : baseInvoke(value, path, args);
33+
result[++index] = isFunc ? apply(path, value, args) : invoke(value, path, args);
3434
});
3535
return result;
3636
}

method.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import baseInvoke from './.internal/baseInvoke.js';
1+
import invoke from './invoke.js';
22

33
/**
44
* Creates a function that invokes the method at `path` of a given object.
@@ -7,7 +7,7 @@ import baseInvoke from './.internal/baseInvoke.js';
77
* @since 3.7.0
88
* @category Util
99
* @param {Array|string} path The path of the method to invoke.
10-
* @param {...*} [args] The arguments to invoke the method with.
10+
* @param {Array} [args] The arguments to invoke the method with.
1111
* @returns {Function} Returns the new invoker function.
1212
* @example
1313
*
@@ -22,8 +22,8 @@ import baseInvoke from './.internal/baseInvoke.js';
2222
* map(objects, method(['a', 'b']));
2323
* // => [2, 1]
2424
*/
25-
function method(path, ...args) {
26-
return object => baseInvoke(object, path, args);
25+
function method(path, args) {
26+
return object => invoke(object, path, args);
2727
}
2828

2929
export default method;

methodOf.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import baseInvoke from './.internal/baseInvoke.js';
1+
import invoke from './invoke.js';
22

33
/**
44
* The opposite of `method`; this method creates a function that invokes
@@ -8,7 +8,7 @@ import baseInvoke from './.internal/baseInvoke.js';
88
* @since 3.7.0
99
* @category Util
1010
* @param {Object} object The object to query.
11-
* @param {...*} [args] The arguments to invoke the method with.
11+
* @param {Array} [args] The arguments to invoke the method with.
1212
* @returns {Function} Returns the new invoker function.
1313
* @example
1414
*
@@ -21,8 +21,8 @@ import baseInvoke from './.internal/baseInvoke.js';
2121
* map([['a', '2'], ['c', '0']], methodOf(object));
2222
* // => [2, 0]
2323
*/
24-
function methodOf(object, ...args) {
25-
return path => baseInvoke(object, path, args);
24+
function methodOf(object, args) {
25+
return path => invoke(object, path, args);
2626
}
2727

2828
export default methodOf;

0 commit comments

Comments
 (0)