main
| 1 | import { isObject } from "./is-object" |
| 2 | |
| 3 | export function deepMerge(target: any, source: any) { |
| 4 | const recursive = (a, b) => { |
| 5 | if (!isObject(a)) { |
| 6 | return b |
| 7 | } |
| 8 | if (!isObject(b)) { |
| 9 | return a |
| 10 | } |
| 11 | |
| 12 | Object.keys(b).forEach((key) => { |
| 13 | if (isObject(b[key])) { |
| 14 | a[key] ??= {} |
| 15 | a[key] = deepMerge(a[key], b[key]) |
| 16 | } else { |
| 17 | a[key] = b[key] |
| 18 | } |
| 19 | }) |
| 20 | |
| 21 | return a |
| 22 | } |
| 23 | |
| 24 | const copy = { ...target } |
| 25 | return recursive(copy, source) |
| 26 | } |