source: node_modules/es-toolkit/dist/compat/object/get.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: 2.3 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
5const isUnsafeProperty = require('../../_internal/isUnsafeProperty.js');
6const isDeepKey = require('../_internal/isDeepKey.js');
7const toKey = require('../_internal/toKey.js');
8const toPath = require('../util/toPath.js');
9
10function get(object, path, defaultValue) {
11 if (object == null) {
12 return defaultValue;
13 }
14 switch (typeof path) {
15 case 'string': {
16 if (isUnsafeProperty.isUnsafeProperty(path)) {
17 return defaultValue;
18 }
19 const result = object[path];
20 if (result === undefined) {
21 if (isDeepKey.isDeepKey(path)) {
22 return get(object, toPath.toPath(path), defaultValue);
23 }
24 else {
25 return defaultValue;
26 }
27 }
28 return result;
29 }
30 case 'number':
31 case 'symbol': {
32 if (typeof path === 'number') {
33 path = toKey.toKey(path);
34 }
35 const result = object[path];
36 if (result === undefined) {
37 return defaultValue;
38 }
39 return result;
40 }
41 default: {
42 if (Array.isArray(path)) {
43 return getWithPath(object, path, defaultValue);
44 }
45 if (Object.is(path?.valueOf(), -0)) {
46 path = '-0';
47 }
48 else {
49 path = String(path);
50 }
51 if (isUnsafeProperty.isUnsafeProperty(path)) {
52 return defaultValue;
53 }
54 const result = object[path];
55 if (result === undefined) {
56 return defaultValue;
57 }
58 return result;
59 }
60 }
61}
62function getWithPath(object, path, defaultValue) {
63 if (path.length === 0) {
64 return defaultValue;
65 }
66 let current = object;
67 for (let index = 0; index < path.length; index++) {
68 if (current == null) {
69 return defaultValue;
70 }
71 if (isUnsafeProperty.isUnsafeProperty(path[index])) {
72 return defaultValue;
73 }
74 current = current[path[index]];
75 }
76 if (current === undefined) {
77 return defaultValue;
78 }
79 return current;
80}
81
82exports.get = get;
Note: See TracBrowser for help on using the repository browser.