This repository was archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 265
feat: add duration formatter #209
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
20 changes: 20 additions & 0 deletions
20
packages/superset-ui-number-format/src/factories/createDurationFormatter.ts
This file contains hidden or 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,20 @@ | ||
| import prettyMsFormatter from 'pretty-ms'; | ||
| import NumberFormatter from '../NumberFormatter'; | ||
|
|
||
| export default function createDurationFormatter( | ||
| config: { | ||
| description?: string; | ||
| id?: string; | ||
| label?: string; | ||
| multiplier?: number; | ||
| } & prettyMsFormatter.Options = {}, | ||
| ) { | ||
| const { description, id, label, multiplier = 1, ...prettyMsOptions } = config; | ||
|
|
||
| return new NumberFormatter({ | ||
| description, | ||
| formatFunc: value => prettyMsFormatter(value * multiplier, prettyMsOptions), | ||
| id: id || 'duration_format', | ||
| label: label || `Duration formatter`, | ||
| }); | ||
| } | ||
This file contains hidden or 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
32 changes: 32 additions & 0 deletions
32
packages/superset-ui-number-format/test/factories/createDurationFormatter.test.ts
This file contains hidden or 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,32 @@ | ||
| import NumberFormatter from '../../src/NumberFormatter'; | ||
| import createDurationFormatter from '../../src/factories/createDurationFormatter'; | ||
|
|
||
| describe('createDurationFormatter()', () => { | ||
| it('creates an instance of NumberFormatter', () => { | ||
| const formatter = createDurationFormatter(); | ||
| expect(formatter).toBeInstanceOf(NumberFormatter); | ||
| }); | ||
| it('format milliseconds in human readable format with default options', () => { | ||
| const formatter = createDurationFormatter(); | ||
| expect(formatter(0)).toBe('0ms'); | ||
| expect(formatter(1000)).toBe('1s'); | ||
| expect(formatter(1337)).toBe('1.3s'); | ||
| expect(formatter(10500)).toBe('10.5s'); | ||
| expect(formatter(60 * 1000)).toBe('1m'); | ||
| expect(formatter(90 * 1000)).toBe('1m 30s'); | ||
| }); | ||
| it('format seconds in human readable format with default options', () => { | ||
| const formatter = createDurationFormatter({ multiplier: 1000 }); | ||
| expect(formatter(0.5)).toBe('500ms'); | ||
| expect(formatter(1)).toBe('1s'); | ||
| expect(formatter(30)).toBe('30s'); | ||
| expect(formatter(60)).toBe('1m'); | ||
| expect(formatter(90)).toBe('1m 30s'); | ||
| }); | ||
| it('format milliseconds in human readable format with additional pretty-ms options', () => { | ||
| const zeroDecimalFormatter = createDurationFormatter({ secondsDecimalDigits: 0 }); | ||
| expect(zeroDecimalFormatter(10500)).toBe('11s'); | ||
| const subMillisecondFormatter = createDurationFormatter({ formatSubMilliseconds: true }); | ||
| expect(subMillisecondFormatter(100.40008)).toBe('100ms 400µs 80ns'); | ||
| }); | ||
| }); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.