| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import { VideoItem } from '../../viewmodel/VideoItem'
- function normalizeSearchText(value: string | undefined): string {
- return value ? value.trim().toLowerCase() : ''
- }
- function buildSearchCandidateText(song: VideoItem): string {
- const rawFragments: Array<string | undefined> = [
- song.name,
- song.artist,
- song.album,
- song.fileName
- ]
- const fragments: string[] = []
- rawFragments.forEach((item: string | undefined): void => {
- if (item !== undefined && item.length > 0) {
- fragments.push(item)
- }
- })
- return fragments
- .join('\n')
- .toLowerCase()
- }
- // 歌单搜索只在当前歌单队列里过滤,保持原始歌单顺序不变。
- export function filterPlaylistSongsByKeyword(songs: VideoItem[], keyword: string): VideoItem[] {
- const normalizedKeyword = normalizeSearchText(keyword)
- if (normalizedKeyword.length === 0) {
- return [...songs]
- }
- return songs.filter((song: VideoItem): boolean => {
- return buildSearchCandidateText(song).includes(normalizedKeyword)
- })
- }
- // 播放队列定位未命中时必须返回 -1,避免错误回落到队列第一首。
- export function findSongIndexByFilePath(queue: VideoItem[], filePath: string): number {
- for (let index = 0; index < queue.length; index++) {
- if (queue[index].filePath === filePath) {
- return index
- }
- }
- return -1
- }
|