source: node_modules/es-toolkit/dist/compat/util/invoke.js

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.5 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
5const toPath = require('./toPath.js');
6const toKey = require('../_internal/toKey.js');
7const last = require('../array/last.js');
8const get = require('../object/get.js');
9
10function invoke(object, path, ...args) {
11 args = args.flat(1);
12 if (object == null) {
13 return;
14 }
15 switch (typeof path) {
16 case 'string': {
17 if (typeof object === 'object' && Object.hasOwn(object, path)) {
18 return invokeImpl(object, [path], args);
19 }
20 return invokeImpl(object, toPath.toPath(path), args);
21 }
22 case 'number':
23 case 'symbol': {
24 return invokeImpl(object, [path], args);
25 }
26 default: {
27 if (Array.isArray(path)) {
28 return invokeImpl(object, path, args);
29 }
30 else {
31 return invokeImpl(object, [path], args);
32 }
33 }
34 }
35}
36function invokeImpl(object, path, args) {
37 const parent = get.get(object, path.slice(0, -1), object);
38 if (parent == null) {
39 return undefined;
40 }
41 let lastKey = last.last(path);
42 const lastValue = lastKey?.valueOf();
43 if (typeof lastValue === 'number') {
44 lastKey = toKey.toKey(lastValue);
45 }
46 else {
47 lastKey = String(lastKey);
48 }
49 const func = get.get(parent, lastKey);
50 return func?.apply(parent, args);
51}
52
53exports.invoke = invoke;
Note: See TracBrowser for help on using the repository browser.