| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|---|
| 4 |
|
|---|
| 5 | const isFunction = require('../../predicate/isFunction.js');
|
|---|
| 6 | const isNil = require('../../predicate/isNil.js');
|
|---|
| 7 | const get = require('../object/get.js');
|
|---|
| 8 | const isArrayLike = require('../predicate/isArrayLike.js');
|
|---|
| 9 |
|
|---|
| 10 | function 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 |
|
|---|
| 40 | exports.invokeMap = invokeMap;
|
|---|