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)
This commit is contained in:
Mark McDowall 2013-12-03 07:39:02 -08:00
parent 149f94b006
commit c3bd5e0053
2 changed files with 123 additions and 135 deletions

View File

@ -6,64 +6,41 @@
* @license BSD-3 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE> * @license BSD-3 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE>
* @link http://filesizejs.com * @link http://filesizejs.com
* @module filesize * @module filesize
* @version 1.10.0 * @version 2.0.0
*/ */
( function ( global ) { ( function ( global ) {
"use strict"; "use strict";
var base = 10, var bit = /b$/,
right = /\.(.*)/,
bit = /b$/,
bite = /^B$/, bite = /^B$/,
zero = /^0$/, radix = 10,
options; right = /\.(.*)/,
zero = /^0$/;
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 * filesize
* *
* @method filesize
* @param {Mixed} arg String, Int or Float to transform * @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 {Object} descriptor [Optional] Flags
* @param {Boolean} bits [Optional] Determines if `bit` sizes are used for result calculation, default is true
* @return {String} Readable file size String * @return {String} Readable file size String
*/ */
function filesize ( arg) { function filesize ( arg, descriptor ) {
var result = "", var result = "",
bits = true,
skip = false, skip = false,
i, neg, num, pos, shrt, size, sizes, suffix, z; i = 6,
base, bits, neg, num, round, size, sizes, unix, spacer, suffix, z;
// Determining arguments if ( isNaN( arg ) ) {
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];
}
}
if ( isNaN( arg ) || ( pos !== undefined && isNaN( pos ) ) ) {
throw new Error( "Invalid arguments" ); throw new Error( "Invalid arguments" );
} }
shrt = ( shrt === true ); descriptor = descriptor || {};
bits = ( bits === true ); bits = ( descriptor.bits === true );
pos = shrt ? 1 : ( pos === undefined ? 2 : parseInt( pos, base ) ); 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 ); num = Number( arg );
neg = ( num < 0 ); neg = ( num < 0 );
@ -74,22 +51,15 @@
// Zero is now a special case because bytes divide by 1 // Zero is now a special case because bytes divide by 1
if ( num === 0 ) { if ( num === 0 ) {
if ( shrt ) { if ( unix ) {
result = "0"; result = "0";
} }
else { else {
result = "0 B"; result = "0" + spacer + "B";
} }
} }
else { else {
if ( bits ) { sizes = options[base][bits ? "bits" : "bytes"];
sizes = options.all.increments;
i = options.all.nth;
}
else {
sizes = options.bitless.increments;
i = options.bitless.nth;
}
while ( i-- ) { while ( i-- ) {
size = sizes[i][1]; size = sizes[i][1];
@ -99,12 +69,12 @@
// Treating bytes as cardinal // Treating bytes as cardinal
if ( bite.test( suffix ) ) { if ( bite.test( suffix ) ) {
skip = true; skip = true;
pos = 0; round = 0;
} }
result = ( num / size ).toFixed( pos ); result = ( num / size ).toFixed( round );
if ( !skip && shrt ) { if ( !skip && unix ) {
if ( bits && bit.test( suffix ) ) { if ( bits && bit.test( suffix ) ) {
suffix = suffix.toLowerCase(); suffix = suffix.toLowerCase();
} }
@ -112,19 +82,20 @@
suffix = suffix.charAt( 0 ); suffix = suffix.charAt( 0 );
z = right.exec( result ); z = right.exec( result );
if ( suffix === "k" ) { if ( !bits && suffix === "k" ) {
suffix = "K"; suffix = "K";
} }
if ( z !== null && z[1] !== undefined && zero.test( z[1] ) ) { if ( z !== null && z[1] !== undefined && zero.test( z[1] ) ) {
result = parseInt( result, base ); result = parseInt( result, radix );
} }
result += suffix; result += spacer + suffix;
} }
else if ( !shrt ) { else if ( !unix ) {
result += " " + suffix; result += spacer + suffix;
} }
break; break;
} }
} }
@ -138,6 +109,22 @@
return result; 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]]
}
};
// CommonJS, AMD, script tag // CommonJS, AMD, script tag
if ( typeof exports !== "undefined" ) { if ( typeof exports !== "undefined" ) {
module.exports = filesize; module.exports = filesize;
@ -150,4 +137,5 @@
else { else {
global.filesize = filesize; global.filesize = filesize;
} }
} )( this ); } )( this );

View File

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