File tree Expand file tree Collapse file tree 1 file changed +7
-4
lines changed Expand file tree Collapse file tree 1 file changed +7
-4
lines changed Original file line number Diff line number Diff line change 5
5
*/
6
6
const lyricFormat = ( lrc ) => {
7
7
// 匹配时间轴和歌词文本的正则表达式
8
- const regex = / ^ \[ ( . * ? ) \] \s * ( .+ ?) \s * $ / ;
8
+ const regex = / ^ \[ ( [ ^ \] ] + ) \] \s * ( .+ ?) \s * $ / ;
9
9
// 将歌词字符串按行分割为数组
10
10
const lines = lrc . split ( "\n" ) ;
11
11
// 对每一行进行转换
@@ -15,9 +15,11 @@ const lyricFormat = (lrc) => {
15
15
// 转换时间轴和歌词文本为对象
16
16
. map ( ( line ) => {
17
17
const [ , time , text ] = line . match ( regex ) ;
18
- const seconds = time
19
- . split ( ":" )
20
- . reduce ( ( acc , cur ) => acc * 60 + parseFloat ( cur ) , 0 ) ;
18
+ const parts = time . split ( ":" ) ;
19
+ let seconds = parseInt ( parts [ 0 ] ) * 60 + parseFloat ( parts [ 1 ] ) ;
20
+ if ( parts . length > 2 ) {
21
+ seconds += parseFloat ( parts [ 2 ] ) / 1000 ;
22
+ }
21
23
return { time : seconds , lyric : text . trim ( ) } ;
22
24
} ) . filter ( ( element ) => element . lyric . trim ( ) !== "" ) ;
23
25
// 检查是否为纯音乐,是则返回空数组
@@ -28,4 +30,5 @@ const lyricFormat = (lrc) => {
28
30
return result ;
29
31
} ;
30
32
33
+
31
34
export default lyricFormat ;
You can’t perform that action at this time.
0 commit comments