dlsjs 0.1.23 → 0.1.24
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/dlsjs.cjs.js +7 -7
- package/dist/dlsjs.cjs.js.map +1 -1
- package/dist/dlsjs.esm.js +34 -15
- package/dist/dlsjs.esm.js.map +1 -1
- package/dist/dlsjs.umd.js +7 -7
- package/dist/dlsjs.umd.js.map +1 -1
- package/package.json +1 -1
- package/src/core/ES/Tree.js +112 -30
package/package.json
CHANGED
package/src/core/ES/Tree.js
CHANGED
|
@@ -6,26 +6,65 @@
|
|
|
6
6
|
import {isType} from "./Judgment";
|
|
7
7
|
import {clone, uniqBy} from "ramda";
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* 列表转树(高性能版,适配100W+数据)
|
|
11
|
+
* @param {Array} list - 原始列表数据
|
|
12
|
+
* @param {Object} extendProps - 自定义属性映射 {id: 'id', children: 'children', parentId: 'parentId'}
|
|
13
|
+
* @returns {Array} 树形结构数据
|
|
14
|
+
*/
|
|
9
15
|
export function list2Tree (list, extendProps = {}) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
// 1. 配置默认属性映射,兼容自定义字段
|
|
17
|
+
const props = {
|
|
18
|
+
id: 'id',
|
|
19
|
+
children: 'children',
|
|
20
|
+
parentId: 'parentId',
|
|
21
|
+
useConcat: false, // 保留原有配置,实际优化后无需concat
|
|
22
|
+
...extendProps
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const { id: idKey, children: childrenKey, parentId: parentIdKey, useConcat } = props;
|
|
26
|
+
|
|
27
|
+
// 2. 构建ID到节点的哈希映射(O(n)),同时初始化children数组
|
|
28
|
+
const nodeMap = new Map();
|
|
29
|
+
const rootNodes = []; // 存储根节点(parentId为空/0/不存在的节点)
|
|
30
|
+
|
|
31
|
+
// 第一次遍历:初始化映射和children,O(n)
|
|
32
|
+
for (const node of list) {
|
|
33
|
+
const nodeId = node[idKey];
|
|
34
|
+
// 初始化children数组(避免后续判断undefined)
|
|
35
|
+
if (!node[childrenKey]) {
|
|
36
|
+
node[childrenKey] = [];
|
|
19
37
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
38
|
+
// 将节点存入Map,方便后续快速查找父节点
|
|
39
|
+
nodeMap.set(nodeId, node);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 第二次遍历:关联父子节点,O(n)
|
|
43
|
+
for (const node of list) {
|
|
44
|
+
const parentId = node[parentIdKey];
|
|
45
|
+
const parentNode = nodeMap.get(parentId);
|
|
46
|
+
|
|
47
|
+
if (parentNode) {
|
|
48
|
+
// 有父节点:添加到父节点的children中
|
|
49
|
+
if (useConcat && Array.isArray(parentNode[childrenKey])) {
|
|
50
|
+
parentNode[childrenKey] = parentNode[childrenKey].concat(node);
|
|
51
|
+
} else {
|
|
52
|
+
parentNode[childrenKey].push(node);
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
// 无父节点:归为根节点(parentId为空/0/不存在)
|
|
56
|
+
rootNodes.push(node);
|
|
26
57
|
}
|
|
27
|
-
}
|
|
28
|
-
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// 3. 清理空children(可选,保持和原逻辑一致)
|
|
61
|
+
for (const node of list) {
|
|
62
|
+
if (Array.isArray(node[childrenKey]) && node[childrenKey].length === 0) {
|
|
63
|
+
delete node[childrenKey];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return rootNodes;
|
|
29
68
|
}
|
|
30
69
|
|
|
31
70
|
/**
|
|
@@ -170,6 +209,11 @@ export function forkProp(obj, leafKey, propName) {
|
|
|
170
209
|
* @param dimMap 维表关联映射字段 e.g.{'市': {propFieldName: 'code1', textFieldName: 'name1'}, '区': {propFieldName: 'code2', textFieldName: 'name2'}, '乡镇': {propFieldName: 'code3', textFieldName: 'name3'}}
|
|
171
210
|
*/
|
|
172
211
|
export function dim2Tree(dimList, dimLink, dimMap) {
|
|
212
|
+
let list = dim2List(dimList, dimLink, dimMap)
|
|
213
|
+
return list2Tree(list);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export function dim2List(dimList, dimLink, dimMap) {
|
|
173
217
|
let filedSort = [];
|
|
174
218
|
if (dimLink.length) {
|
|
175
219
|
for (let propName of dimLink) {
|
|
@@ -193,8 +237,7 @@ export function dim2Tree(dimList, dimLink, dimMap) {
|
|
|
193
237
|
dataTreeOrg.push(node);
|
|
194
238
|
}
|
|
195
239
|
}
|
|
196
|
-
|
|
197
|
-
return list2Tree(dataTreeOrg);
|
|
240
|
+
return uniqBy(item => item.id, dataTreeOrg);
|
|
198
241
|
}
|
|
199
242
|
|
|
200
243
|
/**
|
|
@@ -233,19 +276,58 @@ export function tree2List(treeLike, list = []) {
|
|
|
233
276
|
return list
|
|
234
277
|
}
|
|
235
278
|
|
|
279
|
+
/**
|
|
280
|
+
* 裁剪树形结构到指定层级(高性能版,不裁剪原始树,返回一颗新树)
|
|
281
|
+
* @param {Array} treeLike - 原始树形结构数据
|
|
282
|
+
* @param {number} level - 要保留的层级(从1开始,level=1保留根节点,level=2保留根+一级子节点,以此类推)
|
|
283
|
+
* @returns {Array} 裁剪后的指定层级节点列表
|
|
284
|
+
*/
|
|
236
285
|
export function treeCutLevel(treeLike, level) {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
286
|
+
// 边界值处理:层级≤0 返回空,层级=1 直接返回根节点(浅拷贝避免修改原数据)
|
|
287
|
+
if (level <= 0) return [];
|
|
288
|
+
if (level === 1) {
|
|
289
|
+
// 只保留根节点,浅拷贝根节点(删除children),不影响原数据
|
|
290
|
+
return treeLike.map(node => {
|
|
291
|
+
const newNode = { ...node }; // 浅拷贝当前节点
|
|
292
|
+
delete newNode.children; // 移除子节点,只保留当前层级
|
|
293
|
+
return newNode;
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// 初始化:当前处理的层级节点列表(初始为根节点)
|
|
298
|
+
let currentLevelNodes = [...treeLike]; // 浅拷贝当前层级节点,不修改原数组
|
|
299
|
+
// 存储最终要返回的目标层级节点
|
|
300
|
+
let targetNodes = [];
|
|
301
|
+
|
|
302
|
+
// 遍历到指定层级(从第2层开始,到level层结束)
|
|
303
|
+
for (let currentLevel = 2; currentLevel <= level; currentLevel++) {
|
|
304
|
+
// 收集下一层的所有节点
|
|
305
|
+
const nextLevelNodes = [];
|
|
306
|
+
|
|
307
|
+
for (const node of currentLevelNodes) {
|
|
308
|
+
// 只处理有children的节点
|
|
309
|
+
if (node.children && node.children.length > 0) {
|
|
310
|
+
if (currentLevel === level) {
|
|
311
|
+
// 到达目标层级:浅拷贝当前子节点,删除其children后加入结果
|
|
312
|
+
const cutNodes = node.children.map(child => {
|
|
313
|
+
const newChild = { ...child };
|
|
314
|
+
delete newChild.children;
|
|
315
|
+
return newChild;
|
|
316
|
+
});
|
|
317
|
+
targetNodes.push(...cutNodes);
|
|
318
|
+
} else {
|
|
319
|
+
// 未到目标层级:收集子节点,继续遍历下一层
|
|
320
|
+
nextLevelNodes.push(...node.children);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
244
323
|
}
|
|
324
|
+
|
|
325
|
+
// 更新当前层级节点为下一层
|
|
326
|
+
currentLevelNodes = nextLevelNodes;
|
|
327
|
+
|
|
328
|
+
// 提前终止:如果下一层没有节点,无需继续循环
|
|
329
|
+
if (currentLevelNodes.length === 0) break;
|
|
245
330
|
}
|
|
246
|
-
return target
|
|
247
|
-
}
|
|
248
331
|
|
|
249
|
-
|
|
250
|
-
list2Tree, setParentIds4List, treeFind, treeDel, treeMap, treeFindParent, findLeaf, treeCutLevel
|
|
332
|
+
return targetNodes;
|
|
251
333
|
}
|