Skip to content

Commit 6f842c2

Browse files
authored
Timer value (#2313)
* Dont show milliseconds for durations over a minute, dont show millisecond decimals if duration over a second * Add more one test, remove console.log
1 parent 8f10db3 commit 6f842c2

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

Diff for: src/lib/models/event-groups/get-group-name.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ export const getEventGroupName = (event: CommonHistoryEvent): string => {
2424
if (isTimerStartedEvent(event)) {
2525
return `${event.timerStartedEventAttributes
2626
?.timerId} (${formatDurationAbbreviated(
27-
event.timerStartedEventAttributes
28-
?.startToFireTimeout as unknown as Duration,
27+
event.timerStartedEventAttributes?.startToFireTimeout,
2928
)})`;
3029
}
3130

Diff for: src/lib/utilities/format-time.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { describe, expect, it } from 'vitest';
33
import {
44
formatDistance,
55
formatDistanceAbbreviated,
6+
formatDurationAbbreviated,
67
fromSecondsToDaysOrHours,
78
fromSecondsToMinutesAndSeconds,
89
getDuration,
@@ -297,4 +298,27 @@ describe('getTimestampDifference', () => {
297298
const end = '2022-04-13T16:29:35.633009Z';
298299
expect(getTimestampDifference(start, end)).toBe(50316513);
299300
});
301+
302+
describe('formatDurationAbbreviated', () => {
303+
it('should return duration abbreviated with full milliseconds if under one second', () => {
304+
expect(formatDurationAbbreviated('0.86920383s')).toBe('869.20383ms');
305+
});
306+
it('should return duration abbreviated with rounded milliseconds if under one minute', () => {
307+
expect(formatDurationAbbreviated('13.439023207s')).toBe('13s, 439ms');
308+
});
309+
it('should return duration abbreviated with no milliseconds if over one minute', () => {
310+
expect(formatDurationAbbreviated('64.2134111s')).toBe('1m, 4s');
311+
});
312+
it('should return duration abbreviated', () => {
313+
expect(formatDurationAbbreviated('2652361s')).toBe('30d, 16h, 46m, 1s');
314+
});
315+
it('should return duration abbreviated', () => {
316+
expect(formatDurationAbbreviated('2694361s')).toBe('1month, 4h, 26m, 1s');
317+
});
318+
it('should return duration abbreviated', () => {
319+
expect(formatDurationAbbreviated('32694361s')).toBe(
320+
'1year, 13d, 9h, 46m, 1s',
321+
);
322+
});
323+
});
300324
});

Diff for: src/lib/utilities/to-duration.test.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ describe('fromSeconds', () => {
349349
it('should correctly format milliseconds', () => {
350350
expect(fromSeconds('0.001s')).toEqual('1 millisecond');
351351
expect(fromSeconds('0.006s')).toEqual('6 milliseconds');
352+
expect(fromSeconds('0.6s')).toEqual('600 milliseconds');
352353
expect(fromSeconds('0.06s')).toEqual('60 milliseconds');
353354
expect(fromSeconds('0.0006s')).toEqual('0.6 milliseconds');
354355
expect(fromSeconds('0.00006s')).toEqual('0.06 milliseconds');
@@ -358,14 +359,17 @@ describe('fromSeconds', () => {
358359

359360
it('should correctly format seconds', () => {
360361
expect(fromSeconds('0s')).toEqual('');
362+
expect(fromSeconds('0.00s')).toEqual('');
361363
expect(fromSeconds('1s')).toEqual('1 second');
362364
expect(fromSeconds('6s')).toEqual('6 seconds');
363365
expect(fromSeconds('6.00s')).toEqual('6 seconds');
364-
expect(fromSeconds('0.00s')).toEqual('');
366+
expect(fromSeconds('6.412382134s')).toEqual('6 seconds, 412 milliseconds');
367+
expect(fromSeconds('59.32322s')).toEqual('59 seconds, 323 milliseconds');
365368
});
366369

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

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

384388
it('should correctly format hours, minutes, seconds and milliseconds', () => {
385389
expect(fromSeconds('3661s')).toEqual('1 hour, 1 minute, 1 second');
386-
expect(fromSeconds('3661.06s')).toEqual(
387-
'1 hour, 1 minute, 1 second, 60 milliseconds',
388-
);
389-
expect(fromSeconds('3661.006s')).toEqual(
390-
'1 hour, 1 minute, 1 second, 6 milliseconds',
390+
expect(fromSeconds('3661.06s')).toEqual('1 hour, 1 minute, 1 second');
391+
expect(fromSeconds('3661.006s')).toEqual('1 hour, 1 minute, 1 second');
392+
expect(fromSeconds('2148128.1234123412s')).toEqual(
393+
'24 days, 20 hours, 42 minutes, 8 seconds',
391394
);
392395
});
393396

Diff for: src/lib/utilities/to-duration.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,16 @@ export const fromSeconds = (
143143
Math.round(parsedDecimal * 1000 * 1000000000) / 1000000000; // round to nanoseconds
144144

145145
if (!milliseconds) return durationString;
146-
const msString = `${milliseconds} ${pluralize('millisecond', milliseconds)}`;
146+
if (!durationString)
147+
return `${milliseconds} ${pluralize('millisecond', milliseconds)}`;
148+
if (parsedSeconds < 60) {
149+
return `${durationString}, ${milliseconds.toFixed(0)} ${pluralize(
150+
'millisecond',
151+
milliseconds,
152+
)}`;
153+
}
147154

148-
if (!durationString) return msString;
149-
return `${durationString}, ${msString}`;
155+
return `${durationString}`;
150156
};
151157

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

0 commit comments

Comments
 (0)