From f4bdfb94843c5b0e92527e82de10f01131d7e28b Mon Sep 17 00:00:00 2001 From: fangyunong Date: Sun, 13 Jul 2025 11:10:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=B0=81=E8=A3=85=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/index.ts | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index deb37af..5489560 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,4 +1,28 @@ -// 深拷贝 -export function deepClone(){ +// 对象深拷贝 obj 对象 +export function deepClone(obj: T): T { + if (obj === null || typeof obj !== "object") return obj; + if (obj instanceof Date) return new Date(obj.getTime()) as unknown as T; + if (obj instanceof RegExp) return new RegExp(obj) as unknown as T; + const clone: any = Array.isArray(obj) ? [] : {}; + for (const key in obj) { + if (Object.hasOwnProperty.call(obj, key)) { + clone[key] = deepClone(obj[key]); + } + } + return clone; +} -} \ No newline at end of file +// 字典value转label +interface DictItem { + value: T; + label: string; +} +export function dictTransform>( + value: T, + dict: K[] +): string | T { + if (!dict || !Array.isArray(dict)) throw new Error("字典必须为一个数组对象"); + if (value === undefined || value === null) throw new Error("请传入字段值"); + const foundItem = dict.find((item) => item.value === value); + return foundItem ? foundItem.label : dict[0]?.label || ""; +}