Skip to content

Conversation

@individual-it
Copy link
Member

Description

I've added some more tests for computerFileSize and they would fail, so fixed
the function to be more picky on what input to accept and try to parse and also fixed the rounding of values without units

Motivation and Context

@PVince81 mentioned this function in the context of fixing #25358 but it does not work correct

  1. input of "B" would return "NaN" and not null
  2. invalid values like "40/0" or "40,30 kb" would be parsed
  3. negative values would be parsed and returned as negative values
  4. values without unit would not be rounded to full bytes. 125.5 would return 125.5, but I don't think oC runs on 4bit systems :-)

That are exact the problems we have in #25358
After this PR is accepted I'm happy to look into fixing #25358

How Has This Been Tested?

wrote more tests

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@mention-bot
Copy link

@individual-it, thanks for your PR! By analyzing the history of the files in this pull request, we identified @PVince81, @felixheidecke and @butonic to be potential reviewers.

@individual-it individual-it changed the title Individual it patch computer file size fix computerFileSize to parse correctly, prerapation to fix #25358 Feb 27, 2017
['116.4 TB', 127983153473126],
['116.4tb', 127983153473126]
['116.4tb', 127983153473126],
['8776656778888777655.4tb', 9.650036181387265e+30],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we're already ready for the next century's storage requirements 😆

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

living in the foothills of the Himalayas I know about huge clouds, so better be prepared, who knows what the climate change will bring ;-)

core/js/js.js Outdated
var matches = s.match(/([kmgtp]?b?)$/i);
if (matches[1]) {
bytes = bytes * bytesArray[matches[1]];
var matches = s.match(/^[\s+]?([0-9]*)(\.([0-9]+))?( +)?([kmgtp]?b?)$/i);
Copy link
Contributor

@PVince81 PVince81 Feb 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to pre-compile the regexp and store it in a hidden variable this._computerFileSizeRegexp or so.
I'm not sure if modern Javascript engines recompile the Regexp every time or whether it already caches it somewhere.

To be safe, store it somewhere so if this function is called in a loop it will run a bit faster.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed it. please check it

@PVince81 PVince81 added this to the 10.0 milestone Feb 27, 2017
@PVince81
Copy link
Contributor

Looks better 👍

@individual-it
Copy link
Member Author

thought that a user might enter values with white spaces, and that should be possible and valid

@PVince81
Copy link
Contributor

Oh, screw Jenkins...

I ran the JS tests locally and they pass.

@PVince81 PVince81 merged commit d699837 into master Feb 27, 2017
@PVince81 PVince81 deleted the individual-it-patch-computerFileSize branch February 27, 2017 16:03
IljaN pushed a commit that referenced this pull request Mar 1, 2017
…27257)

* fix computerFileSize to be mory picky

* more tests for computerFileSize

* codestyle fix

* fix double initialization

* pre-compile and store regexp in a hidden variable

as suggested by @PVince81 in Review #27257 (comment)

* test strings with whitespaces

* trim string before parsing
@lock
Copy link

lock bot commented Aug 3, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Aug 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants