All files / src/lib subtitle-timing.ts

100% Statements 11/11
100% Branches 8/8
100% Functions 3/3
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23            5x 496x             5x 89x 89x 111x 67x   89x    
// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 Max Boksem. See NOTICE for additional terms under GPLv3 section 7.
 
import type { SubtitleCue } from "./types";
 
/** The cue to show `time` seconds into the subtitles, or undefined for none. */
export const cueAt = (cues: SubtitleCue[], time: number): SubtitleCue | undefined =>
	cues.find((cue) => time >= cue.start && time <= cue.end);
 
/**
 * Seconds from `time` to the next start or end of a cue, or null when none
 * comes after it. A cue shows up to and including its end, so the end of a
 * cue that's showing counts, even when `time` is on it.
 */
export const nextCueChange = (cues: SubtitleCue[], time: number): number | null => {
	let next = Infinity;
	for (const cue of cues) {
		if (cue.start > time) next = Math.min(next, cue.start);
		else if (cue.end >= time) next = Math.min(next, cue.end);
	}
	return next === Infinity ? null : next - time;
};