Skip to content

Adding computerFileSize in OC.Util - #26789

Merged
PVince81 merged 4 commits into
owncloud:masterfrom
ppaysant:add-js-computerFileSize
Dec 7, 2016
Merged

PVince81 merged 4 commits into
owncloud:masterfrom
ppaysant:add-js-computerFileSize

Conversation

@ppaysant

@ppaysant ppaysant commented Dec 7, 2016

Copy link
Copy Markdown
Contributor

Description

Add a computerFileSize function in js OC.Util.

Related Issue

#26788

Motivation and Context

This function may help. For instance, I'm calling the setquota.php in settings/ajax/ from one of my app and the quota size returned is in human readable format. I need the size in bytes.

How Has This Been Tested?

Unit tested in coreSpec.js

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
Copy Markdown

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

@PVince81 PVince81 added this to the 9.2 milestone Dec 7, 2016
Comment thread core/js/js.js Outdated

/**
* Returns a file size in bytes from a humanly readable string
* @param {string} string file size in human readable format

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

returns the parsed number or null if the string could not be parsed

Comment thread core/js/js.js Outdated
if (matches[1]) {
bytes = bytes * bytes_array[matches[1]];
}
else {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd prefer null here

Comment thread core/js/js.js Outdated
return false;
}

bytes = Math.round(bytes);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

remove logging

Comment thread core/js/js.js Outdated
OC.Util = {
// TODO: remove original functions from global namespace
humanFileSize: humanFileSize,
computerFileSize: computerFileSize,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please add your function directly here.

The reason humanFileSize is separate is because it used to be global in the past, but global is bad... so we added a shortcut here.

However computerFileSize never existed before, so we have a chance to have it directly in the namespace and no global.

Comment thread core/js/tests/specs/coreSpec.js Outdated
['125b', 125],
['125 KB', 128000],
['125kb', 128000],
['122.1 MB', 128031130, ],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

trailing comma in array ?

});
});
describe('computerFileSize', function() {
it('correctly parses file sizes from a human readable formated string', function() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

add a test for failed parse and check that it returns null

@PVince81

PVince81 commented Dec 7, 2016

Copy link
Copy Markdown
Contributor

Looks good so far, and now I realize I missed an opportunity to properly use Github's "Review" feature 😕

@PVince81 PVince81 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Second level review in sharp mode, stuff I missed the first time.

Comment thread core/js/js.js
if (!isNaN(parseFloat(s)) && isFinite(s)) {
return parseFloat(s);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

use camel case bytesArray

Comment thread core/js/js.js Outdated

var matches = s.match(/([kmgtp]?b?)$/i);
if (matches[1]) {
bytes = bytes * bytes_array[matches[1]];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

} else { looks better

Comment thread core/js/js.js
* @return {number} or null if string could not be parsed
*
* Makes 2kB to 2048.
*

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Move these comments in the header, under Returns a file size...

Comment thread core/js/js.js
'pb': 1024 * 1024 * 1024 * 1024 * 1024,
'p' : 1024 * 1024 * 1024 * 1024 * 1024
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I noticed that you're calling parseFloat() three times in this method.
Would be better to just call it once at the top var bytes = parseFloat(s);, then do the isNan() tests, etc

Comment thread core/js/tests/specs/coreSpec.js Outdated
['119.2 GB', 127990025421],
['119.2gb', 127990025421],
['116.4 TB', 127983153473126],
['116.4tb', 127983153473126],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

also add a case to cover the !isNan() + isFinite() case.

And add a test for pure numbers, without unit.

Comment thread core/js/js.js

var s = string.toLowerCase();
var bytes = parseFloat(s)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I guess isFinite does not work with bytes directly ? Just asking, am not familiar with that method

@PVince81

PVince81 commented Dec 7, 2016

Copy link
Copy Markdown
Contributor

Looks good 👍

@lock

lock Bot commented Aug 4, 2019

Copy link
Copy Markdown

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 4, 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.

3 participants