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