Upgraded Filesize.js to 2.0.0

Fixed: File size/disk space in UI now shows actual size instead of manufacturer size (111 GB instead of 120 GB)
pull/2/head
Mark McDowall 11 years ago
parent 149f94b006
commit c3bd5e0053

@ -6,148 +6,136 @@
* @license BSD-3 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE>
* @link http://filesizejs.com
* @module filesize
* @version 1.10.0
* @version 2.0.0
*/
( function ( global ) {
"use strict";
var base = 10,
right = /\.(.*)/,
bit = /b$/,
bite = /^B$/,
zero = /^0$/,
options;
options = {
all : {
increments : [["B", 1], ["kb", 125], ["kB", 1000], ["Mb", 125000], ["MB", 1000000], ["Gb", 125000000], ["GB", 1000000000], ["Tb", 125000000000], ["TB", 1000000000000], ["Pb", 125000000000000], ["PB", 1000000000000000]],
nth : 11
},
bitless : {
increments : [["B", 1], ["kB", 1000], ["MB", 1000000], ["GB", 1000000000], ["TB", 1000000000000], ["PB", 1000000000000000]],
nth : 6
}
};
/**
* filesize
*
* @param {Mixed} arg String, Int or Float to transform
* @param {Mixed} pos [Optional] Position to round to, defaults to 2 if shrt is ommitted, or `true` for shrthand output
* @param {Boolean} bits [Optional] Determines if `bit` sizes are used for result calculation, default is true
* @return {String} Readable file size String
*/
function filesize ( arg) {
var result = "",
bits = true,
skip = false,
i, neg, num, pos, shrt, size, sizes, suffix, z;
// Determining arguments
if (arguments[3] !== undefined) {
pos = arguments[1];
shrt = arguments[2];
bits = arguments[3];
}
else {
typeof arguments[1] === "boolean" ? shrt = arguments[1] : pos = arguments[1];
if ( typeof arguments[2] === "boolean" ) {
bits = arguments[2];
}
}
"use strict";
if ( isNaN( arg ) || ( pos !== undefined && isNaN( pos ) ) ) {
throw new Error("Invalid arguments");
}
var bit = /b$/,
bite = /^B$/,
radix = 10,
right = /\.(.*)/,
zero = /^0$/;
shrt = ( shrt === true );
bits = ( bits === true );
pos = shrt ? 1 : ( pos === undefined ? 2 : parseInt( pos, base ) );
num = Number( arg );
neg = ( num < 0 );
/**
* filesize
*
* @method filesize
* @param {Mixed} arg String, Int or Float to transform
* @param {Object} descriptor [Optional] Flags
* @return {String} Readable file size String
*/
function filesize ( arg, descriptor ) {
var result = "",
skip = false,
i = 6,
base, bits, neg, num, round, size, sizes, unix, spacer, suffix, z;
if ( isNaN( arg ) ) {
throw new Error( "Invalid arguments" );
}
// Flipping a negative number to determine the size
if ( neg ) {
num = -num;
}
descriptor = descriptor || {};
bits = ( descriptor.bits === true );
unix = ( descriptor.unix === true );
base = descriptor.base !== undefined ? descriptor.base : unix ? 2 : 10;
round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2;
spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " ";
num = Number( arg );
neg = ( num < 0 );
// Flipping a negative number to determine the size
if ( neg ) {
num = -num;
}
// Zero is now a special case because bytes divide by 1
if ( num === 0 ) {
if ( shrt ) {
result = "0";
}
else {
result = "0 B";
}
// Zero is now a special case because bytes divide by 1
if ( num === 0 ) {
if ( unix ) {
result = "0";
}
else {
if ( bits ) {
sizes = options.all.increments;
i = options.all.nth;
}
else {
sizes = options.bitless.increments;
i = options.bitless.nth;
}
while ( i-- ) {
size = sizes[i][1];
suffix = sizes[i][0];
if ( num >= size ) {
// Treating bytes as cardinal
if ( bite.test( suffix ) ) {
skip = true;
pos = 0;
}
result = "0" + spacer + "B";
}
}
else {
sizes = options[base][bits ? "bits" : "bytes"];
result = ( num / size ).toFixed( pos );
while ( i-- ) {
size = sizes[i][1];
suffix = sizes[i][0];
if ( !skip && shrt ) {
if ( bits && bit.test( suffix ) ) {
suffix = suffix.toLowerCase();
}
if ( num >= size ) {
// Treating bytes as cardinal
if ( bite.test( suffix ) ) {
skip = true;
round = 0;
}
suffix = suffix.charAt( 0 );
z = right.exec( result );
result = ( num / size ).toFixed( round );
if ( suffix === "k" ) {
suffix = "K";
}
if ( !skip && unix ) {
if ( bits && bit.test( suffix ) ) {
suffix = suffix.toLowerCase();
}
if ( z !== null && z[1] !== undefined && zero.test( z[1] ) ) {
result = parseInt( result, base );
}
suffix = suffix.charAt( 0 );
z = right.exec( result );
result += suffix;
if ( !bits && suffix === "k" ) {
suffix = "K";
}
else if ( !shrt ) {
result += " " + suffix;
if ( z !== null && z[1] !== undefined && zero.test( z[1] ) ) {
result = parseInt( result, radix );
}
break;
result += spacer + suffix;
}
else if ( !unix ) {
result += spacer + suffix;
}
}
}
// Decorating a 'diff'
if ( neg ) {
result = "-" + result;
break;
}
}
return result;
}
// CommonJS, AMD, script tag
if ( typeof exports !== "undefined" ) {
module.exports = filesize;
// Decorating a 'diff'
if ( neg ) {
result = "-" + result;
}
else if ( typeof define === "function" ) {
define( function () {
return filesize;
});
}
else {
global.filesize = filesize;
return result;
}
/**
* Size options
*
* @type {Object}
*/
var options = {
2 : {
bits : [["B", 1], ["kb", 128], ["Mb", 131072], ["Gb", 134217728], ["Tb", 137438953472], ["Pb", 140737488355328]],
bytes : [["B", 1], ["kB", 1024], ["MB", 1048576], ["GB", 1073741824], ["TB", 1099511627776], ["PB", 1125899906842624]]
},
10 : {
bits : [["B", 1], ["kb", 125], ["Mb", 125000], ["Gb", 125000000], ["Tb", 125000000000], ["Pb", 125000000000000]],
bytes : [["B", 1], ["kB", 1000], ["MB", 1000000], ["GB", 1000000000], ["TB", 1000000000000], ["PB", 1000000000000000]]
}
})( this );
};
// CommonJS, AMD, script tag
if ( typeof exports !== "undefined" ) {
module.exports = filesize;
}
else if ( typeof define === "function" ) {
define( function () {
return filesize;
} );
}
else {
global.filesize = filesize;
}
} )( this );

@ -10,7 +10,7 @@ define(
bytes: function (sourceSize) {
var size = Number(sourceSize);
return Filesize(size, 1, false);
return Filesize(size, { base: 2, round: 1 });
},
dateHelper: function (sourceDate) {

Loading…
Cancel
Save