add deep merge

This commit is contained in:
Philipp Kühn
2021-01-20 09:18:49 +01:00
parent b1d3b4ce8d
commit 1c424f4db1
6 changed files with 100 additions and 12 deletions

View File

@@ -0,0 +1,3 @@
export default function isObject(item: any): boolean {
return (item && typeof item === 'object' && !Array.isArray(item))
}

View File

@@ -0,0 +1,22 @@
import { AnyObject } from '../types'
import isObject from './isObject'
export default function mergeDeep(target: AnyObject, source: AnyObject) {
const output = { ...target }
if (isObject(target) && isObject(source)) {
Object.keys(source).forEach(key => {
if (isObject(source[key])) {
if (!(key in target)) {
Object.assign(output, { [key]: source[key] })
} else {
output[key] = mergeDeep(target[key], source[key])
}
} else {
Object.assign(output, { [key]: source[key] })
}
})
}
return output
}