Skip to main content

max / makenotwork

1.0 KB · 24 lines History Blame Raw
1 // Pure upload-progress formatting (no DOM), extracted from static/upload.js.
2
3 /** Human-readable transfer speed, e.g. "2.4 MB/s" or "740 KB/s". */
4 export function formatSpeed(bytesPerSec: number): string {
5 return bytesPerSec > 1024 * 1024
6 ? (bytesPerSec / (1024 * 1024)).toFixed(1) + ' MB/s'
7 : (bytesPerSec / 1024).toFixed(0) + ' KB/s';
8 }
9
10 /**
11 * Human-readable ETA, e.g. "45s" or "2m 5s".
12 *
13 * One rounding, applied once, before the split into minutes and seconds. The
14 * previous spelling rounded twice, `ceil(sec / 60)` for the minutes and
15 * `ceil(sec % 60)` for the remainder, so 125 seconds read as "3m 5s": the
16 * minutes had already absorbed the remainder and then it was added again.
17 * Rounding first also keeps the remainder under 60, which floor-then-ceil
18 * would not: 119.5 seconds would have printed "1m 60s".
19 */
20 export function formatEta(remainingSec: number): string {
21 const whole = Math.ceil(remainingSec);
22 return whole < 60 ? whole + 's' : Math.floor(whole / 60) + 'm ' + (whole % 60) + 's';
23 }
24