@d-matrix/utils 1.24.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.
- package/dist/algorithm/tree.d.ts +9 -1
- package/dist/algorithm/tree.js +28 -1
- package/package.json +1 -1
- package/readme.md +17 -0
package/dist/algorithm/tree.d.ts
CHANGED
|
@@ -20,6 +20,14 @@ export declare const findNode: <T extends Record<string, any>>(tree: T[], predic
|
|
|
20
20
|
* @param child 子节点
|
|
21
21
|
* @param indentityKey 节点唯一id字段名称
|
|
22
22
|
* @param childrenKey 节点的子节点的字段名称
|
|
23
|
-
* @returns
|
|
23
|
+
* @returns 父节点
|
|
24
24
|
*/
|
|
25
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;
|
package/dist/algorithm/tree.js
CHANGED
|
@@ -47,7 +47,7 @@ export const findNode = (tree, predicate, childrenKey = 'children') => {
|
|
|
47
47
|
* @param child 子节点
|
|
48
48
|
* @param indentityKey 节点唯一id字段名称
|
|
49
49
|
* @param childrenKey 节点的子节点的字段名称
|
|
50
|
-
* @returns
|
|
50
|
+
* @returns 父节点
|
|
51
51
|
*/
|
|
52
52
|
export const findParent = (tree, child, indentityKey = 'id', childrenKey = 'children') => {
|
|
53
53
|
if (tree === undefined || child === undefined) {
|
|
@@ -68,3 +68,30 @@ export const findParent = (tree, child, indentityKey = 'id', childrenKey = 'chil
|
|
|
68
68
|
}
|
|
69
69
|
return null;
|
|
70
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
package/readme.md
CHANGED
|
@@ -407,6 +407,23 @@ const actual = tree.findParent(treeData, treeData.subs[1].subs[2], 'code', 'subs
|
|
|
407
407
|
expect(actual).to.be.deep.equal(treeData.subs[1]);
|
|
408
408
|
```
|
|
409
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
|
+
|
|
410
427
|
### file
|
|
411
428
|
|
|
412
429
|
- `toImage(file: BlobPart | FileURL, options?: BlobPropertyBag): Promise<HTMLImageElement>`
|