All files / src/lib format.ts

100% Statements 13/13
100% Branches 11/11
100% Functions 4/4
100% Lines 13/13

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 24 25 26 27 28 29 30 31 32                19x 3405x 3405x 3405x 3405x 914x   2491x       19x 21x         19x 1711x     19x 18x  
// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 Max Boksem. See NOTICE for additional terms under GPLv3 section 7.
 
/**
 * Format seconds into a human-readable time string.
 * - Under 1 hour: "M:SS" (e.g. "3:07")
 * - 1 hour or more: "H:MM:SS" (e.g. "1:03:07")
 */
export const formatTime = (seconds: number): string => {
	const h = Math.floor(seconds / 3600);
	const m = Math.floor((seconds % 3600) / 60);
	const s = Math.floor(seconds % 60);
	if (h > 0) {
		return `${h}:${String(m).padStart(2, "0")}:${String(s).padStart(2, "0")}`;
	}
	return `${m}:${String(s).padStart(2, "0")}`;
};
 
/** A file size such as "350 MB" or "1.2 GB". */
export const formatBytes = (bytes: number): string =>
	bytes >= 1_000_000_000
		? `${(bytes / 1_000_000_000).toFixed(1)} GB`
		: `${Math.round(bytes / 1_000_000)} MB`;
 
/** A count and its noun, like "1 episode" or "1,200 episodes". */
export const formatCount = (count: number, noun: string, plural = `${noun}s`): string =>
	`${count.toLocaleString()} ${count === 1 ? noun : plural}`;
 
/** A skip as the player shows it, like "+10s" or "−30s". The minus is U+2212, not a hyphen. */
export const formatSkip = (seconds: number): string =>
	seconds === 0 ? "0s" : `${seconds > 0 ? "+" : "−"}${Math.abs(seconds)}s`;