From 0fa2f07d64e066c9e92c6c5c0b93f83072191da1 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Tue, 9 Mar 2021 10:01:24 -0500 Subject: [PATCH] escapeRegex should be a static function Signed-off-by: Sebastian Malton --- src/common/__tests__/search-store.test.ts | 2 +- src/common/search-store.ts | 18 +++++++++--------- src/renderer/components/dock/log-list.tsx | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/common/__tests__/search-store.test.ts b/src/common/__tests__/search-store.test.ts index 33220ea2e8..27d52971de 100644 --- a/src/common/__tests__/search-store.test.ts +++ b/src/common/__tests__/search-store.test.ts @@ -62,7 +62,7 @@ describe("search store tests", () => { }); it("escapes string for using in regex", () => { - const regex = (searchStore as any).escapeRegex("some.interesting-query\\#?()[]"); + const regex = SearchStore.escapeRegex("some.interesting-query\\#?()[]"); expect(regex).toBe("some\\.interesting\\-query\\\\\\#\\?\\(\\)\\[\\]"); }); diff --git a/src/common/search-store.ts b/src/common/search-store.ts index 2141351ce7..75a4100969 100644 --- a/src/common/search-store.ts +++ b/src/common/search-store.ts @@ -3,6 +3,14 @@ import { dockStore } from "../renderer/components/dock/dock.store"; import { autobind } from "../renderer/utils"; export class SearchStore { + /** + * An utility methods escaping user string to safely pass it into new Regex(variable) + * @param value Unescaped string + */ + public static escapeRegex(value: string): string { + return value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&"); + } + /** * Text in the search input * @@ -63,7 +71,7 @@ export class SearchStore { * @returns Array of line indexes [0, 0, 14, 17, 17, 17, 20...] */ private findOccurrences(lines: string[] = [], query: string): number[] { - const regex = new RegExp(this.escapeRegex(query), "gi"); + const regex = new RegExp(SearchStore.escapeRegex(query), "gi"); return lines .flatMap((line, index) => Array.from(line.matchAll(regex), () => index)); @@ -137,14 +145,6 @@ export class SearchStore { return firstLineIndex + occurrence === this.activeOverlayIndex; } - /** - * An utility methods escaping user string to safely pass it into new Regex(variable) - * @param value Unescaped string - */ - private escapeRegex(value: string): string { - return value.replace( /[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&" ); - } - @action private reset(): void { this.searchQuery = ""; diff --git a/src/renderer/components/dock/log-list.tsx b/src/renderer/components/dock/log-list.tsx index 3b66f42d86..fc6efacdfa 100644 --- a/src/renderer/components/dock/log-list.tsx +++ b/src/renderer/components/dock/log-list.tsx @@ -8,7 +8,7 @@ import { action, computed, observable } from "mobx"; import { observer } from "mobx-react"; import { Align, ListOnScrollProps } from "react-window"; -import { searchStore } from "../../../common/search-store"; +import { SearchStore, searchStore } from "../../../common/search-store"; import { cssNames } from "../../utils"; import { Button } from "../button"; import { Icon } from "../icon"; @@ -164,7 +164,7 @@ export class LogList extends React.Component { if (searchQuery) { // If search is enabled, replace keyword with backgrounded // Case-insensitive search (lowercasing query and keywords in line) - const regex = new RegExp(searchStore.escapeRegex(searchQuery), "gi"); + const regex = new RegExp(SearchStore.escapeRegex(searchQuery), "gi"); const matches = item.matchAll(regex); const modified = item.replace(regex, match => match.toLowerCase()); // Splitting text line by keyword