Skip to content

Commit

Permalink
feat(TreeData): 新增 traverseNode 遍历节点
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Dec 21, 2020
1 parent 8612272 commit c5dd7b1
Show file tree
Hide file tree
Showing 3 changed files with 16,796 additions and 1,999 deletions.
33 changes: 33 additions & 0 deletions src/utils/TreeData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,39 @@ describe('TreeData', () => {
).toMatchSnapshot()
})

test('traverseNode', () => {
expect(
new TreeData(data)
.traverseNode(
_ => _.node.id === '2',
_ => (_.node.id += '_checked'),
)
.export(),
).toMatchSnapshot()
})

test('traverseNodeDFS', () => {
expect(
new TreeData(data)
.traverseNodeDFS(
_ => _.node.id === '2',
_ => (_.node.id += '_checked'),
)
.export(),
).toMatchSnapshot()
})

test('traverseNodeBFS', () => {
expect(
new TreeData(data)
.traverseNodeBFS(
_ => _.node.id === '2',
_ => (_.node.id += '_checked'),
)
.export(),
).toMatchSnapshot()
})

// test('大数据', () => {
// const data = range(0, 1000).map(i => ({
// id: `${i}`,
Expand Down
87 changes: 75 additions & 12 deletions src/utils/TreeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,74 @@ export class TreeData<TNode extends TreeDataNode> {
}
}

/**
* 遍历节点。
*
* @param node 节点
* @param fn 遍历函数
* @param searchStrategy 遍历搜索方式,默认为选项中的遍历搜索方式
*/
traverseNode(
node:
| OneOrMore<TNode>
| ((payload: TreeDataTraverseFnPayload<TNode>) => boolean),
fn: OneOrMore<TreeDataTraverseFn<TNode> | false>,
searchStrategy: TreeDataSearchStrategy = this.searchStrategy,
) {
const nodes: TNode[] =
typeof node === 'function'
? this.findNodeAll(node as any)
: Array.isArray(node)
? node
: [node]
let _node: TNode | undefined
while ((_node = nodes.pop())) {
if (Array.isArray(_node[this.childrenPropName])) {
const fns: Array<TreeDataTraverseFn<TNode>> = (Array.isArray(fn)
? fn
: [fn]
).filter(fn => typeof fn === 'function') as any
for (let i = 0; i < fns.length; i++) {
TreeData.traverse<TNode>(
_node[this.childrenPropName],
this.childrenPropName,
searchStrategy,
fns[i],
)
}
}
}
return this
}

/**
* 深度优先遍历节点。
*
* @param fn 遍历函数
*/
traverseNodeDFS(
node:
| OneOrMore<TNode>
| ((payload: TreeDataTraverseFnPayload<TNode>) => boolean),
fn: OneOrMore<TreeDataTraverseFn<TNode> | false>,
): this {
return this.traverseNode(node, fn, 'DFS')
}

/**
* 广度优先遍历节点。
*
* @param fn 遍历函数
*/
traverseNodeBFS(
node:
| OneOrMore<TNode>
| ((payload: TreeDataTraverseFnPayload<TNode>) => boolean),
fn: OneOrMore<TreeDataTraverseFn<TNode> | false>,
): this {
return this.traverseNode(node, fn, 'BFS')
}

/**
* 遍历。
*
Expand All @@ -218,18 +286,13 @@ export class TreeData<TNode extends TreeDataNode> {
fn: OneOrMore<TreeDataTraverseFn<TNode> | false>,
searchStrategy: TreeDataSearchStrategy = this.searchStrategy,
): this {
const fns: Array<TreeDataTraverseFn<TNode>> = (Array.isArray(fn)
? fn
: [fn]
).filter(fn => typeof fn === 'function') as any
for (let i = 0; i < fns.length; i++) {
TreeData.traverse<TNode>(
this.data,
this.childrenPropName,
searchStrategy,
fns[i],
)
}
this.traverseNode(
{
[this.childrenPropName]: this.data,
} as any,
fn,
searchStrategy,
)
return this
}

Expand Down
Loading

0 comments on commit c5dd7b1

Please sign in to comment.