@d-matrix/utils 1.23.0 → 1.25.0

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.
@@ -14,3 +14,20 @@ export declare const nodeCountAtDepth: <T extends Record<string, any>>(root: T,
14
14
  * @returns 找到的节点
15
15
  */
16
16
  export declare const findNode: <T extends Record<string, any>>(tree: T[], predicate: (node: T) => boolean, childrenKey?: string) => T | null;
17
+ /**
18
+ * 根据子节点查找父节点
19
+ * @param tree
20
+ * @param child 子节点
21
+ * @param indentityKey 节点唯一id字段名称
22
+ * @param childrenKey 节点的子节点的字段名称
23
+ * @returns 父节点
24
+ */
25
+ export declare const findParent: <T extends Record<string, any>>(tree: T | undefined, child: T | undefined, indentityKey?: string, childrenKey?: string) => T | null;
26
+ /**
27
+ * 查找节点路径
28
+ * @param tree
29
+ * @param func 符合条件的节点函数,返回 boolean
30
+ * @param childrenKey 子节点key
31
+ * @returns 节点路径
32
+ */
33
+ export declare function findPath<T extends Record<string, any>>(tree: T[], func: (node: T) => boolean, childrenKey?: string): T[] | null;
@@ -41,3 +41,57 @@ export const findNode = (tree, predicate, childrenKey = 'children') => {
41
41
  }
42
42
  return null;
43
43
  };
44
+ /**
45
+ * 根据子节点查找父节点
46
+ * @param tree
47
+ * @param child 子节点
48
+ * @param indentityKey 节点唯一id字段名称
49
+ * @param childrenKey 节点的子节点的字段名称
50
+ * @returns 父节点
51
+ */
52
+ export const findParent = (tree, child, indentityKey = 'id', childrenKey = 'children') => {
53
+ if (tree === undefined || child === undefined) {
54
+ return null;
55
+ }
56
+ const children = tree[childrenKey];
57
+ if (Array.isArray(children)) {
58
+ const count = children.length;
59
+ for (let i = 0; i < count; i++) {
60
+ if (children[i][indentityKey] === child[indentityKey]) {
61
+ return tree;
62
+ }
63
+ const parent = findParent(children[i], child, indentityKey, childrenKey);
64
+ if (parent !== null) {
65
+ return parent;
66
+ }
67
+ }
68
+ }
69
+ return null;
70
+ };
71
+ /**
72
+ * 查找节点路径
73
+ * @param tree
74
+ * @param func 符合条件的节点函数,返回 boolean
75
+ * @param childrenKey 子节点key
76
+ * @returns 节点路径
77
+ */
78
+ export function findPath(tree, func, childrenKey = 'children') {
79
+ const path = [];
80
+ const list = [...tree];
81
+ const visitedSet = new Set();
82
+ while (list.length) {
83
+ const node = list[0];
84
+ if (visitedSet.has(node)) {
85
+ path.pop();
86
+ list.shift();
87
+ }
88
+ else {
89
+ visitedSet.add(node);
90
+ Array.isArray(node[childrenKey]) && list.unshift(...node[childrenKey]);
91
+ path.push(node);
92
+ if (func(node))
93
+ return path;
94
+ }
95
+ }
96
+ return null;
97
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@d-matrix/utils",
3
3
  "sideEffects": false,
4
- "version": "1.23.0",
4
+ "version": "1.25.0",
5
5
  "description": "A dozen of utils for Front-End Development",
6
6
  "main": "dist/index.js",
7
7
  "scripts": {
package/readme.md CHANGED
@@ -390,6 +390,40 @@ const actual2 = tree.findNode([root], (node) => node.id === 33);
390
390
  expect(actual2).to.be.deep.equal(root.children[1].children[2]);
391
391
  ```
392
392
 
393
+ - `tree.findNode(tree, child, indentityKey, childrenKey)`
394
+
395
+ 根据子节点查找父节点
396
+
397
+ ```ts
398
+ const treeData = {
399
+ code: 1,
400
+ subs: [
401
+ { code: 2, subs: [{ code: 21 }, { code: 22 }, { code: 23 }] },
402
+ { code: 3, subs: [{ code: 31 }, { code: 32 }, { code: 33 }] },
403
+ ],
404
+ };
405
+
406
+ const actual = tree.findParent(treeData, treeData.subs[1].subs[2], 'code', 'subs');
407
+ expect(actual).to.be.deep.equal(treeData.subs[1]);
408
+ ```
409
+
410
+ - `function findPath<T extends Record<string, any>>(tree: T[], func: (node: T) => boolean, childrenKey = 'children'): T[] | null`
411
+
412
+ 查找节点路径, 返回节点数组或`null`
413
+
414
+ ```ts
415
+ const treeData = {
416
+ code: 1,
417
+ subs: [
418
+ { code: 2, subs: [{ code: 21 }, { code: 22 }, { code: 23 }] },
419
+ { code: 3, subs: [{ code: 31 }, { code: 32 }, { code: 33 }] },
420
+ ],
421
+ };
422
+
423
+ const actual = tree.findPath([root], (node) => node.id === 33);
424
+ expect(actual).to.be.deep.equal([root, root.children[1], root.children[1].children[2]]);
425
+ ```
426
+
393
427
  ### file
394
428
 
395
429
  - `toImage(file: BlobPart | FileURL, options?: BlobPropertyBag): Promise<HTMLImageElement>`