Skip to content

Add the documented b unit to the parse regex#70

Open
spokodev wants to merge 1 commit into
visionmedia:masterfrom
spokodev:fix/parse-b-unit-floor
Open

Add the documented b unit to the parse regex#70
spokodev wants to merge 1 commit into
visionmedia:masterfrom
spokodev:fix/parse-b-unit-floor

Conversation

@spokodev

Copy link
Copy Markdown

Problem

The README lists b (bytes) as the first supported, case-insensitive unit, but it is missing from the parse regex:

var parseRegExp = /^((-|\+)?(\d+(?:\.\d+)?)) *(kb|mb|gb|tb|pb)$/i;  // no `b`

So a b-suffixed value never reaches the unit path Math.floor(map[unit] * floatValue) and falls through to the parseInt(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:

bytes.parse('-1.9b')     // -1   (should be -2)
bytes.parse('-0.9b')     //  0   (should be -1)
bytes.parse('-256.77 b') // -256 (should be -257)

Every other unit already floors negatives (bytes.parse('-1.5kb') === -1536). And the existing test treats b as a real, floored unit:

// test/byte-parse.js — "Should drop partial bytes"
assert.equal(bytes.parse('1.1b'), 1);

That test passes today only because truncation and Math.floor agree for positives.

Fix

Add b to the regex alternation so it takes the same Math.floor path as kb/mb/gb/tb/pb (map.b already exists):

-var parseRegExp = /^((-|\+)?(\d+(?:\.\d+)?)) *(kb|mb|gb|tb|pb)$/i;
+var parseRegExp = /^((-|\+)?(\d+(?:\.\d+)?)) *(b|kb|mb|gb|tb|pb)$/i;

This is intentionally narrow and distinct from #60 (which proposed new single-letter units k/m/g/t/p): it touches only the already-documented b unit and accepts no new inputs — every b-suffixed string already matched via the parseInt fallback, so there is no breaking change in what parse accepts or rejects.

Verification

  • Added negative-fractional b assertions to the existing "Should drop partial bytes" test; they fail on master and pass with the fix.
  • Confirmed unchanged: positive b (1.1b → 1), all other units, no-unit parseInt leniency, and null rejection cases. A 2,000,000-input fuzz vs a reference parser shows only negative-fractional b/B values change (all toward the correct floored value); 0 new rejections.
  • Full mocha suite: 30/30.

`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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant