Skip to content

Commit 12e7afe

Browse files
authored
Merge pull request #4 from GreatAuk/bump-dep
升级开发依赖包
2 parents 7d2fb23 + afd29ff commit 12e7afe

File tree

4 files changed

+393
-241
lines changed

4 files changed

+393
-241
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939
"@types/node": "^18.14.6",
4040
"@vitest/coverage-v8": "^3.2.4",
4141
"bumpp": "^9.0.0",
42-
"oxlint": "^1.3.0",
42+
"oxlint": "^1.32.0",
4343
"rimraf": "^4.4.0",
4444
"stale-dep": "^0.8.2",
45-
"tsdown": "^0.12.9",
45+
"tsdown": "^0.17.3",
4646
"tsx": "^3.12.3",
4747
"turbo": "^2.5.4",
4848
"typescript": "^5.8.3",

packages/tree/src/treeFilterNode.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,40 @@ interface Options<TreeNode> {
1616
* @param {Options} [options] - {
1717
* @returns A filtered tree
1818
* @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/tree/src/treeFilterNode.ts
19+
* @example
20+
* ```
21+
const tree = [
22+
{
23+
id: 'root',
24+
children: [
25+
{
26+
id: 'child1',
27+
children: [
28+
{
29+
id: 'child1-1',
30+
},
31+
],
32+
},
33+
{
34+
id: 'child2',
35+
},
36+
],
37+
},
38+
]
39+
const filteredTree = treeFilterNode(tree, node => node.id === 'child2')
40+
console.log(filteredTree)
41+
// output
42+
// [
43+
// {
44+
// "children": [
45+
// {
46+
// "id": "child2",
47+
// },
48+
// ],
49+
// "id": "root",
50+
// },
51+
// ]
52+
* ```
1953
*/
2054
export function treeFilterNode<TreeNode>(tree: TreeNode[] | TreeNode, predicate: (node: TreeNode) => boolean, options?: Options<TreeNode>): TreeNode[] {
2155
const { fieldNames, onEachTraverse } = options || {}

packages/tree/src/treeFindPath.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,31 @@ interface Options {
2727
},
2828
]
2929
30-
const path = treeFindPath(tree, node => node.name === 'b')
31-
console.log(path?.map(v => v.name)) // ['a', 'b']
30+
const pathNodes = treeFindPath(tree, node => node.name === 'b')
31+
console.log(pathNodes?.map(v => v.name)) // ['a', 'b']
3232
* ```
3333
*/
3434
export function treeFindPath<TreeNode>(tree: TreeNode[] | TreeNode, predicate: (node: TreeNode) => boolean, options?: Options): TreeNode[] | null {
3535
const { fieldNames } = options || {}
3636
const { children: childrenField } = { ...DEFAULT_FIELD_NAMES, ...fieldNames }
3737

38-
const path: TreeNode[] = []
38+
const pathNodes: TreeNode[] = []
3939
const queue = Array.isArray(tree) ? [...tree] : [tree]
4040
const visitedSet = new Set<TreeNode>()
4141

4242
while (queue.length) {
4343
const node = queue[0]
4444
if (visitedSet.has(node)) {
45-
path.pop()
45+
pathNodes.pop()
4646
queue.shift()
4747
}
4848
else {
4949
visitedSet.add(node)
5050
// @ts-expect-error - dynamic field name
5151
node[childrenField] && queue.unshift(...node[childrenField])
52-
path.push(node)
52+
pathNodes.push(node)
5353
if (predicate(node))
54-
return path
54+
return pathNodes
5555
}
5656
}
5757
return null

0 commit comments

Comments
 (0)