tiptap/packages/core/src/utilities/mergeDeep.ts

23 lines
593 B
TypeScript
Raw Normal View History

2021-01-20 16:18:49 +08:00
import { AnyObject } from '../types'
import isObject from './isObject'
2021-01-28 16:50:17 +08:00
export default function mergeDeep(target: AnyObject, source: AnyObject): AnyObject {
2021-01-20 16:18:49 +08:00
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
}