escapeRegex should be a static function

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2021-03-09 13:06:24 -05:00
1 parent 21a1c1ec27
commit 0fa2f07d64
3 files changed
+12 -12

No files matched your search

+1 -1
View File
@@ -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\\\\\\#\\?\\(\\)\\[\\]");
});
+9 -9
View File
@@ -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 = "";
+2 -2
View File
@@ -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<Props> {
if (searchQuery) { // If search is enabled, replace keyword with backgrounded <span>
// 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