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