display "<1 kB" for really small files

* added parameters for humanFileSize to trigger that behaviour
* add unit tests for that
This commit is contained in:
Morris Jobke 2014-06-02 10:38:46 +02:00
parent d39216c5e7
commit 5d457dafc1
4 changed files with 24 additions and 3 deletions

View File

@ -613,7 +613,7 @@
// size column // size column
if (typeof(fileData.size) !== 'undefined' && fileData.size >= 0) { 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)); sizeColor = Math.round(160-Math.pow((fileData.size/(1024*1024)),2));
} else { } else {
simpleSize = t('files', 'Pending'); simpleSize = t('files', 'Pending');

View File

@ -252,7 +252,7 @@ describe('OCA.Files.FileList tests', function() {
size: '0' size: '0'
}; };
var $tr = fileList.add(fileData); 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() { it('adds new file to the end of the list', function() {
var $tr; var $tr;

View File

@ -1163,9 +1163,10 @@ $.fn.filterAttr = function(attr_name, attr_value) {
/** /**
* Returns a human readable file size * Returns a human readable file size
* @param {number} size Size in bytes * @param {number} size Size in bytes
* @param {boolean} skipSmallSizes return '< 1 kB' for small files
* @return {string} * @return {string}
*/ */
function humanFileSize(size) { function humanFileSize(size, skipSmallSizes) {
var humanList = ['B', 'kB', 'MB', 'GB', 'TB']; var humanList = ['B', 'kB', 'MB', 'GB', 'TB'];
// Calculate Log with base 1024: size = 1024 ** order // Calculate Log with base 1024: size = 1024 ** order
var order = size?Math.floor(Math.log(size) / Math.log(1024)):0; 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); order = Math.min(humanList.length - 1, order);
var readableFormat = humanList[order]; var readableFormat = humanList[order];
var relativeSize = (size / Math.pow(1024, order)).toFixed(1); 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){ if(order < 2){
relativeSize = parseFloat(relativeSize).toFixed(0); relativeSize = parseFloat(relativeSize).toFixed(0);
} }

View File

@ -489,6 +489,19 @@ describe('Core base tests', function() {
expect(OC.Util.humanFileSize(data[i][0])).toEqual(data[i][1]); 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]);
}
});
}); });
}); });
}); });