Unescape regex string (#45137)

This commit is contained in:
Ashley Harrison
2022-02-10 14:45:39 +00:00
committed by GitHub
parent fd5e968bc1
commit ec48b81388
2 changed files with 16 additions and 5 deletions
+3 -3
View File
@@ -1,4 +1,4 @@
import { PanelPluginMeta, PluginState } from '@grafana/data';
import { PanelPluginMeta, PluginState, unEscapeStringFromRegex } from '@grafana/data';
import { config } from 'app/core/config';
export function getAllPanelPluginMeta(): PanelPluginMeta[] {
@@ -12,7 +12,7 @@ export function getAllPanelPluginMeta(): PanelPluginMeta[] {
export function filterPluginList(
pluginsList: PanelPluginMeta[],
searchQuery: string,
searchQuery: string, // Note: this will be an escaped regex string as it comes from `FilterInput`
current: PanelPluginMeta
): PanelPluginMeta[] {
if (!searchQuery.length) {
@@ -24,7 +24,7 @@ export function filterPluginList(
});
}
const query = searchQuery.toLowerCase();
const query = unEscapeStringFromRegex(searchQuery).toLowerCase();
const first: PanelPluginMeta[] = [];
const match: PanelPluginMeta[] = [];
const isGraphQuery = 'graph'.startsWith(query);
+13 -2
View File
@@ -1,4 +1,4 @@
import { PanelPluginMeta } from '@grafana/data';
import { PanelPluginMeta, escapeStringForRegex } from '@grafana/data';
import { filterPluginList } from './util';
describe('panel state utils', () => {
@@ -8,7 +8,18 @@ describe('panel state utils', () => {
{ id: 'timeseries', name: 'Graph (old)' },
{ id: 'timeline', name: 'Timeline' },
];
const found = filterPluginList(pluginsList, 'gra', { id: 'xyz' } as any);
const found = filterPluginList(pluginsList, escapeStringForRegex('gra'), { id: 'xyz' } as any);
expect(found.map((v) => v.id)).toEqual(['graph', 'timeseries']);
});
it('should handle escaped regex characters in the search query (e.g. -)', async () => {
const pluginsList: PanelPluginMeta[] = [
{ id: 'graph', name: 'Graph (old)' } as any,
{ id: 'timeseries', name: 'Graph (old)' },
{ id: 'timeline', name: 'Timeline' },
{ id: 'panelwithdashes', name: 'Panel-With-Dashes' },
];
const found = filterPluginList(pluginsList, escapeStringForRegex('panel-'), { id: 'xyz' } as any);
expect(found.map((v) => v.id)).toEqual(['panelwithdashes']);
});
});