Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timer value #2313

Merged
merged 2 commits into from
Sep 6, 2024
Merged
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
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
Loading