my-uniapp-tools 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.js +2 -0
- package/src/utils/index.ts +89 -0
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具函数库
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 深拷贝对象
|
|
7
|
+
* @param target 目标对象,将被拷贝的内容覆盖
|
|
8
|
+
* @param source 源对象,被拷贝的对象
|
|
9
|
+
* @returns 返回深拷贝后的目标对象
|
|
10
|
+
* @description 支持对象、数组、基本数据类型的深度拷贝,处理循环引用问题
|
|
11
|
+
*/
|
|
12
|
+
export function useDeepCopyByObj<T>(target: T, source: any): T {
|
|
13
|
+
// 处理循环引用的WeakMap
|
|
14
|
+
const visited = new WeakMap();
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 内部递归拷贝函数
|
|
18
|
+
* @param targetObj 目标对象
|
|
19
|
+
* @param sourceObj 源对象
|
|
20
|
+
* @returns 拷贝后的对象
|
|
21
|
+
*/
|
|
22
|
+
function copyRecursive(targetObj: any, sourceObj: any): any {
|
|
23
|
+
// 处理null和undefined
|
|
24
|
+
if (sourceObj === null || sourceObj === undefined) {
|
|
25
|
+
return sourceObj;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// 处理基本数据类型
|
|
29
|
+
if (typeof sourceObj !== 'object') {
|
|
30
|
+
return sourceObj;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 处理循环引用
|
|
34
|
+
if (visited.has(sourceObj)) {
|
|
35
|
+
return visited.get(sourceObj);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 处理Date对象
|
|
39
|
+
if (sourceObj instanceof Date) {
|
|
40
|
+
return new Date(sourceObj.getTime());
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// 处理RegExp对象
|
|
44
|
+
if (sourceObj instanceof RegExp) {
|
|
45
|
+
return new RegExp(sourceObj.source, sourceObj.flags);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 处理数组
|
|
49
|
+
if (Array.isArray(sourceObj)) {
|
|
50
|
+
const result: any[] = [];
|
|
51
|
+
visited.set(sourceObj, result);
|
|
52
|
+
|
|
53
|
+
for (let i = 0; i < sourceObj.length; i++) {
|
|
54
|
+
result[i] = copyRecursive(undefined, sourceObj[i]);
|
|
55
|
+
}
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 处理普通对象
|
|
60
|
+
const result: any = {};
|
|
61
|
+
visited.set(sourceObj, result);
|
|
62
|
+
|
|
63
|
+
// 拷贝所有可枚举属性
|
|
64
|
+
for (const key in sourceObj) {
|
|
65
|
+
if (sourceObj.hasOwnProperty(key)) {
|
|
66
|
+
result[key] = copyRecursive(undefined, sourceObj[key]);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// 如果target是对象类型,则将source的属性拷贝到target中
|
|
74
|
+
if (typeof target === 'object' && target !== null && !Array.isArray(target)) {
|
|
75
|
+
const copiedSource = copyRecursive(undefined, source);
|
|
76
|
+
|
|
77
|
+
// 将拷贝后的source属性合并到target中
|
|
78
|
+
for (const key in copiedSource) {
|
|
79
|
+
if (copiedSource.hasOwnProperty(key)) {
|
|
80
|
+
(target as any)[key] = copiedSource[key];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return target;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// 如果target不是对象,则直接返回source的深拷贝
|
|
88
|
+
return copyRecursive(undefined, source) as T;
|
|
89
|
+
}
|