aboutsummaryrefslogtreecommitdiffstats
path: root/src/helpers/searchSong.ts
blob: 62091b60a6fbcd4f8be4e7b193ff2b3211d95ed3 (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
26
import { songs } from "../../server/data/songs";
import { Song } from "../types/song";

function fuzzyMatch(input: string): string {
  return input.toLowerCase().replace(/[^0-9a-z ]/gi, "");
}

export function searchSong(searchTerm: string): Song[] {
  const normalizedSearch = fuzzyMatch(searchTerm);

  return songs
    .filter((song: Song) => {
      const songName = fuzzyMatch(song.name);
      const songArtist = fuzzyMatch(song.artist);

      return (
        songArtist.includes(normalizedSearch) ||
        songName.includes(normalizedSearch)
      );
    })
    .sort(
      (a, b) =>
        a.artist.toLowerCase().localeCompare(b.artist.toLowerCase()) ||
        a.name.toLowerCase().localeCompare(b.name.toLowerCase())
    );
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage