From 95f07e292f2b4f63b520312a74b4816b16d4bae5 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Sun, 19 Apr 2020 22:45:20 +0800 Subject: [PATCH] refactor: abandon regex array --- src/driver/core/FileExplorer.ts | 2 +- src/utils/VisibleNodesGenerator.ts | 22 +++++++++++----------- src/utils/general.ts | 6 +++--- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/driver/core/FileExplorer.ts b/src/driver/core/FileExplorer.ts index 307e96e..fc8fb7a 100644 --- a/src/driver/core/FileExplorer.ts +++ b/src/driver/core/FileExplorer.ts @@ -200,7 +200,7 @@ export const search: BoundMethodCreator<[string]> = dispatch => searchKey => { } export const goTo: BoundMethodCreator<[string[]]> = dispatch => async currentPath => { - visibleNodesGenerator.search([]) + visibleNodesGenerator.search(null) tasksAfterRender.push(() => { dispatch.call(expandTo, currentPath) }) diff --git a/src/utils/VisibleNodesGenerator.ts b/src/utils/VisibleNodesGenerator.ts index 9727b36..3fa1f12 100644 --- a/src/utils/VisibleNodesGenerator.ts +++ b/src/utils/VisibleNodesGenerator.ts @@ -23,7 +23,7 @@ import { findNode } from './general' function search( root: TreeNode, - regexps: RegExp[], + regexp: RegExp, onChildMatch: (node: TreeNode) => void, ): TreeNode | null { // go traverse no matter root matches or not to make sure find all nodes matches @@ -31,14 +31,14 @@ function search( if (root.type === 'tree' && root.contents) { let childMatch = false for (const item of root.contents) { - if (isNodeMatch(item, regexps)) { + if (isNodeMatch(item, regexp)) { childMatch = true break } } for (const item of root.contents) { - const $item = search(item, regexps, onChildMatch) + const $item = search(item, regexp, onChildMatch) if ($item) { if ($item !== item) childMatch = true contents.push($item) @@ -57,14 +57,14 @@ function search( } } - if (isNodeMatch(root, regexps)) { + if (isNodeMatch(root, regexp)) { return root } return null } -function isNodeMatch(root: TreeNode, regexps: RegExp[]): boolean { - return regexps.some(regexp => regexp.test(root.name)) +function isNodeMatch(root: TreeNode, regexp: RegExp): boolean { + return regexp.test(root.name) } function compressTree(root: TreeNode, prefix: string[] = []): TreeNode { @@ -116,8 +116,8 @@ class L2 { this.compress = Boolean(options.compress) } - search = (regexps: RegExp[], onChildMatch: (node: TreeNode) => void) => { - const rootNode = regexps.length ? search(this.l1.root, regexps, onChildMatch) : this.l1.root + search = (regexp: RegExp | null, onChildMatch: (node: TreeNode) => void) => { + const rootNode = regexp ? search(this.l1.root, regexp, onChildMatch) : this.l1.root this.root = rootNode && this.compress @@ -163,9 +163,9 @@ class L3 { return node } - search = (regexps: RegExp[]) => { + search = (regexp: RegExp | null) => { this.expandedNodes.clear() - this.l2.search(regexps, node => this.expandedNodes.add(node.path)) + this.l2.search(regexp, node => this.expandedNodes.add(node.path)) this.generateVisibleNodes() } @@ -249,7 +249,7 @@ export class VisibleNodesGenerator { } init() { - this.search([]) + this.search(null) } get visibleNodes() { diff --git a/src/utils/general.ts b/src/utils/general.ts index 18387b0..b7bd562 100644 --- a/src/utils/general.ts +++ b/src/utils/general.ts @@ -131,14 +131,14 @@ export async function JSONRequest(url: string, data: any, extra: RequestInit = { } export function searchKeyToRegexps(searchKey: string) { - if (!searchKey) return [] + if (!searchKey) return null try { const flags = /[A-Z]/.test(searchKey) ? '' : 'i' // case-sensitive when searchKey contains uppercase char - return [new RegExp(searchKey, flags)] + return new RegExp(searchKey, flags) } catch (err) { - return [/$^/] // matching nothing if failed transforming regexp + return null // matching nothing if failed transforming regexp } }