(object: any, path: string | (string | number)[])
| 26 | } |
| 27 | |
| 28 | export default function unset(object: any, path: string | (string | number)[]) { |
| 29 | if (isString(path) && Object.prototype.hasOwnProperty.call(object, path)) { |
| 30 | delete object[path]; |
| 31 | return object; |
| 32 | } |
| 33 | |
| 34 | const paths = Array.isArray(path) |
| 35 | ? path |
| 36 | : isKey(path) |
| 37 | ? [path] |
| 38 | : stringToPath(path); |
| 39 | |
| 40 | const childObject = paths.length === 1 ? object : baseGet(object, paths); |
| 41 | |
| 42 | const index = paths.length - 1; |
| 43 | const key = paths[index]; |
| 44 | |
| 45 | if (childObject) { |
| 46 | delete childObject[key]; |
| 47 | } |
| 48 | |
| 49 | if ( |
| 50 | index !== 0 && |
| 51 | ((isObject(childObject) && isEmptyObject(childObject)) || |
| 52 | (Array.isArray(childObject) && isEmptyArray(childObject))) |
| 53 | ) { |
| 54 | unset(object, paths.slice(0, -1)); |
| 55 | } |
| 56 | |
| 57 | return object; |
| 58 | } |
no test coverage detected