From 5a4d79d9765dfca8812638b57d5f5fe21f7a06ee Mon Sep 17 00:00:00 2001 From: Ben Lin <maobin001@msn.com> Date: 星期二, 16 七月 2024 17:05:25 +0800 Subject: [PATCH] 工单下发更新 --- src/utils/index.ts | 61 +++++++++++++++++++++--------- 1 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index 301226b..2b6c68b 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,9 +1,9 @@ -import type { App, Component } from 'vue'; import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router'; +import type { App, Component } from 'vue'; -import { cloneDeep, mergeWith, uniq } from 'lodash-es'; +import { intersectionWith, isEqual, mergeWith, unionWith } from 'lodash-es'; import { unref } from 'vue'; -import { isArray, isObject } from '/@/utils/is'; +import { isArray, isObject } from '@/utils/is'; export const noop = () => {}; @@ -34,24 +34,49 @@ } /** - - 閫掑綊鍚堝苟涓や釜瀵硅薄銆� - Recursively merge two objects. - @param target 鐩爣瀵硅薄锛屽悎骞跺悗缁撴灉瀛樻斁浜庢銆俆he target object to merge into. - @param source 瑕佸悎骞剁殑婧愬璞°�俆he source object to merge from. - @returns 鍚堝苟鍚庣殑瀵硅薄銆俆he merged object. + * Recursively merge two objects. + * 閫掑綊鍚堝苟涓や釜瀵硅薄銆� + * + * @param source The source object to merge from. 瑕佸悎骞剁殑婧愬璞°�� + * @param target The target object to merge into. 鐩爣瀵硅薄锛屽悎骞跺悗缁撴灉瀛樻斁浜庢銆� + * @param mergeArrays How to merge arrays. Default is "replace". + * 濡備綍鍚堝苟鏁扮粍銆傞粯璁や负replace銆� + * - "union": Union the arrays. 瀵规暟缁勬墽琛屽苟闆嗘搷浣溿�� + * - "intersection": Intersect the arrays. 瀵规暟缁勬墽琛屼氦闆嗘搷浣溿�� + * - "concat": Concatenate the arrays. 杩炴帴鏁扮粍銆� + * - "replace": Replace the source array with the target array. 鐢ㄧ洰鏍囨暟缁勬浛鎹㈡簮鏁扮粍銆� + * @returns The merged object. 鍚堝苟鍚庣殑瀵硅薄銆� */ export function deepMerge<T extends object | null | undefined, U extends object | null | undefined>( - target: T, - source: U, + source: T, + target: U, + mergeArrays: 'union' | 'intersection' | 'concat' | 'replace' = 'replace', ): T & U { - return mergeWith(cloneDeep(target), source, (objValue, srcValue) => { - if (isObject(objValue) && isObject(srcValue)) { - return mergeWith(cloneDeep(objValue), srcValue, (prevValue, nextValue) => { - // 濡傛灉鏄暟缁勶紝鍚堝苟鏁扮粍(鍘婚噸) If it is an array, merge the array (remove duplicates) - return isArray(prevValue) ? uniq(prevValue, nextValue) : undefined; - }); + if (!target) { + return source as T & U; + } + if (!source) { + return target as T & U; + } + return mergeWith({}, source, target, (sourceValue, targetValue) => { + if (isArray(targetValue) && isArray(sourceValue)) { + switch (mergeArrays) { + case 'union': + return unionWith(sourceValue, targetValue, isEqual); + case 'intersection': + return intersectionWith(sourceValue, targetValue, isEqual); + case 'concat': + return sourceValue.concat(targetValue); + case 'replace': + return targetValue; + default: + throw new Error(`Unknown merge array strategy: ${mergeArrays as string}`); + } } + if (isObject(targetValue) && isObject(sourceValue)) { + return deepMerge(sourceValue, targetValue, mergeArrays); + } + return undefined; }); } @@ -72,7 +97,7 @@ export function getDynamicProps<T extends Record<string, unknown>, U>(props: T): Partial<U> { const ret: Recordable = {}; - Object.keys(props).map((key) => { + Object.keys(props).forEach((key) => { ret[key] = unref((props as Recordable)[key]); }); -- Gitblit v1.9.3