From 769ba47a1de3b98ccac22ca94fe3b5fa384c263f Mon Sep 17 00:00:00 2001 From: memorydream <34763046+memorydream@users.noreply.github.com> Date: Fri, 28 Jan 2022 14:54:40 +0800 Subject: [PATCH] fix(utils/lyrics): [min:sec] timestamp lyrics parse (#1277) * fix(utils/lyrics): [min:sec] timestamp lyrics parse * refactor(utils/lyrics): simplify time expression * style(utils/lyrics): remove duplicate space Co-authored-by: pan93412 --- src/utils/lyrics.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/utils/lyrics.js b/src/utils/lyrics.js index 7fee5aa..517e9f4 100644 --- a/src/utils/lyrics.js +++ b/src/utils/lyrics.js @@ -9,7 +9,7 @@ export function lyricParser(lrc) { // regexr.com/6e52n const extractLrcRegex = /^(?(?:\[.+?\])+)(?!\[)(?.+)$/gm; -const extractTimestampRegex = /\[(?\d+):(?\d+)(?:\.|:)(?\d+)\]/g; +const extractTimestampRegex = /\[(?\d+):(?\d+)(?:\.|:)*(?\d+)*\]/g; /** * @typedef {{time: number, rawTime: string, content: string}} ParsedLyric @@ -62,7 +62,8 @@ function parseLyric(lrc) { for (const timestamp of lyricTimestamps.matchAll(extractTimestampRegex)) { const { min, sec, ms } = timestamp.groups; const rawTime = timestamp[0]; - const time = Number(min) * 60 + Number(sec) + 0.001 * Number(ms); + const time = + Number(min) * 60 + Number(sec) + Number(ms ?? 0) * 0.001; /** @type {ParsedLyric} */ const parsedLyric = { rawTime, time, content: trimContent(content) };