| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|---|
| 4 |
|
|---|
| 5 | const toPath = require('./toPath.js');
|
|---|
| 6 | const toKey = require('../_internal/toKey.js');
|
|---|
| 7 | const last = require('../array/last.js');
|
|---|
| 8 | const get = require('../object/get.js');
|
|---|
| 9 |
|
|---|
| 10 | function 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 | }
|
|---|
| 36 | function 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 |
|
|---|
| 53 | exports.invoke = invoke;
|
|---|