| 1 | import { parseTimeString } from './timestring' |
| 2 | |
| 3 | export interface TimesplitInput { |
| 4 | no: number |
| 5 | timesplit: string |
| 6 | title?: string |
| 7 | } |
| 8 | |
| 9 | export interface TimesplitOutput { |
| 10 | timestampStart: number |
| 11 | timestampEnd: number |
| 12 | noStart: number |
| 13 | noEnd: number |
| 14 | title?: string |
| 15 | } |
| 16 | |
| 17 | export function parseTimesplits(inputs: TimesplitInput[]): TimesplitOutput[] { |
| 18 | let ts = 0 |
| 19 | const outputs: TimesplitOutput[] = [] |
| 20 | let current: TimesplitOutput = { |
| 21 | timestampStart: ts, |
| 22 | timestampEnd: ts, |
| 23 | noStart: 0, |
| 24 | noEnd: 0, |
| 25 | title: '[start]', |
| 26 | } |
| 27 | outputs.push(current) |
| 28 | for (const input of inputs) { |
| 29 | const time = parseTimeString(input.timesplit) |
| 30 | const end = time.relative |
| 31 | ? ts + time.seconds |
| 32 | : time.seconds |
| 33 | if (end < ts) { |
| 34 | throw new Error(`Timesplit end ${end} is before start ${ts}`) |
| 35 | } |
| 36 | current.timestampEnd = end |
| 37 | current.noEnd = input.no |
| 38 | if (input.title) { |
| 39 | current.title = input.title |
| 40 | } |
| 41 | ts = end |
| 42 | current = { |
| 43 | timestampStart: end, |
| 44 | timestampEnd: end, |
| 45 | noStart: input.no, |
| 46 | noEnd: input.no, |
| 47 | } |
| 48 | outputs.push(current) |
| 49 | } |
| 50 | return outputs |
| 51 | } |
| 52 |