Skip to content

Commit 0418af8

Browse files
committed
Simplify some functions
1 parent 5942d80 commit 0418af8

2 files changed

Lines changed: 39 additions & 20 deletions

File tree

.prettierrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"semi": true,
33
"singleQuote": true,
44
"trailingComma": "all",
5-
"printWidth": 100,
5+
"printWidth": 80,
66
"tabWidth": 4
77
}

src/vtt.ts

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ export class VTT {
9090
identifier?: string | number,
9191
settings?: CueSettings,
9292
) {
93-
const cue = new Cue(startTime, endTime, text, identifier, settings);
94-
this.addSegment(cue);
95-
return this;
93+
return this.addSegment(
94+
new Cue(startTime, endTime, text, identifier, settings),
95+
);
9696
}
9797
/**
9898
* Adds a new region to the VTT instance.
@@ -115,9 +115,9 @@ export class VTT {
115115
viewportAnchor?: RegionAnchor,
116116
scroll?: 'up',
117117
) {
118-
const region = new Region(id, width, lines, regionAnchor, viewportAnchor, scroll);
119-
this.addSegment(region);
120-
return this;
118+
return this.addSegment(
119+
new Region(id, width, lines, regionAnchor, viewportAnchor, scroll),
120+
);
121121
}
122122
/**
123123
* Adds a new style segment to the VTT instance.
@@ -132,9 +132,7 @@ export class VTT {
132132
selectors: string[],
133133
declarations: Partial<Pick<CSSStyleDeclaration, CueCSSProperty>>,
134134
) {
135-
const style = new Style([{ selectors, declarations }]);
136-
this.addSegment(style);
137-
return this;
135+
return this.addSegment(new Style([{ selectors, declarations }]));
138136
}
139137
/**
140138
* Adds a new comment segment to the VTT instance.
@@ -145,9 +143,7 @@ export class VTT {
145143
* @returns The VTT instance with the new comment segment added, allowing for method chaining.
146144
*/
147145
addComment(text: string) {
148-
const comment = new Comment(text);
149-
this.addSegment(comment);
150-
return this;
146+
return this.addSegment(new Comment(text));
151147
}
152148
/**
153149
* Shifts all cue timings by a specified offset in seconds. Positive values will delay cues, while negative values will advance them.
@@ -263,7 +259,9 @@ export class VTT {
263259
* @returns An array of Cue instances representing the cues that are active within the specified time range.
264260
*/
265261
getCuesByTime(start: number, end: number) {
266-
return this.getCues().filter((cue) => cue.startTime < end && cue.endTime > start);
262+
return this.getCues().filter(
263+
(cue) => cue.startTime < end && cue.endTime > start,
264+
);
267265
}
268266
/**
269267
* Retrieves a cue segment by its identifier from the VTT instance.
@@ -307,7 +305,12 @@ export class VTT {
307305
*
308306
* @returns The TextTrack instance that was created and populated with cues from the VTT instance.
309307
*/
310-
attachToVideo(video: HTMLVideoElement, kind: TextTrackKind, label?: string, language?: string) {
308+
attachToVideo(
309+
video: HTMLVideoElement,
310+
kind: TextTrackKind,
311+
label?: string,
312+
language?: string,
313+
) {
311314
const track = video.addTextTrack(kind, label, language);
312315

313316
for (const cue of this.getCues()) {
@@ -369,7 +372,10 @@ export class VTT {
369372
const headerSegment = segments.shift()!;
370373

371374
if (!isHeader(headerSegment)) {
372-
throw new InvalidVttError('Invalid VTT file: Header is malformed', headerSegment);
375+
throw new InvalidVttError(
376+
'Invalid VTT file: Header is malformed',
377+
headerSegment,
378+
);
373379
}
374380

375381
const header = Header.fromString(headerSegment);
@@ -393,7 +399,10 @@ export class VTT {
393399
}
394400
}
395401
if (!vtt.validate()) {
396-
throw new InvalidVttError('Invalid VTT file: One or more segments are malformed', str);
402+
throw new InvalidVttError(
403+
'Invalid VTT file: One or more segments are malformed',
404+
str,
405+
);
397406
}
398407

399408
return vtt;
@@ -463,7 +472,9 @@ export class VTT {
463472
return VTT.fromString(text);
464473
})
465474
.catch((err) => {
466-
throw new Error(`Failed to load VTT file from URL: ${err.message}`);
475+
throw new Error(
476+
`Failed to load VTT file from URL: ${err.message}`,
477+
);
467478
});
468479
}
469480
/**
@@ -489,12 +500,17 @@ export class VTT {
489500
),
490501
);
491502
if (timingLineIndex === -1) {
492-
throw new InvalidVttError('Invalid SRT segment: Missing timing line', segment);
503+
throw new InvalidVttError(
504+
'Invalid SRT segment: Missing timing line',
505+
segment,
506+
);
493507
}
494508
const timingLine = lines[timingLineIndex];
495509
const identifier = lines.slice(0, timingLineIndex).join('\n');
496510
const textLines = lines.slice(timingLineIndex + 1);
497-
const [start, end] = timingLine.split('-->').map((t) => t.trim().replace(',', '.'));
511+
const [start, end] = timingLine
512+
.split('-->')
513+
.map((t) => t.trim().replace(',', '.'));
498514
const parts = identifier
499515
? [identifier, `${start} --> ${end}`, textLines.join('\n')]
500516
: [`${start} --> ${end}`, textLines.join('\n')];
@@ -515,14 +531,17 @@ export class VTT {
515531
static merge(...vtts: VTT[]): VTT {
516532
const [first, ...rest] = vtts;
517533
const merged = new VTT(first.header.description, first.header.meta);
534+
518535
for (const segment of first.segments.slice(1)) {
519536
merged.addSegment(segment);
520537
}
538+
521539
for (const vtt of rest) {
522540
for (const segment of vtt.segments.slice(1)) {
523541
merged.addSegment(segment);
524542
}
525543
}
544+
526545
return merged;
527546
}
528547
/**

0 commit comments

Comments
 (0)