Repository navigation
Fix trailing zero stripping when a leading fraction digit is zero - #73
Closed
afonsojanu wants to merge 1 commit into
Closed
afonsojanu wants to merge 1 commit into
afonsojanu wants to merge 1 commit into
Conversation
format() drops trailing zeros from the fixed-point string unless
fixedDecimals is set, but the old regex only handled that when the
first digit after the decimal point was non-zero. A value like
1075 bytes formatted with decimalPlaces: 3 produces toFixed output
"1.050", and neither alternative in the old pattern matched it, so
the trailing zero was left in place ("1.050KB" instead of "1.05KB").
Replaced it with a simpler pattern that just trims trailing zeros
after the decimal point, followed by a second pass that drops a
now-bare trailing dot (covers the all-zero-fraction case, e.g. "1.000"
-> "1"). Added a test covering the reported case plus the existing
all-zero case.
Author
|
Closing this for now as part of a clean slate — stepping back from everything I had open across these repos. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #69.
format() is supposed to trim trailing zeros from the fixed-point output unless
fixedDecimalsis set, but the regex doing that (/(?:\.0*|(\.[^0]+)0+)$/) only covers two shapes: an all-zero fraction, or a fraction whose very first digit is non-zero. It misses anything in between, such as1075bytes withdecimalPlaces: 3, whosetoFixed(3)output is"1.050". Neither branch of the old regex matches that string (the first requires every fraction digit to be zero, the second requires the digit right after the dot to be non-zero), so the extra zero survives and you get"1.050KB"instead of"1.05KB".Swapped it for a plain "trim trailing zeros after the dot" pattern plus a second pass to drop a now-bare trailing dot (handles the all-zero case like
1.000->1). Ran the existing suite plus a small fuzz check across random values and decimalPlaces 0-5 and didn't find any case left with a stray trailing zero or dangling dot.Added a test in
test/byte-format.jscovering the reported input along with the pre-existing all-zero-fraction case to make sure that one still collapses correctly.