diff --git a/static/js/filter.js b/static/js/filter.js index 83fd7c7..4c5f7a5 100644 --- a/static/js/filter.js +++ b/static/js/filter.js @@ -20,15 +20,18 @@ function buttonToggle(clickedButton) { } function filterSongs() { - const searchTerm = search.value.trim().toLowerCase().normalize("NFD").replace(/\p{Diacritic}/gu, ""); + const searchTerm = sanitizeString(search.value); const songs = Array.from(songList.children); songs.forEach(song => { - const title = song.dataset.title.toLowerCase().normalize("NFD").replace(/\p{Diacritic}/gu, ""); - const matching = title.includes(searchTerm) && (!selectedCategory || song.dataset.category === selectedCategory); + const matching = song.dataset.title.includes(searchTerm) && (!selectedCategory || song.dataset.category === selectedCategory); song.classList.toggle("hidden", !matching); }); } +function sanitizeString(string) { + return string.trim().toLowerCase().normalize("NFD").replace(/\p{Diacritic}/gu, "") +} + // Event listeners search.addEventListener("input", filterSongs); // Filtering happens before the reset itself without this timeout @@ -37,7 +40,10 @@ buttons.forEach(button => button.addEventListener("click", () => buttonToggle(bu // Normalize song titles Array.from(songList.children).forEach(song => { - song.dataset.title = song.dataset.title.toLowerCase().normalize("NFD").replace(/\p{Diacritic}/gu, ""); + song.dataset.title = sanitizeString(song.dataset.title); + if (song.dataset.artist) { + song.dataset.artist = sanitizeString(song.dataset.artist); + } }); // Display the filter section on JS-enabled browsers