-
Notifications
You must be signed in to change notification settings - Fork 272
feat: add duration formatter #209
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { RegistryWithDefaultKey, OverwritePolicy } from '@superset-ui/core'; | ||
import createD3NumberFormatter from './factories/createD3NumberFormatter'; | ||
import createDurationFormatter from './factories/createDurationFormatter'; | ||
import createSmartNumberFormatter from './factories/createSmartNumberFormatter'; | ||
import NumberFormats from './NumberFormats'; | ||
import NumberFormatter from './NumberFormatter'; | ||
|
@@ -14,6 +15,9 @@ export default class NumberFormatterRegistry extends RegistryWithDefaultKey< | |
overwritePolicy: OverwritePolicy.WARN, | ||
}); | ||
|
||
this.registerValue(NumberFormats.DURATION, createDurationFormatter()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert the changes to this file. The duration formatter should be optional and not registered by default. There are other apps using this package and some do not need duration formatter. You can register these formatters in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can add a section in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok that makes sense. I wasn't able to find There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I made a typo. It is |
||
this.registerValue(NumberFormats.DURATION_MS, createDurationFormatter({ multiplier: 1 })); | ||
this.registerValue(NumberFormats.DURATION_S, createDurationFormatter({ multiplier: 1000 })); | ||
this.registerValue(NumberFormats.SMART_NUMBER, createSmartNumberFormatter()); | ||
this.registerValue( | ||
NumberFormats.SMART_NUMBER_SIGNED, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import prettyMsFormatter from 'pretty-ms'; | ||
import NumberFormatter from '../NumberFormatter'; | ||
import NumberFormats from '../NumberFormats'; | ||
|
||
export default function createDurationFormatter( | ||
config: { | ||
villebro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
description?: string; | ||
id?: string; | ||
label?: string; | ||
multiplier?: number; | ||
} = {}, | ||
) { | ||
const { description, id, label, multiplier = 1 } = config; | ||
|
||
return new NumberFormatter({ | ||
description, | ||
formatFunc: value => prettyMsFormatter(value * multiplier), | ||
id: id || NumberFormats.DURATION, | ||
label: label || `Duration formatter`, | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import NumberFormatter from '../../src/NumberFormatter'; | ||
import createDurationFormatter from '../../src/factories/createDurationFormatter'; | ||
|
||
describe('createDurationFormatter()', () => { | ||
it('creates an instance of NumberFormatter', () => { | ||
const formatter = createDurationFormatter(); | ||
const formatterMs = createDurationFormatter({ multiplier: 1 }); | ||
const formatterS = createDurationFormatter({ multiplier: 1000 }); | ||
expect(formatter).toBeInstanceOf(NumberFormatter); | ||
expect(formatterMs).toBeInstanceOf(NumberFormatter); | ||
expect(formatterS).toBeInstanceOf(NumberFormatter); | ||
}); | ||
it('format milliseconds in human readable format', () => { | ||
const formatter = createDurationFormatter(); | ||
expect(formatter(0)).toBe('0ms'); | ||
expect(formatter(1000)).toBe('1s'); | ||
expect(formatter(1337)).toBe('1.3s'); | ||
expect(formatter(60 * 1000)).toBe('1m'); | ||
expect(formatter(90 * 1000)).toBe('1m 30s'); | ||
}); | ||
it('format seconds in human readable format', () => { | ||
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'); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert the changes to this file. The duration formatter should be optional and not registered by default. There are other apps using this package and some do not need duration formatter. You can register these formatters in
incubator-superset
'ssetupFormatter.js
. That way other apps can take advantage of tree-shaking and won't have to bundlepretty-ms
.