fix(FE): escape regular expression to filter dashboards with special characters (#1279)

* fix(FE): escape reg exp to filter dashboards
* test(FE): add type and use uuid v4
This commit is contained in:
rw4nn 2022-06-23 06:55:55 +02:00 committed by GitHub
parent 84b876170d
commit d21ab7b82d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 1 deletions

View File

@ -0,0 +1,58 @@
import { Dashboard } from 'types/api/dashboard/getAll';
import { v4 as uuid } from 'uuid';
import { TOperator } from '../types';
import { executeSearchQueries } from '../utils';
describe('executeSearchQueries', () => {
const firstDashboard: Dashboard = {
id: 11111,
uuid: uuid(),
created_at: '',
updated_at: '',
data: {
title: 'first dashboard',
},
};
const secondDashboard: Dashboard = {
id: 22222,
uuid: uuid(),
created_at: '',
updated_at: '',
data: {
title: 'second dashboard',
},
};
const thirdDashboard: Dashboard = {
id: 333333,
uuid: uuid(),
created_at: '',
updated_at: '',
data: {
title: 'third dashboard (with special characters +?\\)',
},
};
const dashboards = [firstDashboard, secondDashboard, thirdDashboard];
it('should filter dashboards based on title', () => {
const query = {
category: 'title',
id: 'someid',
operator: '=' as TOperator,
value: 'first dashboard',
};
expect(executeSearchQueries([query], dashboards)).toEqual([firstDashboard]);
});
it('should filter dashboards with special characters', () => {
const query = {
category: 'title',
id: 'someid',
operator: '=' as TOperator,
value: 'third dashboard (with special characters +?\\)',
};
expect(executeSearchQueries([query], dashboards)).toEqual([thirdDashboard]);
});
});

View File

@ -42,6 +42,8 @@ export const executeSearchQueries = (
if (!searchData.length || !queries.length) {
return searchData;
}
const escapeRegExp = (regExp: string): string =>
regExp.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
queries.forEach((query: IQueryStructure) => {
const { operator } = query;
@ -61,7 +63,7 @@ export const executeSearchQueries = (
for (const searchSpaceItem of searchSpace) {
if (searchSpaceItem)
for (const queryValue of value) {
if (searchSpaceItem.match(queryValue)) {
if (searchSpaceItem.match(escapeRegExp(queryValue))) {
return resolveOperator(true, operator);
}
}