-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8283643
commit 448b464
Showing
3 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/utils/data-formatters/__tests__/format-duration.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { type Duration } from '@/__generated__/proto-ts/google/protobuf/Duration'; | ||
|
||
import formatDuration from '../format-duration'; | ||
|
||
describe('formatDuration', () => { | ||
it('should return "0s" for null duration', () => { | ||
expect(formatDuration(null)).toBe('0s'); | ||
}); | ||
|
||
it('should format duration with only seconds', () => { | ||
const duration: Duration = { seconds: '3600', nanos: 0 }; | ||
expect(formatDuration(duration)).toBe('1h'); | ||
}); | ||
|
||
it('should format duration with only nanoseconds', () => { | ||
const duration: Duration = { seconds: '0', nanos: 500000000 }; | ||
expect(formatDuration(duration)).toBe('500ms'); | ||
}); | ||
|
||
it('should format duration with large values', () => { | ||
const duration: Duration = { seconds: '31556952', nanos: 0 }; | ||
expect(formatDuration(duration)).toBe('1y, 5h, 49m, 12s'); | ||
}); | ||
|
||
it('should format duration with sub milliseconds', () => { | ||
const duration: Duration = { seconds: '61', nanos: 123456789 }; | ||
expect(formatDuration(duration)).toBe('1m, 1s, 123.456789ms'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { type Duration } from '@/__generated__/proto-ts/google/protobuf/Duration'; | ||
import dayjs from '@/utils/datetime/dayjs'; | ||
|
||
const formatDuration = (duration: Duration | null) => { | ||
const defaultReturn = '0s'; | ||
if (!duration) { | ||
return defaultReturn; | ||
} | ||
const secondsAsMillis = duration.seconds | ||
? parseInt(duration.seconds) * 1000 | ||
: 0; | ||
const nanosAsMillis = duration.nanos ? duration.nanos / 1000000 : 0; | ||
const intMillis = Math.floor(nanosAsMillis); | ||
const remainingNanosAsMillis = nanosAsMillis % 1; | ||
const milliseconds = secondsAsMillis + intMillis; | ||
const units = ['y', 'M', 'd', 'h', 'm', 's', 'ms'] as const; | ||
const values: Partial<Record<(typeof units)[number], number>> = {}; | ||
let d = dayjs.duration(milliseconds); | ||
units.forEach((unit) => { | ||
const value = d.as(unit); | ||
const intValue = Math.floor(value); | ||
if (unit === 'ms') { | ||
values[unit] = intValue + remainingNanosAsMillis; | ||
} else { | ||
values[unit] = intValue; | ||
} | ||
d = d.subtract(intValue, unit); | ||
}); | ||
|
||
const result = units | ||
.filter((unit) => values[unit]) | ||
.map((unit) => `${values[unit]}${unit}`) | ||
.join(', '); | ||
|
||
return result || defaultReturn; | ||
}; | ||
|
||
export default formatDuration; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters