source: node_modules/es-toolkit/dist/compat/array/invokeMap.js@ a762898

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

Added visualizations

  • Property mode set to 100644
File size: 1.4 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
5const isFunction = require('../../predicate/isFunction.js');
6const isNil = require('../../predicate/isNil.js');
7const get = require('../object/get.js');
8const isArrayLike = require('../predicate/isArrayLike.js');
9
10function invokeMap(collection, path, ...args) {
11 if (isNil.isNil(collection)) {
12 return [];
13 }
14 const values = isArrayLike.isArrayLike(collection) ? Array.from(collection) : Object.values(collection);
15 const result = [];
16 for (let i = 0; i < values.length; i++) {
17 const value = values[i];
18 if (isFunction.isFunction(path)) {
19 result.push(path.apply(value, args));
20 continue;
21 }
22 const method = get.get(value, path);
23 let thisContext = value;
24 if (Array.isArray(path)) {
25 const pathExceptLast = path.slice(0, -1);
26 if (pathExceptLast.length > 0) {
27 thisContext = get.get(value, pathExceptLast);
28 }
29 }
30 else if (typeof path === 'string' && path.includes('.')) {
31 const parts = path.split('.');
32 const pathExceptLast = parts.slice(0, -1).join('.');
33 thisContext = get.get(value, pathExceptLast);
34 }
35 result.push(method == null ? undefined : method.apply(thisContext, args));
36 }
37 return result;
38}
39
40exports.invokeMap = invokeMap;
Note: See TracBrowser for help on using the repository browser.