Add the documented b unit to the parse regex#70
Open
spokodev wants to merge 1 commit into
Open
Conversation
`b` is documented as a supported, case-insensitive unit, but it is
missing from the parse regex, so `b`-suffixed values fall through to the
`parseInt` fallback (which truncates toward zero) instead of the unit
path (which floors). The two diverge for negative fractional values:
bytes.parse('-1.9b') // -1 (should be -2, like every other unit)
The existing "Should drop partial bytes" test (parse('1.1b') === 1)
already treats `b` as a real floored unit; it passed only because
truncation and floor agree for positives. Adding `b` to the regex routes
it through the same Math.floor path as kb/mb/gb/tb/pb. No new inputs are
accepted, since every `b`-suffixed string already matched via the
fallback.
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
The README lists
b(bytes) as the first supported, case-insensitive unit, but it is missing from the parse regex:So a
b-suffixed value never reaches the unit pathMath.floor(map[unit] * floatValue)and falls through to theparseInt(val, 10)fallback, which truncates toward zero instead of flooring. For positives the two agree, so it's masked; for negative fractional values they diverge:Every other unit already floors negatives (
bytes.parse('-1.5kb') === -1536). And the existing test treatsbas a real, floored unit:That test passes today only because truncation and
Math.flooragree for positives.Fix
Add
bto the regex alternation so it takes the sameMath.floorpath askb/mb/gb/tb/pb(map.balready exists):This is intentionally narrow and distinct from #60 (which proposed new single-letter units
k/m/g/t/p): it touches only the already-documentedbunit and accepts no new inputs — everyb-suffixed string already matched via theparseIntfallback, so there is no breaking change in whatparseaccepts or rejects.Verification
bassertions to the existing "Should drop partial bytes" test; they fail onmasterand pass with the fix.b(1.1b→ 1), all other units, no-unitparseIntleniency, andnullrejection cases. A 2,000,000-input fuzz vs a reference parser shows only negative-fractionalb/Bvalues change (all toward the correct floored value); 0 new rejections.