Преглед изворни кода

fix(card): support timestamps without milliseconds

onecold пре 4 месеци
родитељ
комит
28e49d6560

+ 5 - 3
entry/src/main/ets/common/player/MusicCardSnapshot.ets

@@ -1,7 +1,7 @@
 import { MUSIC_CARD_EMPTY_ARTIST, MUSIC_CARD_EMPTY_TITLE } from './MusicCardConstants'
 
 const DEFAULT_TIME_TEXT = '00:00'
-const LYRIC_TIMESTAMP_PATTERN = /\[(\d{2}):(\d{2})\.(\d{2,3})\]/g
+const LYRIC_TIMESTAMP_PATTERN = /\[(\d{2}):(\d{2})(?:\.(\d{2,3}))?\]/g
 
 const isNonEmpty = (value?: string): boolean => {
   return !!value && value.trim() !== ''
@@ -101,8 +101,10 @@ export function resolveMusicCardLyricLines(
       const minutes = parseInt(match[1], 10)
       const seconds = parseInt(match[2], 10)
       const msPart = match[3]
-      const msValue =
-        msPart.length === 2 ? parseInt(msPart, 10) * 10 : parseInt(msPart, 10)
+      let msValue = 0
+      if (msPart) {
+        msValue = msPart.length === 2 ? parseInt(msPart, 10) * 10 : parseInt(msPart, 10)
+      }
       const timeMs = minutes * 60 * 1000 + seconds * 1000 + msValue
 
       timestamps.push({ timeMs })

+ 8 - 0
entry/src/ohosTest/ets/test/MusicCardSnapshot.test.ets

@@ -48,6 +48,14 @@ export default function musicCardSnapshotTest() {
       expect(lines.hasLyric).assertEqual(false)
     })
 
+    it('resolveMusicCardLyricLinesHandlesNoMilliseconds', 0, () => {
+      const lines = resolveMusicCardLyricLines('[00:05]无毫秒', 6000)
+
+      expect(lines.line1).assertEqual('无毫秒')
+      expect(lines.line2).assertEqual('')
+      expect(lines.hasLyric).assertEqual(true)
+    })
+
     it('buildMusicCardBindingDataForEmptySnapshot', 0, () => {
       const binding = buildMusicCardBindingData(createEmptyMusicCardSnapshot())