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", () => {
|
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\\\\\\#\\?\\(\\)\\[\\]");
|
expect(regex).toBe("some\\.interesting\\-query\\\\\\#\\?\\(\\)\\[\\]");
|
||||||
});
|
});
|
||||||
|
|||||||
@ -3,6 +3,14 @@ import { dockStore } from "../renderer/components/dock/dock.store";
|
|||||||
import { autobind } from "../renderer/utils";
|
import { autobind } from "../renderer/utils";
|
||||||
|
|
||||||
export class SearchStore {
|
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
|
* Text in the search input
|
||||||
*
|
*
|
||||||
@ -63,7 +71,7 @@ export class SearchStore {
|
|||||||
* @returns Array of line indexes [0, 0, 14, 17, 17, 17, 20...]
|
* @returns Array of line indexes [0, 0, 14, 17, 17, 17, 20...]
|
||||||
*/
|
*/
|
||||||
private findOccurrences(lines: string[] = [], query: string): number[] {
|
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
|
return lines
|
||||||
.flatMap((line, index) => Array.from(line.matchAll(regex), () => index));
|
.flatMap((line, index) => Array.from(line.matchAll(regex), () => index));
|
||||||
@ -137,14 +145,6 @@ export class SearchStore {
|
|||||||
return firstLineIndex + occurrence === this.activeOverlayIndex;
|
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
|
@action
|
||||||
private reset(): void {
|
private reset(): void {
|
||||||
this.searchQuery = "";
|
this.searchQuery = "";
|
||||||
|
|||||||
@ -8,7 +8,7 @@ import { action, computed, observable } from "mobx";
|
|||||||
import { observer } from "mobx-react";
|
import { observer } from "mobx-react";
|
||||||
import { Align, ListOnScrollProps } from "react-window";
|
import { Align, ListOnScrollProps } from "react-window";
|
||||||
|
|
||||||
import { searchStore } from "../../../common/search-store";
|
import { SearchStore, searchStore } from "../../../common/search-store";
|
||||||
import { cssNames } from "../../utils";
|
import { cssNames } from "../../utils";
|
||||||
import { Button } from "../button";
|
import { Button } from "../button";
|
||||||
import { Icon } from "../icon";
|
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>
|
if (searchQuery) { // If search is enabled, replace keyword with backgrounded <span>
|
||||||
// Case-insensitive search (lowercasing query and keywords in line)
|
// 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 matches = item.matchAll(regex);
|
||||||
const modified = item.replace(regex, match => match.toLowerCase());
|
const modified = item.replace(regex, match => match.toLowerCase());
|
||||||
// Splitting text line by keyword
|
// Splitting text line by keyword
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user