PlaylistSearchHelper.ets 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { VideoItem } from '../../viewmodel/VideoItem'
  2. function normalizeSearchText(value: string | undefined): string {
  3. return value ? value.trim().toLowerCase() : ''
  4. }
  5. function buildSearchCandidateText(song: VideoItem): string {
  6. const rawFragments: Array<string | undefined> = [
  7. song.name,
  8. song.artist,
  9. song.album,
  10. song.fileName
  11. ]
  12. const fragments: string[] = []
  13. rawFragments.forEach((item: string | undefined): void => {
  14. if (item !== undefined && item.length > 0) {
  15. fragments.push(item)
  16. }
  17. })
  18. return fragments
  19. .join('\n')
  20. .toLowerCase()
  21. }
  22. // 歌单搜索只在当前歌单队列里过滤,保持原始歌单顺序不变。
  23. export function filterPlaylistSongsByKeyword(songs: VideoItem[], keyword: string): VideoItem[] {
  24. const normalizedKeyword = normalizeSearchText(keyword)
  25. if (normalizedKeyword.length === 0) {
  26. return [...songs]
  27. }
  28. return songs.filter((song: VideoItem): boolean => {
  29. return buildSearchCandidateText(song).includes(normalizedKeyword)
  30. })
  31. }
  32. // 播放队列定位未命中时必须返回 -1,避免错误回落到队列第一首。
  33. export function findSongIndexByFilePath(queue: VideoItem[], filePath: string): number {
  34. for (let index = 0; index < queue.length; index++) {
  35. if (queue[index].filePath === filePath) {
  36. return index
  37. }
  38. }
  39. return -1
  40. }