|
1 | | -import { Lrc } from './lrc' |
| 1 | +import { Lrc } from './lrc'; |
2 | 2 |
|
3 | 3 | export class Runner { |
4 | 4 | offset: boolean; |
5 | 5 | _currentIndex: number; |
6 | | - lrc: Lrc; |
| 6 | + lrc!: Lrc; |
7 | 7 |
|
8 | 8 | constructor(lrc: Lrc = new Lrc(), offset: boolean = true) { |
9 | | - this.offset = offset |
10 | | - this._currentIndex = -1 |
11 | | - this.setLrc(lrc) |
| 9 | + this.offset = offset; |
| 10 | + this._currentIndex = -1; |
| 11 | + this.setLrc(lrc); |
12 | 12 | } |
13 | 13 |
|
14 | 14 | setLrc(lrc: Lrc) { |
15 | | - this.lrc = lrc.clone() |
16 | | - this.lrcUpdate() |
| 15 | + this.lrc = lrc.clone(); |
| 16 | + this.lrcUpdate(); |
17 | 17 | } |
18 | 18 |
|
19 | 19 | lrcUpdate() { |
20 | 20 | if (this.offset) { |
21 | | - this._offsetAlign() |
| 21 | + this._offsetAlign(); |
22 | 22 | } |
23 | | - this._sort() |
| 23 | + this._sort(); |
24 | 24 | } |
25 | 25 |
|
26 | 26 | _offsetAlign() { |
27 | 27 | if ('offset' in this.lrc.info) { |
28 | | - const offset = parseInt(this.lrc.info.offset) / 1000 |
29 | | - if (! isNaN(offset)) { |
30 | | - this.lrc.offset(offset) |
31 | | - delete this.lrc.info.offset |
| 28 | + const offset = parseInt(this.lrc.info.offset) / 1000; |
| 29 | + if (!isNaN(offset)) { |
| 30 | + this.lrc.offset(offset); |
| 31 | + delete this.lrc.info.offset; |
32 | 32 | } |
33 | 33 | } |
34 | 34 | } |
35 | 35 |
|
36 | 36 | _sort() { |
37 | | - this.lrc.lyrics.sort((a, b) => a.timestamp - b.timestamp) |
| 37 | + this.lrc.lyrics.sort((a, b) => a.timestamp - b.timestamp); |
38 | 38 | } |
39 | 39 |
|
40 | 40 | timeUpdate(timestamp: number) { |
41 | 41 | if (this._currentIndex >= this.lrc.lyrics.length) { |
42 | | - this._currentIndex = this.lrc.lyrics.length - 1 |
| 42 | + this._currentIndex = this.lrc.lyrics.length - 1; |
43 | 43 | } else if (this._currentIndex < -1) { |
44 | | - this._currentIndex = -1 |
| 44 | + this._currentIndex = -1; |
45 | 45 | } |
46 | | - this._currentIndex = this._findIndex(timestamp, this._currentIndex) |
| 46 | + this._currentIndex = this._findIndex(timestamp, this._currentIndex); |
47 | 47 | } |
48 | 48 |
|
49 | | - _findIndex(timestamp: number, startIndex: number) { |
50 | | - const curFrontTimestamp = startIndex == -1 ? |
51 | | - Number.NEGATIVE_INFINITY : this.lrc.lyrics[startIndex].timestamp |
| 49 | + _findIndex(timestamp: number, startIndex: number): number { |
| 50 | + const curFrontTimestamp = |
| 51 | + startIndex == -1 |
| 52 | + ? Number.NEGATIVE_INFINITY |
| 53 | + : this.lrc.lyrics[startIndex].timestamp; |
52 | 54 |
|
53 | | - const curBackTimestamp = (startIndex == this.lrc.lyrics.length - 1) ? |
54 | | - Number.POSITIVE_INFINITY : this.lrc.lyrics[startIndex+1].timestamp |
| 55 | + const curBackTimestamp = |
| 56 | + startIndex == this.lrc.lyrics.length - 1 |
| 57 | + ? Number.POSITIVE_INFINITY |
| 58 | + : this.lrc.lyrics[startIndex + 1].timestamp; |
55 | 59 |
|
56 | 60 | if (timestamp < curFrontTimestamp) { |
57 | | - return this._findIndex(timestamp, startIndex-1) |
| 61 | + return this._findIndex(timestamp, startIndex - 1); |
58 | 62 | } else if (timestamp === curBackTimestamp) { |
59 | 63 | if (curBackTimestamp === Number.POSITIVE_INFINITY) { |
60 | | - return startIndex |
| 64 | + return startIndex; |
61 | 65 | } else { |
62 | | - return startIndex+1 |
| 66 | + return startIndex + 1; |
63 | 67 | } |
64 | 68 | } else if (timestamp > curBackTimestamp) { |
65 | | - return this._findIndex(timestamp, startIndex+1) |
| 69 | + return this._findIndex(timestamp, startIndex + 1); |
66 | 70 | } else { |
67 | | - return startIndex |
| 71 | + return startIndex; |
68 | 72 | } |
69 | 73 | } |
70 | 74 |
|
71 | 75 | getInfo() { |
72 | | - return this.lrc.info |
| 76 | + return this.lrc.info; |
73 | 77 | } |
74 | 78 |
|
75 | 79 | getLyrics() { |
76 | | - return this.lrc.lyrics |
| 80 | + return this.lrc.lyrics; |
77 | 81 | } |
78 | 82 |
|
79 | 83 | getLyric(index: number = this.curIndex()) { |
80 | 84 | if (index >= 0 && index <= this.lrc.lyrics.length - 1) { |
81 | | - return this.lrc.lyrics[index] |
| 85 | + return this.lrc.lyrics[index]; |
82 | 86 | } else { |
83 | | - throw new Error('Index not exist') |
| 87 | + throw new Error('Index not exist'); |
84 | 88 | } |
85 | 89 | } |
86 | 90 |
|
87 | 91 | curIndex() { |
88 | | - return this._currentIndex |
| 92 | + return this._currentIndex; |
89 | 93 | } |
90 | 94 |
|
91 | 95 | curLyric() { |
92 | | - return this.getLyric() |
| 96 | + return this.getLyric(); |
93 | 97 | } |
94 | 98 | } |
0 commit comments