From 6217393d6a713aada02961e10e3824bb1ccfe035 Mon Sep 17 00:00:00 2001 From: Patrick Paysant Date: Wed, 7 Dec 2016 10:38:54 +0100 Subject: [PATCH 1/4] Adding computerFileSize in OC.Util Signed-off-by: Lukas Reschke --- core/js/js.js | 46 +++++++++++++++++++++++++++++++++ core/js/tests/specs/coreSpec.js | 20 ++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/core/js/js.js b/core/js/js.js index 972f0e63144..2b14ded87d7 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -1625,6 +1625,51 @@ function humanFileSize(size, skipSmallSizes) { return relativeSize + ' ' + readableFormat; } +/** + * 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 @@ -1667,6 +1712,7 @@ function relative_modified_date(timestamp) { OC.Util = { // TODO: remove original functions from global namespace humanFileSize: humanFileSize, + computerFileSize: computerFileSize, /** * @param timestamp diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js index d1734a9f3d1..8ece9344b5b 100644 --- a/core/js/tests/specs/coreSpec.js +++ b/core/js/tests/specs/coreSpec.js @@ -590,6 +590,26 @@ describe('Core base tests', function() { } }); }); + describe('computerFileSize', function() { + it('correctly parses file sizes from a human readable formated string', function() { + var data = [ + ['0 B', 0], + ['125 B', 125], + ['125b', 125], + ['125 KB', 128000], + ['125kb', 128000], + ['122.1 MB', 128031130, ], + ['122.1mb', 128031130, ], + ['119.2 GB', 127990025421], + ['119.2gb', 127990025421], + ['116.4 TB', 127983153473126], + ['116.4tb', 127983153473126] + ]; + for (var i = 0; i < data.length; i++) { + expect(OC.Util.computerFileSize(data[i][0])).toEqual(data[i][1]); + } + }); + }); describe('stripTime', function() { it('strips time from dates', function() { expect(OC.Util.stripTime(new Date(2014, 2, 24, 15, 4, 45, 24))) From ff018d48cfda1ff0454dc49d48dd0b7cf98e13cc Mon Sep 17 00:00:00 2001 From: Patrick Paysant Date: Wed, 7 Dec 2016 11:03:15 +0100 Subject: [PATCH 2/4] Implements all comments from @PVince81 Signed-off-by: Lukas Reschke --- core/js/js.js | 90 ++++++++++++++++----------------- 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 @@ -1625,51 +1625,6 @@ function humanFileSize(size, skipSmallSizes) { return relativeSize + ' ' + readableFormat; } -/** - * 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 @@ -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], From ec4bca619d7cd57271262928b1fe06d5f8dc3211 Mon Sep 17 00:00:00 2001 From: Patrick Paysant Date: Wed, 7 Dec 2016 11:05:07 +0100 Subject: [PATCH 3/4] Add test for unparseable string Signed-off-by: Lukas Reschke --- core/js/tests/specs/coreSpec.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js index 2960d1d7d23..39060f4e473 100644 --- a/core/js/tests/specs/coreSpec.js +++ b/core/js/tests/specs/coreSpec.js @@ -603,7 +603,8 @@ describe('Core base tests', function() { ['119.2 GB', 127990025421], ['119.2gb', 127990025421], ['116.4 TB', 127983153473126], - ['116.4tb', 127983153473126] + ['116.4tb', 127983153473126], + ['foobar', null] ]; for (var i = 0; i < data.length; i++) { expect(OC.Util.computerFileSize(data[i][0])).toEqual(data[i][1]); From d4c088cb796f22545f79379c27145ef6285d2d5e Mon Sep 17 00:00:00 2001 From: Patrick Paysant Date: Wed, 7 Dec 2016 11:43:44 +0100 Subject: [PATCH 4/4] Verify input, add more unit tests Signed-off-by: Lukas Reschke --- core/js/js.js | 26 ++++++++++++++------------ core/js/tests/specs/coreSpec.js | 13 +++++++++++-- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/core/js/js.js b/core/js/js.js index f2cdf7c93ef..3651635541a 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -1670,21 +1670,26 @@ OC.Util = { /** * Returns a file size in bytes from a humanly readable string + * Makes 2kB to 2048. + * Inspired by computerFileSize in helper.php * @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); + if (typeof string != 'string') { + return null; } - var bytes_array = { + var s = string.toLowerCase(); + var bytes = parseFloat(s) + + if (!isNaN(bytes) && isFinite(s)) { + return bytes; + } + + var bytesArray = { 'b' : 1, 'k' : 1024, 'kb': 1024, @@ -1698,13 +1703,10 @@ OC.Util = { '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 { + bytes = bytes * bytesArray[matches[1]]; + } else { return null; } diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js index 39060f4e473..370ebc6ba2d 100644 --- a/core/js/tests/specs/coreSpec.js +++ b/core/js/tests/specs/coreSpec.js @@ -593,6 +593,8 @@ describe('Core base tests', function() { describe('computerFileSize', function() { it('correctly parses file sizes from a human readable formated string', function() { var data = [ + ['125', 125], + ['125.25', 125.25], ['0 B', 0], ['125 B', 125], ['125b', 125], @@ -603,13 +605,20 @@ describe('Core base tests', function() { ['119.2 GB', 127990025421], ['119.2gb', 127990025421], ['116.4 TB', 127983153473126], - ['116.4tb', 127983153473126], - ['foobar', null] + ['116.4tb', 127983153473126] ]; for (var i = 0; i < data.length; i++) { expect(OC.Util.computerFileSize(data[i][0])).toEqual(data[i][1]); } }); + it('returns null if the parameter is not a string', function() { + expect(OC.Util.computerFileSize(NaN)).toEqual(null); + expect(OC.Util.computerFileSize(125)).toEqual(null); + }); + it('returns null if the string is unparsable', function() { + expect(OC.Util.computerFileSize('')).toEqual(null); + expect(OC.Util.computerFileSize('foobar')).toEqual(null); + }); }); describe('stripTime', function() { it('strips time from dates', function() {