30 lines
674 B
TypeScript
30 lines
674 B
TypeScript
|
/**
|
||
|
* Both `start` and `end` are in seconds.
|
||
|
*/
|
||
|
export class TimeFragment {
|
||
|
start: number | null;
|
||
|
end: number | null;
|
||
|
|
||
|
constructor(start: number, end: number) {
|
||
|
this.start = start;
|
||
|
this.end = end;
|
||
|
}
|
||
|
|
||
|
public static parse(fragment: string): TimeFragment {
|
||
|
if (!fragment.startsWith("t=")) {
|
||
|
return undefined;
|
||
|
}
|
||
|
const data = fragment.substring("t=".length);
|
||
|
try {
|
||
|
const [start, end] = data.split(",").map((str) => parseFloat(str));
|
||
|
return new TimeFragment(start || null, end || null);
|
||
|
} catch {
|
||
|
return undefined;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public toString(): string {
|
||
|
return `t=${this.start || ""},${this.end || ""}`;
|
||
|
}
|
||
|
}
|