es-toolkit 1.37.2-dev.1256 → 1.37.2-dev.1257

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Invokes the method at `path` of each element in `collection`, returning
3
+ * an array of the results of each invoked method. Any additional arguments
4
+ * are provided to each invoked method. If `path` is a function, it's invoked
5
+ * for, and `this` bound to, each element in `collection`.
6
+ *
7
+ * @template T The type of the elements in the collection.
8
+ * @template R The type of the resolved values from the invoked methods.
9
+ * @param {T[] | Record<string, T> | null | undefined} collection The collection to iterate over.
10
+ * @param {PropertyKey | PropertyKey[] | ((this: T, ...args: unknown[]) => R)} path The path of the method to invoke (string, number, symbol, or an array of these) or the function to invoke.
11
+ * @param {...unknown} [args] The arguments to invoke each method with.
12
+ * @returns {Array<R | undefined>} Returns the array of results. Elements are `undefined` if the path is not found or the method invocation results in `undefined`.
13
+ *
14
+ * @example
15
+ * // Invoke a method on each element
16
+ * invokeMap(['a', 'b', 'c'], 'toUpperCase');
17
+ * // => ['A', 'B', 'C']
18
+ *
19
+ * // Invoke a method with arguments
20
+ * invokeMap([[5, 1, 7], [3, 2, 1]], 'sort');
21
+ * // => [[1, 5, 7], [1, 2, 3]]
22
+ *
23
+ * // Invoke a method on each value in an object
24
+ * invokeMap({ a: 1, b: 2, c: 3 }, 'toFixed', 1);
25
+ * // => ['1.0', '2.0', '3.0']
26
+ *
27
+ * // Use a function instead of a method name
28
+ * invokeMap(
29
+ * ['a', 'b', 'c'],
30
+ * function(this: string, prefix: string, suffix: string) {
31
+ * return prefix + this.toUpperCase() + suffix;
32
+ * },
33
+ * '(',
34
+ * ')'
35
+ * );
36
+ * // => ['(A)', '(B)', '(C)']
37
+ *
38
+ * invokeMap([123, 456], String.prototype.split, '');
39
+ * // => [['1', '2', '3'], ['4', '5', '6']]
40
+ */
41
+ declare function invokeMap<T, R>(collection: T[] | Record<string, T> | null | undefined, path: PropertyKey | PropertyKey[] | ((this: T, ...args: any[]) => R), ...args: unknown[]): Array<R | undefined>;
42
+
43
+ export { invokeMap };
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Invokes the method at `path` of each element in `collection`, returning
3
+ * an array of the results of each invoked method. Any additional arguments
4
+ * are provided to each invoked method. If `path` is a function, it's invoked
5
+ * for, and `this` bound to, each element in `collection`.
6
+ *
7
+ * @template T The type of the elements in the collection.
8
+ * @template R The type of the resolved values from the invoked methods.
9
+ * @param {T[] | Record<string, T> | null | undefined} collection The collection to iterate over.
10
+ * @param {PropertyKey | PropertyKey[] | ((this: T, ...args: unknown[]) => R)} path The path of the method to invoke (string, number, symbol, or an array of these) or the function to invoke.
11
+ * @param {...unknown} [args] The arguments to invoke each method with.
12
+ * @returns {Array<R | undefined>} Returns the array of results. Elements are `undefined` if the path is not found or the method invocation results in `undefined`.
13
+ *
14
+ * @example
15
+ * // Invoke a method on each element
16
+ * invokeMap(['a', 'b', 'c'], 'toUpperCase');
17
+ * // => ['A', 'B', 'C']
18
+ *
19
+ * // Invoke a method with arguments
20
+ * invokeMap([[5, 1, 7], [3, 2, 1]], 'sort');
21
+ * // => [[1, 5, 7], [1, 2, 3]]
22
+ *
23
+ * // Invoke a method on each value in an object
24
+ * invokeMap({ a: 1, b: 2, c: 3 }, 'toFixed', 1);
25
+ * // => ['1.0', '2.0', '3.0']
26
+ *
27
+ * // Use a function instead of a method name
28
+ * invokeMap(
29
+ * ['a', 'b', 'c'],
30
+ * function(this: string, prefix: string, suffix: string) {
31
+ * return prefix + this.toUpperCase() + suffix;
32
+ * },
33
+ * '(',
34
+ * ')'
35
+ * );
36
+ * // => ['(A)', '(B)', '(C)']
37
+ *
38
+ * invokeMap([123, 456], String.prototype.split, '');
39
+ * // => [['1', '2', '3'], ['4', '5', '6']]
40
+ */
41
+ declare function invokeMap<T, R>(collection: T[] | Record<string, T> | null | undefined, path: PropertyKey | PropertyKey[] | ((this: T, ...args: any[]) => R), ...args: unknown[]): Array<R | undefined>;
42
+
43
+ export { invokeMap };
@@ -0,0 +1,36 @@
1
+ import { isFunction } from '../../predicate/isFunction.mjs';
2
+ import { isNil } from '../../predicate/isNil.mjs';
3
+ import { get } from '../object/get.mjs';
4
+ import { isArrayLike } from '../predicate/isArrayLike.mjs';
5
+
6
+ function invokeMap(collection, path, ...args) {
7
+ if (isNil(collection)) {
8
+ return [];
9
+ }
10
+ const values = isArrayLike(collection) ? Array.from(collection) : Object.values(collection);
11
+ const result = [];
12
+ for (let i = 0; i < values.length; i++) {
13
+ const value = values[i];
14
+ if (isFunction(path)) {
15
+ result.push(path.apply(value, args));
16
+ continue;
17
+ }
18
+ const method = get(value, path);
19
+ let thisContext = value;
20
+ if (Array.isArray(path)) {
21
+ const pathExceptLast = path.slice(0, -1);
22
+ if (pathExceptLast.length > 0) {
23
+ thisContext = get(value, pathExceptLast);
24
+ }
25
+ }
26
+ else if (typeof path === 'string' && path.includes('.')) {
27
+ const parts = path.split('.');
28
+ const pathExceptLast = parts.slice(0, -1).join('.');
29
+ thisContext = get(value, pathExceptLast);
30
+ }
31
+ result.push(method == null ? undefined : method.apply(thisContext, args));
32
+ }
33
+ return result;
34
+ }
35
+
36
+ export { invokeMap };
@@ -30,6 +30,7 @@ export { initial } from './array/initial.mjs';
30
30
  export { intersection } from './array/intersection.mjs';
31
31
  export { intersectionBy } from './array/intersectionBy.mjs';
32
32
  export { intersectionWith } from './array/intersectionWith.mjs';
33
+ export { invokeMap } from './array/invokeMap.mjs';
33
34
  export { join } from './array/join.mjs';
34
35
  export { keyBy } from './array/keyBy.mjs';
35
36
  export { last } from './array/last.mjs';
@@ -30,6 +30,7 @@ export { initial } from './array/initial.js';
30
30
  export { intersection } from './array/intersection.js';
31
31
  export { intersectionBy } from './array/intersectionBy.js';
32
32
  export { intersectionWith } from './array/intersectionWith.js';
33
+ export { invokeMap } from './array/invokeMap.js';
33
34
  export { join } from './array/join.js';
34
35
  export { keyBy } from './array/keyBy.js';
35
36
  export { last } from './array/last.js';
@@ -30,6 +30,7 @@ export { initial } from './array/initial.mjs';
30
30
  export { intersection } from './array/intersection.mjs';
31
31
  export { intersectionBy } from './array/intersectionBy.mjs';
32
32
  export { intersectionWith } from './array/intersectionWith.mjs';
33
+ export { invokeMap } from './array/invokeMap.mjs';
33
34
  export { join } from './array/join.mjs';
34
35
  export { keyBy } from './array/keyBy.mjs';
35
36
  export { last } from './array/last.mjs';
@@ -30,6 +30,7 @@ export { initial } from './array/initial.mjs';
30
30
  export { intersection } from './array/intersection.mjs';
31
31
  export { intersectionBy } from './array/intersectionBy.mjs';
32
32
  export { intersectionWith } from './array/intersectionWith.mjs';
33
+ export { invokeMap } from './array/invokeMap.mjs';
33
34
  export { join } from './array/join.mjs';
34
35
  export { keyBy } from './array/keyBy.mjs';
35
36
  export { last } from './array/last.mjs';
@@ -30,6 +30,7 @@ export { initial } from './array/initial.js';
30
30
  export { intersection } from './array/intersection.js';
31
31
  export { intersectionBy } from './array/intersectionBy.js';
32
32
  export { intersectionWith } from './array/intersectionWith.js';
33
+ export { invokeMap } from './array/invokeMap.js';
33
34
  export { join } from './array/join.js';
34
35
  export { keyBy } from './array/keyBy.js';
35
36
  export { last } from './array/last.js';
@@ -1056,6 +1056,36 @@ function uniqPreserve0(arr) {
1056
1056
  return result;
1057
1057
  }
1058
1058
 
1059
+ function invokeMap(collection, path, ...args) {
1060
+ if (isWeakSet$1.isNil(collection)) {
1061
+ return [];
1062
+ }
1063
+ const values = isArrayLike(collection) ? Array.from(collection) : Object.values(collection);
1064
+ const result = [];
1065
+ for (let i = 0; i < values.length; i++) {
1066
+ const value = values[i];
1067
+ if (isWeakSet$1.isFunction(path)) {
1068
+ result.push(path.apply(value, args));
1069
+ continue;
1070
+ }
1071
+ const method = get(value, path);
1072
+ let thisContext = value;
1073
+ if (Array.isArray(path)) {
1074
+ const pathExceptLast = path.slice(0, -1);
1075
+ if (pathExceptLast.length > 0) {
1076
+ thisContext = get(value, pathExceptLast);
1077
+ }
1078
+ }
1079
+ else if (typeof path === 'string' && path.includes('.')) {
1080
+ const parts = path.split('.');
1081
+ const pathExceptLast = parts.slice(0, -1).join('.');
1082
+ thisContext = get(value, pathExceptLast);
1083
+ }
1084
+ result.push(method == null ? undefined : method.apply(thisContext, args));
1085
+ }
1086
+ return result;
1087
+ }
1088
+
1059
1089
  function join(array, separator = ',') {
1060
1090
  if (!isArrayLike(array)) {
1061
1091
  return '';
@@ -4251,6 +4281,7 @@ const compat = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.defineProperty({
4251
4281
  invert: isPlainObject.invert,
4252
4282
  invertBy,
4253
4283
  invoke,
4284
+ invokeMap,
4254
4285
  isArguments,
4255
4286
  isArray: isPlainObject.isArray,
4256
4287
  isArrayBuffer,
@@ -4561,6 +4592,7 @@ exports.intersectionBy = intersectionBy;
4561
4592
  exports.intersectionWith = intersectionWith;
4562
4593
  exports.invertBy = invertBy;
4563
4594
  exports.invoke = invoke;
4595
+ exports.invokeMap = invokeMap;
4564
4596
  exports.isArguments = isArguments;
4565
4597
  exports.isArrayBuffer = isArrayBuffer;
4566
4598
  exports.isArrayLike = isArrayLike;
@@ -30,6 +30,7 @@ export { initial } from './array/initial.mjs';
30
30
  export { intersection } from './array/intersection.mjs';
31
31
  export { intersectionBy } from './array/intersectionBy.mjs';
32
32
  export { intersectionWith } from './array/intersectionWith.mjs';
33
+ export { invokeMap } from './array/invokeMap.mjs';
33
34
  export { join } from './array/join.mjs';
34
35
  export { keyBy } from './array/keyBy.mjs';
35
36
  export { last } from './array/last.mjs';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "es-toolkit",
3
3
  "description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
4
- "version": "1.37.2-dev.1256+5f76be6a",
4
+ "version": "1.37.2-dev.1257+d4eeda38",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {