fix(drafting): keep the group separator off the minus sign - #175
fix(drafting): keep the group separator off the minus sign#175rajanpanth 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>
dselman
left a comment
There was a problem hiding this comment.
Good work. Well spotted bug fix.
|
CI triage on the red Unit Tests (22.x) leg: the failure is That test also has a timeout-tuning history ( |
|
Ping on this one, since it has been approved and is only held up by CI. The single red leg is A re-run should clear it, but I cannot trigger one from a fork. Happy to push an empty commit if you would rather have it re-run that way. |
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.