Repository navigation
Adding computerFileSize in OC.Util - #26789
Conversation
|
|
||
| /** | ||
| * Returns a file size in bytes from a humanly readable string | ||
| * @param {string} string file size in human readable format |
There was a problem hiding this comment.
returns the parsed number or null if the string could not be parsed
| if (matches[1]) { | ||
| bytes = bytes * bytes_array[matches[1]]; | ||
| } | ||
| else { |
| return false; | ||
| } | ||
|
|
||
| bytes = Math.round(bytes); |
| OC.Util = { | ||
| // TODO: remove original functions from global namespace | ||
| humanFileSize: humanFileSize, | ||
| computerFileSize: computerFileSize, |
There was a problem hiding this comment.
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.
| ['125b', 125], | ||
| ['125 KB', 128000], | ||
| ['125kb', 128000], | ||
| ['122.1 MB', 128031130, ], |
| }); | ||
| }); | ||
| describe('computerFileSize', function() { | ||
| it('correctly parses file sizes from a human readable formated string', function() { |
There was a problem hiding this comment.
add a test for failed parse and check that it returns null
|
Looks good so far, and now I realize I missed an opportunity to properly use Github's "Review" feature 😕 |
PVince81
left a comment
There was a problem hiding this comment.
Second level review in sharp mode, stuff I missed the first time.
| if (!isNaN(parseFloat(s)) && isFinite(s)) { | ||
| return parseFloat(s); | ||
| } | ||
|
|
|
|
||
| var matches = s.match(/([kmgtp]?b?)$/i); | ||
| if (matches[1]) { | ||
| bytes = bytes * bytes_array[matches[1]]; |
| * @return {number} or null if string could not be parsed | ||
| * | ||
| * Makes 2kB to 2048. | ||
| * |
There was a problem hiding this comment.
Move these comments in the header, under Returns a file size...
| 'pb': 1024 * 1024 * 1024 * 1024 * 1024, | ||
| 'p' : 1024 * 1024 * 1024 * 1024 * 1024 | ||
| }; | ||
|
|
There was a problem hiding this comment.
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
| ['119.2 GB', 127990025421], | ||
| ['119.2gb', 127990025421], | ||
| ['116.4 TB', 127983153473126], | ||
| ['116.4tb', 127983153473126], |
There was a problem hiding this comment.
also add a case to cover the !isNan() + isFinite() case.
And add a test for pure numbers, without unit.
|
|
||
| var s = string.toLowerCase(); | ||
| var bytes = parseFloat(s) | ||
|
|
There was a problem hiding this comment.
I guess isFinite does not work with bytes directly ? Just asking, am not familiar with that method
|
Looks good 👍 |
|
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. |
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
Checklist: