Merge pull request #112 from TheRealOwenRees/search

Improve search
This commit is contained in:
Owen Rees
2023-07-19 17:26:07 +02:00
committed by GitHub
2 changed files with 13 additions and 4 deletions
+4 -4
View File
@@ -3,6 +3,7 @@ import { atom } from "jotai";
import { LatLngBounds } from "leaflet";
import atomWithDebounce from "@/utils/atomWithDebounce";
import { normalizedContains } from "@/utils/string";
export const tournamentsAtom = atom<Tournament[]>([]);
export const mapBoundsAtom = atom<LatLngBounds | null>(null);
@@ -52,10 +53,9 @@ export const filteredTournamentsListAtom = atom((get) => {
: tournaments;
// When searching, we search all the tournament, regardless of the map display
if (searchString !== "")
return filteredByNorm.filter((t) =>
t.town.includes(searchString.toUpperCase()),
);
if (searchString !== "") {
return filteredByNorm.filter((t) => normalizedContains(t.town, searchString) || normalizedContains(t.tournament, searchString));
}
// If we not syncing to the map, return all tournaments
if (mapBounds === null || !syncVisible) return filteredByNorm;
+9
View File
@@ -0,0 +1,9 @@
export const removeDiacritics = (str: string) => {
// We normalize to the composed Unicode form, then we can remove the accents
return str.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
};
export const normalizedContains = (haystack: string, needle: string) => {
const regExp = new RegExp(removeDiacritics(needle), "gi");
return regExp.test(removeDiacritics(haystack));
};