forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add duration formatter (apache#209)
* feat: add duration formatter * fix: address review comments
- Loading branch information
1 parent
13ceb8d
commit dc9d0a0
Showing
5 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
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
20 changes: 20 additions & 0 deletions
20
...i/superset-ui/packages/superset-ui-number-format/src/factories/createDurationFormatter.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,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 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
...rset-ui/packages/superset-ui-number-format/test/factories/createDurationFormatter.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,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 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