qmwts 1.1.53 → 1.1.55
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/dist/utils/array-utils.d.ts +23 -0
- package/dist/utils/array-utils.js +78 -2
- package/package.json +1 -1
|
@@ -7,5 +7,28 @@ declare const _default: {
|
|
|
7
7
|
* @param childrenKey 下级字段
|
|
8
8
|
*/
|
|
9
9
|
treeify<T>(array?: any[], idKey?: string, parentKey?: string, childrenKey?: string): T[];
|
|
10
|
+
/**
|
|
11
|
+
* 查询祖先
|
|
12
|
+
* @param array 数组
|
|
13
|
+
* @param id 查询指定id的祖先
|
|
14
|
+
* @param idKey id的key
|
|
15
|
+
* @param parentKey parent的key
|
|
16
|
+
*/
|
|
17
|
+
getAncestors<T>(array: any[] | undefined, id: any, idKey?: string, parentKey?: string): T[];
|
|
18
|
+
/**
|
|
19
|
+
* 查询后代
|
|
20
|
+
* @param array 数组
|
|
21
|
+
* @param id 查询指定id的祖先
|
|
22
|
+
* @param idKey id的key
|
|
23
|
+
* @param parentKey parent的key
|
|
24
|
+
*/
|
|
25
|
+
getDescendants<T>(array: any[] | undefined, id: any, idKey?: string, parentKey?: string): T[];
|
|
26
|
+
/**
|
|
27
|
+
* 根据数组中的某一属性去重
|
|
28
|
+
* @param array
|
|
29
|
+
* @param keyName
|
|
30
|
+
* @param onDuplicate key重复是的行为,override=覆盖,ignore=忽略
|
|
31
|
+
*/
|
|
32
|
+
uniqueBy<T>(array?: any[], keyName?: any, onDuplicate?: "override" | "ignore"): T[];
|
|
10
33
|
};
|
|
11
34
|
export default _default;
|
|
@@ -27,7 +27,7 @@ exports.default = {
|
|
|
27
27
|
var map = new Map(); // 先根据每项的parentId将该项放入Map
|
|
28
28
|
var ids = []; // 记录所有的id,parentId不在这个集合内则说明是最上级
|
|
29
29
|
for (var i = array.length - 1; i >= 0; i--) {
|
|
30
|
-
var
|
|
30
|
+
var e = array[i], id = e[idKey], pid = e[parentKey];
|
|
31
31
|
var children = map.get(pid) || [];
|
|
32
32
|
children.unshift(__assign({}, e));
|
|
33
33
|
map.set(pid, children);
|
|
@@ -36,11 +36,87 @@ exports.default = {
|
|
|
36
36
|
var o = [];
|
|
37
37
|
var values = Array.from(map.values()).flat();
|
|
38
38
|
for (var i = values.length - 1; i >= 0; i--) {
|
|
39
|
-
var
|
|
39
|
+
var e = values[i], id = e[idKey], pid = e[parentKey];
|
|
40
40
|
e[childrenKey] = map.get(id); // 赋值children
|
|
41
41
|
if (!ids.includes(pid)) // 只返回最上级
|
|
42
42
|
o.unshift(e);
|
|
43
43
|
}
|
|
44
44
|
return o;
|
|
45
|
+
},
|
|
46
|
+
/**
|
|
47
|
+
* 查询祖先
|
|
48
|
+
* @param array 数组
|
|
49
|
+
* @param id 查询指定id的祖先
|
|
50
|
+
* @param idKey id的key
|
|
51
|
+
* @param parentKey parent的key
|
|
52
|
+
*/
|
|
53
|
+
getAncestors: function (array, id, idKey, parentKey) {
|
|
54
|
+
if (array === void 0) { array = []; }
|
|
55
|
+
if (idKey === void 0) { idKey = 'id'; }
|
|
56
|
+
if (parentKey === void 0) { parentKey = 'parentId'; }
|
|
57
|
+
// 建立 id - item 的映射
|
|
58
|
+
var idMap = new Map();
|
|
59
|
+
for (var i = array.length - 1; i >= 0; i--) {
|
|
60
|
+
var e = array[i], _a = e, _b = idKey, itemId = _a[_b];
|
|
61
|
+
idMap.set(itemId, e);
|
|
62
|
+
}
|
|
63
|
+
var result = [];
|
|
64
|
+
var current = idMap.get(id);
|
|
65
|
+
while (current) {
|
|
66
|
+
var parentId = current[parentKey];
|
|
67
|
+
var parent_1 = idMap.get(parentId);
|
|
68
|
+
if (parent_1) {
|
|
69
|
+
result.unshift(parent_1);
|
|
70
|
+
current = parent_1;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return result;
|
|
77
|
+
},
|
|
78
|
+
/**
|
|
79
|
+
* 查询后代
|
|
80
|
+
* @param array 数组
|
|
81
|
+
* @param id 查询指定id的祖先
|
|
82
|
+
* @param idKey id的key
|
|
83
|
+
* @param parentKey parent的key
|
|
84
|
+
*/
|
|
85
|
+
getDescendants: function (array, id, idKey, parentKey) {
|
|
86
|
+
if (array === void 0) { array = []; }
|
|
87
|
+
if (idKey === void 0) { idKey = 'id'; }
|
|
88
|
+
if (parentKey === void 0) { parentKey = 'parentId'; }
|
|
89
|
+
var result = [];
|
|
90
|
+
var dfs = function (currentId) {
|
|
91
|
+
for (var i = array.length - 1; i >= 0; i--) {
|
|
92
|
+
var e = array[i], itemId = e[idKey], pid = e[parentKey];
|
|
93
|
+
if (pid === currentId) {
|
|
94
|
+
result.unshift(e);
|
|
95
|
+
dfs(itemId);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
dfs(id);
|
|
100
|
+
return result;
|
|
101
|
+
},
|
|
102
|
+
/**
|
|
103
|
+
* 根据数组中的某一属性去重
|
|
104
|
+
* @param array
|
|
105
|
+
* @param keyName
|
|
106
|
+
* @param onDuplicate key重复是的行为,override=覆盖,ignore=忽略
|
|
107
|
+
*/
|
|
108
|
+
uniqueBy: function (array, keyName, onDuplicate) {
|
|
109
|
+
if (array === void 0) { array = []; }
|
|
110
|
+
if (keyName === void 0) { keyName = 'id'; }
|
|
111
|
+
if (onDuplicate === void 0) { onDuplicate = 'ignore'; }
|
|
112
|
+
var map = new Map();
|
|
113
|
+
for (var i = array.length - 1; i >= 0; i--) {
|
|
114
|
+
var e = array[i];
|
|
115
|
+
var key = e[keyName];
|
|
116
|
+
if (onDuplicate === 'ignore' && map.has(key))
|
|
117
|
+
continue;
|
|
118
|
+
map.set(key, e);
|
|
119
|
+
}
|
|
120
|
+
return Array.from(map.values()).reverse();
|
|
45
121
|
}
|
|
46
122
|
};
|