]> source.dussan.org Git - nextcloud-server.git/commitdiff
display "<1 kB" for really small files
authorMorris Jobke <morris.jobke@gmail.com>
Mon, 2 Jun 2014 08:38:46 +0000 (10:38 +0200)
committerMorris Jobke <morris.jobke@gmail.com>
Mon, 2 Jun 2014 13:33:09 +0000 (15:33 +0200)
* added parameters for humanFileSize to trigger that behaviour
* add unit tests for that

apps/files/js/filelist.js
apps/files/tests/js/filelistSpec.js
core/js/js.js
core/js/tests/specs/coreSpec.js

index 1b2a62137e5ec6d3a17dd468d7040dc01edfb076..4229988b171a52e3ef4fee652c0325370a17a357 100644 (file)
 
                        // size column
                        if (typeof(fileData.size) !== 'undefined' && fileData.size >= 0) {
-                               simpleSize = humanFileSize(parseInt(fileData.size, 10));
+                               simpleSize = humanFileSize(parseInt(fileData.size, 10), true);
                                sizeColor = Math.round(160-Math.pow((fileData.size/(1024*1024)),2));
                        } else {
                                simpleSize = t('files', 'Pending');
index 855a5c9af5188aef27c6ff5c9246bc3602369416..7d3bc946dd3a6793375a0988b4ea001b4c86361b 100644 (file)
@@ -252,7 +252,7 @@ describe('OCA.Files.FileList tests', function() {
                                size: '0'
                        };
                        var $tr = fileList.add(fileData);
-                       expect($tr.find('.filesize').text()).toEqual('0 B');
+                       expect($tr.find('.filesize').text()).toEqual('0 kB');
                });
                it('adds new file to the end of the list', function() {
                        var $tr;
index cf35d8aac6a9af972807cf823904d51fc180759b..cbe466bf11c0e5de03efd675260651d35264aeda 100644 (file)
@@ -1163,9 +1163,10 @@ $.fn.filterAttr = function(attr_name, attr_value) {
 /**
  * Returns a human readable file size
  * @param {number} size Size in bytes
+ * @param {boolean} skipSmallSizes return '< 1 kB' for small files
  * @return {string}
  */
-function humanFileSize(size) {
+function humanFileSize(size, skipSmallSizes) {
        var humanList = ['B', 'kB', 'MB', 'GB', 'TB'];
        // Calculate Log with base 1024: size = 1024 ** order
        var order = size?Math.floor(Math.log(size) / Math.log(1024)):0;
@@ -1173,6 +1174,13 @@ function humanFileSize(size) {
        order = Math.min(humanList.length - 1, order);
        var readableFormat = humanList[order];
        var relativeSize = (size / Math.pow(1024, order)).toFixed(1);
+       if(skipSmallSizes === true && order === 0) {
+               if(relativeSize !== "0.0"){
+                       return '< 1 kB';
+               } else {
+                       return '0 kB';
+               }
+       }
        if(order < 2){
                relativeSize = parseFloat(relativeSize).toFixed(0);
        }
index 65f768fbc5173416387e57e0dfdc234e8632b02d..6b85d8be1660dab8c45bfca9fccaeffe517ab232 100644 (file)
@@ -489,6 +489,19 @@ describe('Core base tests', function() {
                                        expect(OC.Util.humanFileSize(data[i][0])).toEqual(data[i][1]);
                                }
                        });
+                       it('renders file sizes with the correct unit for small sizes', function() {
+                               var data = [
+                                       [0, '0 kB'],
+                                       [125, '< 1 kB'],
+                                       [128000, '125 kB'],
+                                       [128000000, '122.1 MB'],
+                                       [128000000000, '119.2 GB'],
+                                       [128000000000000, '116.4 TB']
+                               ];
+                               for (var i = 0; i < data.length; i++) {
+                                       expect(OC.Util.humanFileSize(data[i][0], true)).toEqual(data[i][1]);
+                               }
+                       });
                });
        });
 });