blob: 13fa7942a2cd5a77631f1f2805bd86213e2c51fd (
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
|
import { songs } from "../constants";
import { Song } from "../types/song";
export function searchSong(searchTerm: string): Song[] {
function fuzzyMatch(input: string){
return input.toLowerCase().replace(/[^0-9a-z ]/gi, '');
}
searchTerm = fuzzyMatch(searchTerm);
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())
);
}
|