diff --git a/src/lib/format.test.mjs b/src/lib/format.test.mjs new file mode 100644 index 0000000..fe95b5c --- /dev/null +++ b/src/lib/format.test.mjs @@ -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'); +}); diff --git a/src/lib/format.ts b/src/lib/format.ts index c52f2f0..8e035d0 100644 --- a/src/lib/format.ts +++ b/src/lib/format.ts @@ -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'}`; }