fix(drafting): keep the group separator off the minus sign - #175
Open
rajanpanth wants to merge 1 commit into
Open
fix(drafting): keep the group separator off the minus sign#175rajanpanth wants to merge 1 commit into
rajanpanth wants to merge 1 commit into
Conversation
draftIntegerFormat and draftDoubleFormat group the integer part by
repeatedly taking its last three characters while more than three
remain. The minus sign is part of that string, so it counted as a digit
and a separator was inserted directly after it whenever the number of
digits was a multiple of three:
getDrafter('Integer')(-123, '0,0') // '-,123'
getDrafter('Integer')(-123456, '0,0') // '-,123,456'
getDrafter('Double')(-123456.78, '0,0.00')// '-,123,456.78'
Numbers such as -1234 were unaffected, because the sign plus four
digits left a two character remainder, which hid the bug for the
lengths people usually try.
The sign is now separated before grouping and prepended afterwards.
Long shares Integer's implementation, so it is fixed too.
Signed-off-by: rajanpanth <rajan.pantha@samriddhicollege.edu.np>
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.
Problem
Formatting a negative number with a grouped format puts the group separator immediately after the minus sign:
Cause
draftIntegerFormatanddraftDoubleFormatgroup the integer part by repeatedly peeling off its last three characters:vscomes fromvalue.toFixed(...), so for a negative number it starts with-. The loop treats that sign as if it were a digit. Once the digits have been consumed,iis left holding just"-", and sinceresalready begins with a separator the result is-,123.This only shows up when the digit count is a multiple of three. With
-1234the sign plus four digits leaves a two character remainder, so-1,234comes out correctly, which is why the common cases look fine.Fix
Separate the sign before grouping and put it back afterwards:
Applied to both
Double/format.tsandInteger/format.ts.LongimportsdraftIntegerFormat, so it is covered by the same change.After the fix:
Testing
Added cases to
test/DraftFormat.test.tscovering negative Integer, Long and Double values at the digit counts that trigger the bug, plus a check that positive grouping is unchanged.Reverting the two source files while keeping the new tests fails 3 of them, so they cover the change rather than restating current behaviour.
On the full suite,
test/JavaScriptEvaluator.test.ts,test/GenerateOptions.test.tsandtest/TemplateMarkInterpreter.test.tsfail for me, but they fail identically on a clean checkout ofmain, so they look unrelated to this change. Excluding those, the suite goes from 92 to 96 passing with the added tests and no new failures.