source: node_modules/es-toolkit/dist/compat/array/invokeMap.mjs

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 1.3 KB
Line 
1import { isFunction } from '../../predicate/isFunction.mjs';
2import { isNil } from '../../predicate/isNil.mjs';
3import { get } from '../object/get.mjs';
4import { isArrayLike } from '../predicate/isArrayLike.mjs';
5
6function 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
36export { invokeMap };
Note: See TracBrowser for help on using the repository browser.