Skip to content
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

@std/fmt/duration: ignoreZeros seems broken #6304

Open
sakgoyal opened this issue Dec 24, 2024 · 6 comments
Open

@std/fmt/duration: ignoreZeros seems broken #6304

sakgoyal opened this issue Dec 24, 2024 · 6 comments
Labels
bug Something isn't working needs triage

Comments

@sakgoyal
Copy link

Describe the bug
the ignoreZero flag seems to ignore leading zeros:

Steps to Reproduce

format(785000, { style: "digital", ignoreZero: true })
> "00:00:13:05" // expected: "13:05"

Expected behavior
expected: "13:05"

Environment

  • OS: Win10
  • deno version: 2.1.4
  • std version: 1.0.3
@sakgoyal sakgoyal added bug Something isn't working needs triage labels Dec 24, 2024
@GriffinJohnston
Copy link
Contributor

This is the expected behavior. For style: "digital" only zeros at the end are ignored. See https://jsr.io/@std/fmt/doc/duration/~/FormatOptions#property_ignorezero

@sakgoyal
Copy link
Author

I see. is there anything I can do to make it remove leading zeros too? perhaps a new option to do that?

@kt3k
Copy link
Member

kt3k commented Dec 25, 2024

I'm not sure that option makes sense. If the leading zeros removed, there's no difference between "00:00:13:05" and "00:13:05:00" (both become "13:05", and I think that would be confusing)

@sakgoyal
Copy link
Author

I see your point. But, wouldn't that be up to the developer to choose what is the right option for them to use? the library should allow the developer flexibility on what they want to do.

I think a good implementation would be changing the api so the settings change from
ignoreZeros: boolean to
ignoreZeros: "left" | "right" | "both"

@kt3k
Copy link
Member

kt3k commented Dec 27, 2024

I'm skeptical that adding option is worth it as string method of javascript is enough flexible to support this kind of situation. If you prefer to strip the leading zeros you can do:

format(785000, { style: "digital", ignoreZero: true }).replace(/^(00:)+/g, "")

@timreichen
Copy link
Contributor

I think the current duration api is not very flexible. Intl.DurationFormat.prototype.format() is more flexible and supports locale, but sadly is not implemented in deno. The proper way of handling this would probably be to adapt the current implementation to mirror Intl.DurationFormat.prototype.format() and also request that deno implements it natively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

No branches or pull requests

4 participants