Skip to content
Open
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
15 changes: 15 additions & 0 deletions packages/cli/src/ui/utils/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ describe('formatters', () => {
);
});

it('should round milliseconds before picking the unit', () => {
expect(formatDuration(999.4)).toBe('999ms');
expect(formatDuration(999.5)).toBe('1.0s');
});

it('should round seconds before picking the unit', () => {
expect(formatDuration(59949)).toBe('59.9s');
expect(formatDuration(59950)).toBe('1m');
expect(formatDuration(59999)).toBe('1m');
});

it('should round up to the hour', () => {
expect(formatDuration(3599999)).toBe('1h');
});

it('should handle negative durations', () => {
expect(formatDuration(-100)).toBe('0s');
});
Expand Down
17 changes: 10 additions & 7 deletions packages/cli/src/ui/utils/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ export const formatDuration = (milliseconds: number): string => {
return '0s';
}

if (milliseconds < 1000) {
return `${Math.round(milliseconds)}ms`;
const roundedMs = Math.round(milliseconds);
if (roundedMs < 1000) {
return `${roundedMs}ms`;
}

const totalSeconds = milliseconds / 1000;

if (totalSeconds < 60) {
return `${totalSeconds.toFixed(1)}s`;
// Round to the displayed precision before picking a unit so a value just
// under a boundary does not round up into the next unit's range.
const tenthsOfSecond = Math.round(milliseconds / 100);
if (tenthsOfSecond < 600) {
return `${(tenthsOfSecond / 10).toFixed(1)}s`;
}

const totalSeconds = Math.round(milliseconds / 1000);
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = Math.floor(totalSeconds % 60);
const seconds = totalSeconds % 60;

const parts: string[] = [];

Expand Down
Loading