add deep merge
This commit is contained in:
3
packages/core/src/utilities/isObject.ts
Normal file
3
packages/core/src/utilities/isObject.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export default function isObject(item: any): boolean {
|
||||
return (item && typeof item === 'object' && !Array.isArray(item))
|
||||
}
|
||||
22
packages/core/src/utilities/mergeDeep.ts
Normal file
22
packages/core/src/utilities/mergeDeep.ts
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user