source: node_modules/es-toolkit/dist/compat/object/result.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: 827 bytes
Line 
1import { isKey } from '../_internal/isKey.mjs';
2import { toKey } from '../_internal/toKey.mjs';
3import { toPath } from '../util/toPath.mjs';
4import { toString } from '../util/toString.mjs';
5
6function result(object, path, defaultValue) {
7 if (isKey(path, object)) {
8 path = [path];
9 }
10 else if (!Array.isArray(path)) {
11 path = toPath(toString(path));
12 }
13 const pathLength = Math.max(path.length, 1);
14 for (let index = 0; index < pathLength; index++) {
15 const value = object == null ? undefined : object[toKey(path[index])];
16 if (value === undefined) {
17 return typeof defaultValue === 'function' ? defaultValue.call(object) : defaultValue;
18 }
19 object = typeof value === 'function' ? value.call(object) : value;
20 }
21 return object;
22}
23
24export { result };
Note: See TracBrowser for help on using the repository browser.