mirror of
https://github.com/pawelmalak/flame.git
synced 2025-07-23 05:19:37 +02:00
Added function to escape regex characters
This commit is contained in:
parent
6281994be8
commit
b848cfd921
3 changed files with 16 additions and 2 deletions
|
@ -22,6 +22,9 @@ import { BookmarkGrid } from '../Bookmarks/BookmarkGrid/BookmarkGrid';
|
||||||
import { SearchBar } from '../SearchBar/SearchBar';
|
import { SearchBar } from '../SearchBar/SearchBar';
|
||||||
import { Header } from './Header/Header';
|
import { Header } from './Header/Header';
|
||||||
|
|
||||||
|
// Utils
|
||||||
|
import { escapeRegex } from '../../utility';
|
||||||
|
|
||||||
export const Home = (): JSX.Element => {
|
export const Home = (): JSX.Element => {
|
||||||
const {
|
const {
|
||||||
apps: { apps, loading: appsLoading },
|
apps: { apps, loading: appsLoading },
|
||||||
|
@ -60,7 +63,9 @@ export const Home = (): JSX.Element => {
|
||||||
if (localSearch) {
|
if (localSearch) {
|
||||||
// Search through apps
|
// Search through apps
|
||||||
setAppSearchResult([
|
setAppSearchResult([
|
||||||
...apps.filter(({ name }) => new RegExp(localSearch, 'i').test(name)),
|
...apps.filter(({ name }) =>
|
||||||
|
new RegExp(escapeRegex(localSearch), 'i').test(name)
|
||||||
|
),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Search through bookmarks
|
// Search through bookmarks
|
||||||
|
@ -70,7 +75,9 @@ export const Home = (): JSX.Element => {
|
||||||
category.bookmarks = categories
|
category.bookmarks = categories
|
||||||
.map(({ bookmarks }) => bookmarks)
|
.map(({ bookmarks }) => bookmarks)
|
||||||
.flat()
|
.flat()
|
||||||
.filter(({ name }) => new RegExp(localSearch, 'i').test(name));
|
.filter(({ name }) =>
|
||||||
|
new RegExp(escapeRegex(localSearch), 'i').test(name)
|
||||||
|
);
|
||||||
|
|
||||||
setBookmarkSearchResult([category]);
|
setBookmarkSearchResult([category]);
|
||||||
} else {
|
} else {
|
||||||
|
|
6
client/src/utility/escapeRegex.ts
Normal file
6
client/src/utility/escapeRegex.ts
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
/**
|
||||||
|
* https://stackoverflow.com/a/30851002/16957052
|
||||||
|
*/
|
||||||
|
export const escapeRegex = (exp: string) => {
|
||||||
|
return exp.replace(/[-[\]{}()*+!<=:?.\/\\^$|#\s,]/g, '\\$&');
|
||||||
|
};
|
|
@ -11,3 +11,4 @@ export * from './validators';
|
||||||
export * from './parseTime';
|
export * from './parseTime';
|
||||||
export * from './decodeToken';
|
export * from './decodeToken';
|
||||||
export * from './applyAuth';
|
export * from './applyAuth';
|
||||||
|
export * from './escapeRegex';
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue