blob: 9ce36cf42e176d571f62867686e68805d9d9975d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import { fetchSongs } from "./fetchSongs";
import { Song } from "../types/song";
export async function searchSong(searchTerm: string): Promise<Song[]> {
function fuzzyMatch(input: string){
return input.toLowerCase().replace(/[^0-9a-z ]/gi, '');
}
searchTerm = fuzzyMatch(searchTerm);
const songs = await fetchSongs();
return songs
.filter((song: Song) => {
const songName = fuzzyMatch(song.name);
const songArtist = fuzzyMatch(song.artist);
if (songArtist.includes(searchTerm) || songName.includes(searchTerm)) {
return song;
}
})
.sort((a, b) =>
a.artist.toLowerCase().localeCompare(b.artist.toLocaleLowerCase())
|| a.name.toLowerCase().localeCompare(b.name.toLocaleLowerCase())
);
}
|