31 lines
741 B
TypeScript
31 lines
741 B
TypeScript
type SearchableSong = {
|
|
id: string
|
|
title: string
|
|
artist: { name: string }
|
|
album: { title: string }
|
|
}
|
|
|
|
/**
|
|
* Case-insensitive match on title, artist, or album, plus the songs in
|
|
* `lyricsMatches` (from `api.lyrics.matchingSongIds`). A blank query keeps
|
|
* every song.
|
|
*/
|
|
export function filterSongs<T extends SearchableSong>(
|
|
songs: T[],
|
|
query: string,
|
|
lyricsMatches?: readonly string[]
|
|
) {
|
|
const normalized = query.trim().toLowerCase()
|
|
if (!normalized) {
|
|
return songs
|
|
}
|
|
const byLyrics = new Set(lyricsMatches)
|
|
return songs.filter(
|
|
(song) =>
|
|
byLyrics.has(song.id) ||
|
|
[song.title, song.artist.name, song.album.title].some((value) =>
|
|
value.toLowerCase().includes(normalized)
|
|
)
|
|
)
|
|
}
|