From 1f1451735b51ad3a66e7b73d307f66bba938e193 Mon Sep 17 00:00:00 2001 From: Brendan F Date: Wed, 12 Jul 2023 23:33:47 -0700 Subject: Ignore non-alphanumeric chars in search --- src/helpers/searchSong.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/helpers/searchSong.ts b/src/helpers/searchSong.ts index 01f6304..4246480 100644 --- a/src/helpers/searchSong.ts +++ b/src/helpers/searchSong.ts @@ -2,12 +2,15 @@ import { songs } from "../constants"; import { Song } from "../types/song"; export function searchSong(searchTerm: string): Song[] { - searchTerm = searchTerm.toLowerCase(); + function fuzzyMatch(input: string){ + return input.toLowerCase().replace(/[^0-9a-z ]/gi, ''); + } + searchTerm = fuzzyMatch(searchTerm); return songs .filter((song: Song) => { - const songName = song.name.toLowerCase(); - const songArtist = song.artist.toLowerCase(); + const songName = fuzzyMatch(song.name); + const songArtist = fuzzyMatch(song.artist); if (songArtist.includes(searchTerm) || songName.includes(searchTerm)) { return song; -- cgit v1.2.3