Skip to content

Commit 373b727

Browse files
committed
support esm
1 parent 5badf76 commit 373b727

File tree

9 files changed

+133
-137
lines changed

9 files changed

+133
-137
lines changed

.prettierrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"semi": true,
33
"singleQuote": true,
4-
"trailingComma": "all"
4+
"trailingComma": "all",
5+
"arrowParens": "always"
56
}

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
"name": "lrc-kit",
33
"version": "1.0.0",
44
"description": "lrc parser, maker, runner",
5-
"main": "lib/lrc-kit.js",
5+
"main": "lib/cjs/lrc-kit.js",
6+
"module": "lib/esm/lrc-kit.js",
67
"scripts": {
7-
"build": "tsc",
8+
"build": "yarn run build:esm && yarn run build:cjs",
9+
"build:esm": "tsc --module es2015 --target es5 --outDir lib/esm",
10+
"build:cjs": "tsc --module commonjs --target es5 --outDir lib/cjs",
811
"clean": "rm -rf lib",
912
"test": "jest",
1013
"prepare": "yarn clean && yarn build"

src/line-parser.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const TIME_REGEXP = /^\s*(\d+)\s*:\s*(\d+(\s*[\.:]\s*\d+)?)\s*$/;
88
export enum LineType {
99
INVALID = 'INVALID',
1010
INFO = 'INFO',
11-
TIME = 'TIME'
11+
TIME = 'TIME',
1212
}
1313

1414
export interface InvalidLine {
@@ -40,18 +40,18 @@ export function parseTags(line: string): null | [string[], string] {
4040

4141
export function parseTime(tags: string[], content: string): TimeLine {
4242
const timestamps: number[] = [];
43-
tags.forEach(tag => {
43+
tags.forEach((tag) => {
4444
const matches = TIME_REGEXP.exec(tag)!;
4545
const minutes = parseFloat(matches[1]);
4646
const seconds = parseFloat(
47-
matches[2].replace(/\s+/g, '').replace(':', '.')
47+
matches[2].replace(/\s+/g, '').replace(':', '.'),
4848
);
4949
timestamps.push(minutes * 60 + seconds);
5050
});
5151
return {
5252
type: LineType.TIME,
5353
timestamps,
54-
content: content.trim()
54+
content: content.trim(),
5555
};
5656
}
5757

@@ -60,7 +60,7 @@ export function parseInfo(tag: string): InfoLine {
6060
return {
6161
type: LineType.INFO,
6262
key: matches[1].trim(),
63-
value: matches[2].trim()
63+
value: matches[2].trim(),
6464
};
6565
}
6666

@@ -89,11 +89,11 @@ export function parseLine(line: string): InfoLine | TimeLine | InvalidLine {
8989
}
9090
}
9191
return {
92-
type: LineType.INVALID
92+
type: LineType.INVALID,
9393
};
9494
} catch (_e) {
9595
return {
96-
type: LineType.INVALID
96+
type: LineType.INVALID,
9797
};
9898
}
9999
}

src/lrc-kit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from './lrc'
2-
export * from './runner'
1+
export * from './lrc';
2+
export * from './runner';

src/lrc.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function padZero(num: number | string, size: number = 2): string {
2626
*/
2727
export function timestampToString(timestamp: number): string {
2828
return `${padZero(Math.floor(timestamp / 60))}:${padZero(
29-
(timestamp % 60).toFixed(2)
29+
(timestamp % 60).toFixed(2),
3030
)}`;
3131
}
3232

@@ -50,19 +50,19 @@ export class Lrc {
5050
const info: Info = {};
5151
text
5252
.split(/\r\n|[\n\r]/g)
53-
.map(line => {
53+
.map((line) => {
5454
return parseLine(line);
5555
})
56-
.forEach(line => {
56+
.forEach((line) => {
5757
switch (line.type) {
5858
case LineType.INFO:
5959
info[line.key] = line.value;
6060
break;
6161
case LineType.TIME:
62-
line.timestamps.forEach(timestamp => {
62+
line.timestamps.forEach((timestamp) => {
6363
lyrics.push({
6464
timestamp: timestamp,
65-
content: line.content
65+
content: line.content,
6666
});
6767
});
6868
break;
@@ -77,7 +77,7 @@ export class Lrc {
7777
}
7878

7979
offset(offsetTime: number) {
80-
this.lyrics.forEach(lyric => {
80+
this.lyrics.forEach((lyric) => {
8181
lyric.timestamp += offsetTime;
8282
if (lyric.timestamp < 0) {
8383
lyric.timestamp = 0;
@@ -100,7 +100,7 @@ export class Lrc {
100100
ret.push(clonePlainObject(lyric));
101101
return ret;
102102
},
103-
[] as Lyric[]
103+
[] as Lyric[],
104104
);
105105
return lrc;
106106
}
@@ -127,7 +127,7 @@ export class Lrc {
127127

128128
if (opts.combine) {
129129
// uniqueness
130-
this.lyrics.forEach(lyric => {
130+
this.lyrics.forEach((lyric) => {
131131
if (lyric.content in lyricsMap) {
132132
lyricsMap[lyric.content].push(lyric.timestamp);
133133
} else {
@@ -142,7 +142,7 @@ export class Lrc {
142142
}
143143
lyricsList.push({
144144
timestamps: lyricsMap[content],
145-
content: content
145+
content: content,
146146
});
147147
}
148148

@@ -151,17 +151,17 @@ export class Lrc {
151151
}
152152

153153
// generate lyrics
154-
lyricsList.forEach(lyric => {
154+
lyricsList.forEach((lyric) => {
155155
lines.push(
156156
`[${lyric.timestamps
157-
.map(timestamp => timestampToString(timestamp))
158-
.join('][')}]${lyric.content || ''}`
157+
.map((timestamp) => timestampToString(timestamp))
158+
.join('][')}]${lyric.content || ''}`,
159159
);
160160
});
161161
} else {
162-
this.lyrics.forEach(lyric => {
162+
this.lyrics.forEach((lyric) => {
163163
lines.push(
164-
`[${timestampToString(lyric.timestamp)}]${lyric.content || ''}`
164+
`[${timestampToString(lyric.timestamp)}]${lyric.content || ''}`,
165165
);
166166
});
167167
}

src/runner.ts

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,98 @@
1-
import { Lrc } from './lrc'
1+
import { Lrc } from './lrc';
22

33
export class Runner {
44
offset: boolean;
55
_currentIndex: number;
6-
lrc: Lrc;
6+
lrc!: Lrc;
77

88
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);
1212
}
1313

1414
setLrc(lrc: Lrc) {
15-
this.lrc = lrc.clone()
16-
this.lrcUpdate()
15+
this.lrc = lrc.clone();
16+
this.lrcUpdate();
1717
}
1818

1919
lrcUpdate() {
2020
if (this.offset) {
21-
this._offsetAlign()
21+
this._offsetAlign();
2222
}
23-
this._sort()
23+
this._sort();
2424
}
2525

2626
_offsetAlign() {
2727
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;
3232
}
3333
}
3434
}
3535

3636
_sort() {
37-
this.lrc.lyrics.sort((a, b) => a.timestamp - b.timestamp)
37+
this.lrc.lyrics.sort((a, b) => a.timestamp - b.timestamp);
3838
}
3939

4040
timeUpdate(timestamp: number) {
4141
if (this._currentIndex >= this.lrc.lyrics.length) {
42-
this._currentIndex = this.lrc.lyrics.length - 1
42+
this._currentIndex = this.lrc.lyrics.length - 1;
4343
} else if (this._currentIndex < -1) {
44-
this._currentIndex = -1
44+
this._currentIndex = -1;
4545
}
46-
this._currentIndex = this._findIndex(timestamp, this._currentIndex)
46+
this._currentIndex = this._findIndex(timestamp, this._currentIndex);
4747
}
4848

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;
5254

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;
5559

5660
if (timestamp < curFrontTimestamp) {
57-
return this._findIndex(timestamp, startIndex-1)
61+
return this._findIndex(timestamp, startIndex - 1);
5862
} else if (timestamp === curBackTimestamp) {
5963
if (curBackTimestamp === Number.POSITIVE_INFINITY) {
60-
return startIndex
64+
return startIndex;
6165
} else {
62-
return startIndex+1
66+
return startIndex + 1;
6367
}
6468
} else if (timestamp > curBackTimestamp) {
65-
return this._findIndex(timestamp, startIndex+1)
69+
return this._findIndex(timestamp, startIndex + 1);
6670
} else {
67-
return startIndex
71+
return startIndex;
6872
}
6973
}
7074

7175
getInfo() {
72-
return this.lrc.info
76+
return this.lrc.info;
7377
}
7478

7579
getLyrics() {
76-
return this.lrc.lyrics
80+
return this.lrc.lyrics;
7781
}
7882

7983
getLyric(index: number = this.curIndex()) {
8084
if (index >= 0 && index <= this.lrc.lyrics.length - 1) {
81-
return this.lrc.lyrics[index]
85+
return this.lrc.lyrics[index];
8286
} else {
83-
throw new Error('Index not exist')
87+
throw new Error('Index not exist');
8488
}
8589
}
8690

8791
curIndex() {
88-
return this._currentIndex
92+
return this._currentIndex;
8993
}
9094

9195
curLyric() {
92-
return this.getLyric()
96+
return this.getLyric();
9397
}
9498
}

0 commit comments

Comments
 (0)