From 9da5d0df116170358480b85214f072e86860c26f Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Wed, 14 Oct 2020 23:12:42 +0800 Subject: [PATCH] refactor: simplify search --- src/utils/VisibleNodesGenerator.ts | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/utils/VisibleNodesGenerator.ts b/src/utils/VisibleNodesGenerator.ts index 5e13059..dc4a7b0 100644 --- a/src/utils/VisibleNodesGenerator.ts +++ b/src/utils/VisibleNodesGenerator.ts @@ -14,7 +14,7 @@ import { findNode } from './general' * | | | expandedNodes + focusNode -> visibleNodes * |3 expansion | when fold/unfold | onExpansionChange * | | | searchedNodes + toggleNode -> expandedNodes - * |2 search key | when search | onSearch + * |2 filters | when filter(search) | onSearch * | | | treeNodes + searchKey -> searchedNodes * |1 tree: { root <-> nodes } | when tree init | treeHelper.parse * | | tree data from api -> { root, nodes } @@ -23,22 +23,23 @@ import { findNode } from './general' function search( root: TreeNode, - regexp: RegExp, + match: (node: TreeNode) => boolean, onChildMatch: (node: TreeNode) => void, ): TreeNode | null { - // go traverse no matter root matches or not to make sure find all nodes matches + // go traverse no matter root matches or not to make sure find all nodes const contents = [] + if (root.type === 'tree' && root.contents) { let childMatch = false for (const item of root.contents) { - if (isNodeMatch(item, regexp)) { + if (match(item)) { childMatch = true break } } for (const item of root.contents) { - const $item = search(item, regexp, onChildMatch) + const $item = search(item, match, onChildMatch) if ($item) { if ($item !== item) childMatch = true contents.push($item) @@ -57,16 +58,12 @@ function search( } } - if (isNodeMatch(root, regexp)) { + if (match(root)) { return root } return null } -function isNodeMatch(root: TreeNode, regexp: RegExp): boolean { - return regexp.test(root.name) -} - function compressTree(root: TreeNode, prefix: string[] = []): TreeNode { if (root.contents) { if (root.contents.length === 1) { @@ -116,8 +113,11 @@ class L2 { this.compress = Boolean(options.compress) } - search = (regexp: RegExp | null, onChildMatch: (node: TreeNode) => void) => { - const rootNode = regexp ? search(this.l1.root, regexp, onChildMatch) : this.l1.root + search = ( + match: null | ((node: TreeNode) => boolean), + onChildMatch: (node: TreeNode) => void, + ) => { + const rootNode = match ? search(this.l1.root, match, onChildMatch) : this.l1.root this.root = rootNode && this.compress @@ -179,7 +179,9 @@ class L3 { search = (regexp: RegExp | null) => { this.expandedNodes.clear() - this.l2.search(regexp, node => this.expandedNodes.add(node.path)) + this.l2.search(regexp && (node => regexp.test(node.name)), node => + this.expandedNodes.add(node.path), + ) this.generateVisibleNodes() }