فهرست منبع

feat(tree): 增加树结构检测

vian 3 سال پیش
والد
کامیت
7e65fbe448
2فایلهای تغییر یافته به همراه15 افزوده شده و 1 حذف شده
  1. 1 1
      tree/package.json
  2. 14 0
      tree/src/tree.ts

+ 1 - 1
tree/package.json

@@ -1,6 +1,6 @@
 {
   "name": "@sc/tree",
-  "version": "1.0.18",
+  "version": "1.0.19",
   "description": "通用树",
   "main": "./dist/index.cjs.js",
   "module": "./dist/index.esm.js",

+ 14 - 0
tree/src/tree.ts

@@ -659,4 +659,18 @@ export class Tree<T extends TreeRaw = TreeRaw> {
     Object.keys(this.IDMap).forEach(key => delete this.IDMap[key]);
     Object.keys(this.ctxMap).forEach(key => delete this.ctxMap[key]);
   }
+
+  // 检测树结构
+  check(): TreeNode<T>[] {
+    const errNodes: TreeNode<T>[] = [];
+    errNodes.push(...this.checkParent());
+    return errNodes;
+  }
+
+  // 检查找不到父项的(parentID对应的节点不存在),返回有问题的节点
+  private checkParent(): TreeNode<T>[] {
+    return this.rawData.filter(
+      node => node.parentID !== this.rootID && !this.IDMap[node.parentID]
+    );
+  }
 }