Repository navigation
Fix format() printing 1024 in the auto-selected unit - #74
Closed
afonsojanu wants to merge 1 commit into
Closed
afonsojanu wants to merge 1 commit into
afonsojanu wants to merge 1 commit into
Conversation
When decimalPlaces rounding pushes a value that was just under a unit
boundary up to 1024, format() kept the smaller unit instead of rolling
over, e.g. bytes(1048575.995, { decimalPlaces: 2 }) printed '1024.00KB'
instead of '1MB'. Only the auto-selected unit is corrected; an
explicitly requested unit is left as the caller asked for it.
Author
|
Closing this one too, same reason: starting fresh and clearing out everything I had open. |
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.
format()auto-picks a unit for a byte count by comparing the raw value against each threshold (>= 1024for KB,>= 1024**2for MB, and so on), then divides and rounds todecimalPlaces. That rounding step can push a value that was just under a threshold back up to (or past) 1024 in the unit that already got picked:1024.00KBand1MBare the same amount, but printing it in the unit that was supposed to be "too small for this" defeats the whole point of the auto-selection, and reads as if it just failed to notice it crossed the boundary.The fix re-checks after rounding: if the formatted magnitude still reads
>= 1024and there's a bigger unit available, it moves up and reformats against that unit instead (looping in case one bump isn't enough, though in practice one is). This only applies when the unit was auto-selected — passing an explicitunitoption is a deliberate choice and is left alone, sobytes.format(1048575.995, { decimalPlaces: 2, unit: 'KB' })still prints1024KB.Added a test ("Should never round the displayed value up to the next unit's threshold") covering the KB/MB/GB/TB/PB boundaries, negative values, and the explicit-unit case. Confirmed it fails on the unpatched
index.jsand passes with the fix;npm test(mocha) andnpm run lint(eslint) both still pass with no new warnings.