refactor: deepMerge (#2649)

This commit is contained in:
Kirk Lin 2023-03-28 21:34:11 +08:00 committed by GitHub
parent 5d8d9786e3
commit 4c63b1abb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 10 deletions

View File

@ -2,8 +2,8 @@ import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router'
import type { App, Component } from 'vue';
import { unref } from 'vue';
import { isObject } from '/@/utils/is';
import { cloneDeep } from 'lodash-es';
import { isArray, isObject } from '/@/utils/is';
import { cloneDeep, mergeWith } from 'lodash-es';
export const noop = () => {};
@ -33,14 +33,25 @@ export function setObjToUrlParams(baseUrl: string, obj: any): string {
return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters;
}
// 深度合并
export function deepMerge<T = any>(src: any = {}, target: any = {}): T {
let key: string;
const res: any = cloneDeep(src);
for (key in target) {
res[key] = isObject(res[key]) ? deepMerge(res[key], target[key]) : target[key];
}
return res;
/**
Recursively merge two objects.
@param target The target object to merge into.
@param source The source object to merge from.
@returns The merged object.
*/
export function deepMerge<T extends object | null | undefined, U extends object | null | undefined>(
target: T,
source: U,
): T & U {
return mergeWith(cloneDeep(target), source, (objValue, srcValue) => {
if (isObject(objValue) && isObject(srcValue)) {
return mergeWith(cloneDeep(objValue), srcValue, (prevValue, nextValue) => {
return isArray(prevValue) ? prevValue.concat(nextValue) : undefined;
});
}
});
}
export function openWindow(