Skip to content

Commit

Permalink
Timer value (#2313)
Browse files Browse the repository at this point in the history
* Dont show milliseconds for durations over a minute, dont show millisecond decimals if duration over a second

* Add more one test, remove console.log
  • Loading branch information
Alex-Tideman committed Sep 6, 2024
1 parent 8f10db3 commit 6f842c2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/lib/models/event-groups/get-group-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ export const getEventGroupName = (event: CommonHistoryEvent): string => {
if (isTimerStartedEvent(event)) {
return `${event.timerStartedEventAttributes
?.timerId} (${formatDurationAbbreviated(
event.timerStartedEventAttributes
?.startToFireTimeout as unknown as Duration,
event.timerStartedEventAttributes?.startToFireTimeout,
)})`;
}

Expand Down
24 changes: 24 additions & 0 deletions src/lib/utilities/format-time.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, expect, it } from 'vitest';
import {
formatDistance,
formatDistanceAbbreviated,
formatDurationAbbreviated,
fromSecondsToDaysOrHours,
fromSecondsToMinutesAndSeconds,
getDuration,
Expand Down Expand Up @@ -297,4 +298,27 @@ describe('getTimestampDifference', () => {
const end = '2022-04-13T16:29:35.633009Z';
expect(getTimestampDifference(start, end)).toBe(50316513);
});

describe('formatDurationAbbreviated', () => {
it('should return duration abbreviated with full milliseconds if under one second', () => {
expect(formatDurationAbbreviated('0.86920383s')).toBe('869.20383ms');
});
it('should return duration abbreviated with rounded milliseconds if under one minute', () => {
expect(formatDurationAbbreviated('13.439023207s')).toBe('13s, 439ms');
});
it('should return duration abbreviated with no milliseconds if over one minute', () => {
expect(formatDurationAbbreviated('64.2134111s')).toBe('1m, 4s');
});
it('should return duration abbreviated', () => {
expect(formatDurationAbbreviated('2652361s')).toBe('30d, 16h, 46m, 1s');
});
it('should return duration abbreviated', () => {
expect(formatDurationAbbreviated('2694361s')).toBe('1month, 4h, 26m, 1s');
});
it('should return duration abbreviated', () => {
expect(formatDurationAbbreviated('32694361s')).toBe(
'1year, 13d, 9h, 46m, 1s',
);
});
});
});
15 changes: 9 additions & 6 deletions src/lib/utilities/to-duration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ describe('fromSeconds', () => {
it('should correctly format milliseconds', () => {
expect(fromSeconds('0.001s')).toEqual('1 millisecond');
expect(fromSeconds('0.006s')).toEqual('6 milliseconds');
expect(fromSeconds('0.6s')).toEqual('600 milliseconds');
expect(fromSeconds('0.06s')).toEqual('60 milliseconds');
expect(fromSeconds('0.0006s')).toEqual('0.6 milliseconds');
expect(fromSeconds('0.00006s')).toEqual('0.06 milliseconds');
Expand All @@ -358,14 +359,17 @@ describe('fromSeconds', () => {

it('should correctly format seconds', () => {
expect(fromSeconds('0s')).toEqual('');
expect(fromSeconds('0.00s')).toEqual('');
expect(fromSeconds('1s')).toEqual('1 second');
expect(fromSeconds('6s')).toEqual('6 seconds');
expect(fromSeconds('6.00s')).toEqual('6 seconds');
expect(fromSeconds('0.00s')).toEqual('');
expect(fromSeconds('6.412382134s')).toEqual('6 seconds, 412 milliseconds');
expect(fromSeconds('59.32322s')).toEqual('59 seconds, 323 milliseconds');
});

it('should correctly format minutes', () => {
expect(fromSeconds('60s')).toEqual('1 minute');
expect(fromSeconds('61.1234123412s')).toEqual('1 minute, 1 second');
});

it('should correctly format hours', () => {
Expand All @@ -383,11 +387,10 @@ describe('fromSeconds', () => {

it('should correctly format hours, minutes, seconds and milliseconds', () => {
expect(fromSeconds('3661s')).toEqual('1 hour, 1 minute, 1 second');
expect(fromSeconds('3661.06s')).toEqual(
'1 hour, 1 minute, 1 second, 60 milliseconds',
);
expect(fromSeconds('3661.006s')).toEqual(
'1 hour, 1 minute, 1 second, 6 milliseconds',
expect(fromSeconds('3661.06s')).toEqual('1 hour, 1 minute, 1 second');
expect(fromSeconds('3661.006s')).toEqual('1 hour, 1 minute, 1 second');
expect(fromSeconds('2148128.1234123412s')).toEqual(
'24 days, 20 hours, 42 minutes, 8 seconds',
);
});

Expand Down
12 changes: 9 additions & 3 deletions src/lib/utilities/to-duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,16 @@ export const fromSeconds = (
Math.round(parsedDecimal * 1000 * 1000000000) / 1000000000; // round to nanoseconds

if (!milliseconds) return durationString;
const msString = `${milliseconds} ${pluralize('millisecond', milliseconds)}`;
if (!durationString)
return `${milliseconds} ${pluralize('millisecond', milliseconds)}`;
if (parsedSeconds < 60) {
return `${durationString}, ${milliseconds.toFixed(0)} ${pluralize(
'millisecond',
milliseconds,
)}`;
}

if (!durationString) return msString;
return `${durationString}, ${msString}`;
return `${durationString}`;
};

export const isValidDurationQuery = (value: string): boolean => {
Expand Down

0 comments on commit 6f842c2

Please sign in to comment.