| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|---|
| 4 |
|
|---|
| 5 | const get = require('./get.js');
|
|---|
| 6 | const isUnsafeProperty = require('../../_internal/isUnsafeProperty.js');
|
|---|
| 7 | const isDeepKey = require('../_internal/isDeepKey.js');
|
|---|
| 8 | const toKey = require('../_internal/toKey.js');
|
|---|
| 9 | const toPath = require('../util/toPath.js');
|
|---|
| 10 |
|
|---|
| 11 | function 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 | }
|
|---|
| 64 | function 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 |
|
|---|
| 82 | exports.unset = unset;
|
|---|