|
1
|
import { isObject } from "./is-object" |
|
2
|
|
|
3
|
export function omitDeep<T extends object = object>( |
|
4
|
input: object, |
|
5
|
excludes: Array<number | string> |
|
6
|
): T { |
|
7
|
if (!input) { |
|
8
|
return input |
|
9
|
} |
|
10
|
|
|
11
|
return Object.entries(input).reduce((nextInput, [key, value]) => { |
|
12
|
const shouldExclude = excludes.includes(key) |
|
13
|
if (shouldExclude) { |
|
14
|
return nextInput |
|
15
|
} |
|
16
|
|
|
17
|
if (Array.isArray(value)) { |
|
18
|
nextInput[key] = value.map((arrItem) => { |
|
19
|
if (isObject(arrItem)) { |
|
20
|
return omitDeep(arrItem, excludes) |
|
21
|
} |
|
22
|
return arrItem |
|
23
|
}) |
|
24
|
return nextInput |
|
25
|
} else if (isObject(value)) { |
|
26
|
nextInput[key] = omitDeep(value, excludes) |
|
27
|
return nextInput |
|
28
|
} |
|
29
|
|
|
30
|
nextInput[key] = value |
|
31
|
|
|
32
|
return nextInput |
|
33
|
}, {} as T) |
|
34
|
} |