Browse Source

fix: deepMerge

main
xingyu 2 years ago
parent
commit
e960ac6419
  1. 22
      src/utils/index.ts

22
src/utils/index.ts

@ -58,26 +58,26 @@ export function deepMerge<T extends object | null | undefined, U extends object
if (!source) { if (!source) {
return target as T & U return target as T & U
} }
if (isArray(target) && isArray(source)) { return mergeWith({}, source, target, (sourceValue, targetValue) => {
if (isArray(targetValue) && isArray(sourceValue)) {
switch (mergeArrays) { switch (mergeArrays) {
case 'union': case 'union':
return unionWith(target, source, isEqual) as T & U return unionWith(sourceValue, targetValue, isEqual)
case 'intersection': case 'intersection':
return intersectionWith(target, source, isEqual) as T & U return intersectionWith(sourceValue, targetValue, isEqual)
case 'concat': case 'concat':
return target.concat(source) as T & U return sourceValue.concat(targetValue)
case 'replace': case 'replace':
return source as T & U return targetValue
default: default:
throw new Error(`Unknown merge array strategy: ${mergeArrays}`) throw new Error(`Unknown merge array strategy: ${mergeArrays as string}`)
} }
} }
if (isObject(target) && isObject(source)) { if (isObject(targetValue) && isObject(sourceValue)) {
return mergeWith({}, target, source, (targetValue, sourceValue) => { return deepMerge(sourceValue, targetValue, mergeArrays)
return deepMerge(targetValue, sourceValue, mergeArrays)
}) as T & U
} }
return source as T & U return undefined
})
} }
export function openWindow(url: string, opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean }) { export function openWindow(url: string, opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean }) {