diff options
author | Patrick Paysant <patrick.paysant@linagora.com> | 2016-12-07 11:03:15 +0100 |
---|---|---|
committer | Lukas Reschke <lukas@statuscode.ch> | 2016-12-19 17:29:05 +0100 |
commit | ff018d48cfda1ff0454dc49d48dd0b7cf98e13cc (patch) | |
tree | 1f521ba6eefb475e69cec2194d1194974b02ad36 /core | |
parent | 6217393d6a713aada02961e10e3824bb1ccfe035 (diff) | |
download | nextcloud-server-ff018d48cfda1ff0454dc49d48dd0b7cf98e13cc.tar.gz nextcloud-server-ff018d48cfda1ff0454dc49d48dd0b7cf98e13cc.zip |
Implements all comments from @PVince81
Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
Diffstat (limited to 'core')
-rw-r--r-- | core/js/js.js | 90 | ||||
-rw-r--r-- | core/js/tests/specs/coreSpec.js | 4 |
2 files changed, 46 insertions, 48 deletions
diff --git a/core/js/js.js b/core/js/js.js index 2b14ded87d7..f2cdf7c93ef 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -1626,51 +1626,6 @@ function humanFileSize(size, skipSmallSizes) { } /** - * Returns a file size in bytes from a humanly readable string - * @param {string} string file size in human readable format - * @return {number} - * - * Makes 2kB to 2048. - * - * Inspired by computerFileSize in helper.php - */ -function computerFileSize(string) { - var s = string.toLowerCase(); - - if (!isNaN(parseFloat(s)) && isFinite(s)) { - return parseFloat(s); - } - - var bytes_array = { - 'b' : 1, - 'k' : 1024, - 'kb': 1024, - 'mb': 1024 * 1024, - 'm' : 1024 * 1024, - 'gb': 1024 * 1024 * 1024, - 'g' : 1024 * 1024 * 1024, - 'tb': 1024 * 1024 * 1024 * 1024, - 't' : 1024 * 1024 * 1024 * 1024, - 'pb': 1024 * 1024 * 1024 * 1024 * 1024, - 'p' : 1024 * 1024 * 1024 * 1024 * 1024 - }; - - var bytes = parseFloat(s); - - var matches = s.match(/([kmgtp]?b?)$/i); - if (matches[1]) { - bytes = bytes * bytes_array[matches[1]]; - } - else { - return false; - } - - bytes = Math.round(bytes); - console.log(bytes); - return bytes; -} - -/** * Format an UNIX timestamp to a human understandable format * @param {number} timestamp UNIX timestamp * @return {string} Human readable format @@ -1712,7 +1667,50 @@ function relative_modified_date(timestamp) { OC.Util = { // TODO: remove original functions from global namespace humanFileSize: humanFileSize, - computerFileSize: computerFileSize, + + /** + * Returns a file size in bytes from a humanly readable string + * @param {string} string file size in human readable format + * @return {number} or null if string could not be parsed + * + * Makes 2kB to 2048. + * + * Inspired by computerFileSize in helper.php + */ + computerFileSize: function (string) { + var s = string.toLowerCase(); + + if (!isNaN(parseFloat(s)) && isFinite(s)) { + return parseFloat(s); + } + + var bytes_array = { + 'b' : 1, + 'k' : 1024, + 'kb': 1024, + 'mb': 1024 * 1024, + 'm' : 1024 * 1024, + 'gb': 1024 * 1024 * 1024, + 'g' : 1024 * 1024 * 1024, + 'tb': 1024 * 1024 * 1024 * 1024, + 't' : 1024 * 1024 * 1024 * 1024, + 'pb': 1024 * 1024 * 1024 * 1024 * 1024, + 'p' : 1024 * 1024 * 1024 * 1024 * 1024 + }; + + var bytes = parseFloat(s); + + var matches = s.match(/([kmgtp]?b?)$/i); + if (matches[1]) { + bytes = bytes * bytes_array[matches[1]]; + } + else { + return null; + } + + bytes = Math.round(bytes); + return bytes; + }, /** * @param timestamp diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js index 8ece9344b5b..2960d1d7d23 100644 --- a/core/js/tests/specs/coreSpec.js +++ b/core/js/tests/specs/coreSpec.js @@ -598,8 +598,8 @@ describe('Core base tests', function() { ['125b', 125], ['125 KB', 128000], ['125kb', 128000], - ['122.1 MB', 128031130, ], - ['122.1mb', 128031130, ], + ['122.1 MB', 128031130], + ['122.1mb', 128031130], ['119.2 GB', 127990025421], ['119.2gb', 127990025421], ['116.4 TB', 127983153473126], |