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

22 lines
611 B
TypeScript
Raw Normal View History

2021-01-30 05:51:03 +08:00
import isPlainObject from './isPlainObject'
2021-01-20 16:18:49 +08:00
2021-04-21 15:43:31 +08:00
export default function mergeDeep(target: Record<string, any>, source: Record<string, any>): Record<string, any> {
2021-01-20 16:18:49 +08:00
const output = { ...target }
2021-01-30 05:51:03 +08:00
if (isPlainObject(target) && isPlainObject(source)) {
2021-01-20 16:18:49 +08:00
Object.keys(source).forEach(key => {
2021-01-30 05:51:03 +08:00
if (isPlainObject(source[key])) {
2021-01-20 16:18:49 +08:00
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
}