Luxon: Feature Enhancement: Duration.toFormat accounts for 0 values

Created on 10 Feb 2020  路  3Comments  路  Source: moment/luxon

Would you be open to adding a new property to the Duration::toFormat opts object for skipping the formatting of values where the digit would be 0?

Say I have

const NINETY_SECONDS = Duration.fromObject({ seconds: 90 });
const FIFTEEN_SECONDS = Duration.fromObject({ seconds: 15 });

And I want to format so that I only see the minutes value if we have over 60 seconds:

NINETY_SECONDS.toFormat(" m 'mins' s 'secs'"); // 1 mins 30 secs
FIFTEEN_SECONDS.toFormat(" m 'mins' s 'secs'"); // 0 mins 15 secs

I'd prefer to have:

NINETY_SECONDS.toFormat(" m 'mins' s 'secs'", {ignoreZeros: true}); // 1 mins 30 secs
FIFTEEN_SECONDS.toFormat(" m 'mins' s 'secs'", {ignoreZeros: true}); // 15 secs

Most helpful comment

That sounds way too subtle to me. We'll end up with options to for skipping the next two, and so on. I think this should be a plugin or similar. Sorry guys, going to close this.

All 3 comments

I'm not sure how that would work. "mins" is a literal; how would Luxon know not to show it?

I think this would be a great addition @mandeldl!

@icambron I think there are a few ways that this can be done. A cursory look shows the string being composed in https://github.com/moment/luxon/blob/2251760c1c0d46d4116a7fe80655d6c98d177f65/src/impl/formatter.js#L5.
Could a check for token.val or tokenToString(token.val) (not sure which one without looking at val more closely) being equivalent to 0 be done in there and result in skipping the 0 value along with the immediately following token.literal ?

That sounds way too subtle to me. We'll end up with options to for skipping the next two, and so on. I think this should be a plugin or similar. Sorry guys, going to close this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

farnoy picture farnoy  路  3Comments

tamirvs picture tamirvs  路  5Comments

jonathanborges picture jonathanborges  路  6Comments

ericmorand picture ericmorand  路  7Comments

dnalborczyk picture dnalborczyk  路  3Comments