diff options
| author | Pinapelz <yukais@pinapelz.com> | 2026-06-03 01:38:53 -0700 |
|---|---|---|
| committer | Pinapelz <yukais@pinapelz.com> | 2026-06-03 01:38:53 -0700 |
| commit | 4a2f82f06490b7fb277dc6c7558d10c34503a495 (patch) | |
| tree | a206b7dd9777371d07ba1599cf3eff7f52df4d97 /src/helpers/fetchSongs.ts | |
| parent | 0cc204ae751a69f20925d2791c930bbf45e4eeed (diff) | |
add basic daily answer encryption
Diffstat (limited to 'src/helpers/fetchSongs.ts')
| -rw-r--r-- | src/helpers/fetchSongs.ts | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/helpers/fetchSongs.ts b/src/helpers/fetchSongs.ts new file mode 100644 index 0000000..5ae0d83 --- /dev/null +++ b/src/helpers/fetchSongs.ts @@ -0,0 +1,51 @@ +import { Song } from "../types/song"; +let cachedSongs: Song[] | null = null; +function fuzzyMatch(input: string): string { + return input.toLowerCase().replace(/[^0-9a-z ]/gi, ''); +} + + +export async function fetchSongs(useCache=true): Promise<Song[]> { + if (useCache && cachedSongs) { + return cachedSongs; + } + + try { + const response = await fetch('/songs'); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + const songsData: Song[] = await response.json(); + cachedSongs = songsData; + return songsData; + } catch (error) { + console.error("Failed to fetch songs:", error); + throw error; + } +} + +export async function searchSongs(searchTerm: string): Promise<Song[]> { + const songsToSearch = await fetchSongs(); + + const processedSearchTerm = fuzzyMatch(searchTerm); + + const matchingSongs = songsToSearch + .filter((song: Song) => { + const songName = fuzzyMatch(song.name); + const songArtist = fuzzyMatch(song.artist); + + if (songArtist.includes(processedSearchTerm) || songName.includes(processedSearchTerm)) { + return true; + } + return false; + }) + .sort((a, b) => + a.artist.toLowerCase().localeCompare(b.artist.toLocaleLowerCase()) + || a.name.toLowerCase().localeCompare(b.name.toLocaleLowerCase()) + ); + + return matchingSongs; +} +export function getCachedSongs(): Song[] | null { + return cachedSongs; +} |
