Skip to content

Commit

Permalink
fix(utils/mergeSafe): do not reassign
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Dec 12, 2023
1 parent d77e1f2 commit 6d53419
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/common/utils/mergeSafe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,29 @@ const mergeSafeRecursive = <Target>(target: Target, source: unknown): Target =>
const result: PlainObject = {}
const targetPropertyNames = Object.getOwnPropertyNames(target)
const targetPropertySymbols = Object.getOwnPropertySymbols(target)
const sourcePropertyNames = Object.getOwnPropertyNames(source)
const sourcePropertySymbols = Object.getOwnPropertySymbols(source)
const assignTargetProperty = (key: string | symbol): void => {
Object.defineProperty(result, key, Object.getOwnPropertyDescriptor(target, key)!)
const isKeySymbol = typeof key === 'symbol'
if (
(!isKeySymbol && !sourcePropertyNames.includes(key)) ||
(isKeySymbol && !sourcePropertySymbols.includes(key))
) {
Object.defineProperty(result, key, Object.getOwnPropertyDescriptor(target, key)!)
}
}
targetPropertyNames.forEach(assignTargetProperty)
targetPropertySymbols.forEach(assignTargetProperty)
const sourcePropertyNames = Object.getOwnPropertyNames(source)
const sourcePropertySymbols = Object.getOwnPropertySymbols(source)
const assignSourceProperty = (key: string | symbol): void => {
const isKeySymbol = typeof key === 'symbol'
if (
(!isKeySymbol && targetPropertyNames.includes(key)) ||
(isKeySymbol && targetPropertySymbols.includes(key))
) {
const targetPropertyValue = result[key]
const targetPropertyValue = target[key]
const sourcePropertyValue: unknown = source[key]
Object.defineProperty(result, key, {
...Object.getOwnPropertyDescriptor(source, key),
...Object.getOwnPropertyDescriptor(target, key),
value: mergeSafeRecursive(targetPropertyValue, sourcePropertyValue),
})
}
Expand Down

0 comments on commit 6d53419

Please sign in to comment.