Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/lib/format.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import assert from 'node:assert/strict';
import test from 'node:test';

import { formatPace } from './format.ts';

test('carries rounded seconds into the next minute', () => {
assert.equal(formatPace(299.6, 1000), '5:00 /km');
});

test('zero-pads pace seconds', () => {
assert.equal(formatPace(305, 1000), '5:05 /km');
});

test('uses metric and imperial pace suffixes', () => {
assert.equal(formatPace(300, 1000, 'metric'), '5:00 /km');
assert.equal(formatPace(300, 1609.344, 'imperial'), '5:00 /mi');
});

test('keeps normal pace values unchanged', () => {
assert.equal(formatPace(272, 1000), '4:32 /km');
});
5 changes: 3 additions & 2 deletions src/lib/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export function formatDuration(seconds: number): string {
export function formatPace(seconds: number, meters: number, unit: DistanceUnit = 'metric'): string {
if (meters <= 0) return '--';
const perUnit = unit === 'imperial' ? (seconds / (meters / 1000 / KM_PER_MILE)) : seconds / (meters / 1000);
const m = Math.floor(perUnit / 60);
const s = Math.round(perUnit % 60);
const rounded = Math.round(perUnit);
const m = Math.floor(rounded / 60);
const s = rounded % 60;
return `${m}:${String(s).padStart(2, '0')} /${unit === 'imperial' ? 'mi' : 'km'}`;
}