-
Notifications
You must be signed in to change notification settings - Fork 14.4k
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
feat(number-format): Add duration formatter with colon notation #30593
feat(number-format): Add duration formatter with colon notation #30593
Conversation
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.
d0143f8
to
3e22d7b
Compare
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.
We don't need to create a new formatter for this, as the existing duration formatter already supports this functionality. Also, let's stick to milliseconds for now to keep this aligned with the existing duration formatters.
export default function createTimeDurationFormatter( | ||
config: { | ||
description?: string; | ||
id?: string; | ||
label?: string; | ||
multiplier?: number; | ||
} = {}, | ||
) { | ||
const { description, id, label } = config; | ||
|
||
return new NumberFormatter({ | ||
description, | ||
formatFunc: value => { | ||
const v = Math.abs(Math.round(value)); | ||
const h = Math.floor(v / 60); | ||
const m = v % 60; | ||
return `${h}:${String(m).padStart(2, '0')}`; | ||
}, | ||
id: id ?? 'duration_format', | ||
label: label ?? `Duration formatter`, | ||
}); | ||
} |
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.
We already have a dependency on pretty-ms that achieves this and (see colonNotation
option).
@@ -78,6 +79,7 @@ export default function setupFormatters( | |||
'DURATION_SUB', | |||
createDurationFormatter({ formatSubMilliseconds: true }), | |||
) | |||
.registerValue('TIME_DURATION', createTimeDurationFormatter()) |
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 check the README.md
in the number-format
directory for an example how to implement this. I believe this should do it:
.registerValue('TIME_DURATION', createTimeDurationFormatter()) | |
.registerValue('DURATION_COL', createTimeDurationFormatter({ colonNotation: true })) |
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.
Thank you. This does not the same, it will format it in yy:dd:hh:mm:ss and I would like to create a formatter for hh:mm. However, I can add both, DURATION_COL and DURATION_COL_SHORT. What are your thoughts?
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.
@gerbermichi as pretty-ms
already supports colon notation, has good test coverage, and lots of customization options, I suggest looking into how this could be implemented with that library. Did you try adding secondsDecimalDigits: 0
to remove the seconds? See the unit tests for some typical use cases: https://github.com/sindresorhus/pretty-ms/blob/d00183f8a040315005452f6f6dec30c581b258a2/test.js#L312-L380 If pretty-ms
still doesn't support what you're looking for, I suggest opening a PR on that repo, as that will also benefit the general OSS community.
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.
@villebro thank you for your feedback. I changed this PR so that it uses the colonNotation from ´pretty-ms´.
FYI: I opened a PR to bump |
Introduces a new formatter, DURATION_COL, for durations presented in colon notation (e.g., 0:10.5). This update enhances the flexibility of duration display formats within the application.
3e22d7b
to
8b7cf05
Compare
SUMMARY
Introduces a new formatter, DURATION_COL, for durations presented in colon notation (e.g., 0:10.5).
BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
TESTING INSTRUCTIONS
Duration in ms (10500 => 0:10.5)
ADDITIONAL INFORMATION