mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
escapeRegex should be a static function
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
21a1c1ec27
commit
0fa2f07d64
@ -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\\\\\\#\\?\\(\\)\\[\\]");
|
||||
});
|
||||
|
||||
@ -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 = "";
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user