Comparing version 1.8.0 to 1.9.0
@@ -9,13 +9,21 @@ /** | ||
* @module filesize | ||
* @version 1.8.0 | ||
* @version 1.9.0 | ||
*/ | ||
(function (global) { | ||
( function ( global ) { | ||
"use strict"; | ||
var base = 10, | ||
sizes = [["B", 1], ["Kb", 128], ["KB", 1024], ["Mb", 131072], ["MB", 1.049e+6], ["Gb", 1.342e+8], ["GB", 1.074e+9], ["Tb", 1.374e+11], ["TB", 1.1e+12], ["Pb", 1.407e+14], ["PB", 1.126e+15]], | ||
nth = sizes.length, | ||
regex = /\.(.*)/, | ||
bit = /b$/, | ||
zero = /^0$/; | ||
var base = 10, | ||
right = /\.(.*)/, | ||
bit = /b$/, | ||
zero = /^0$/, | ||
options = { | ||
all : { | ||
increments : [["B", 1], ["Kb", 128], ["KB", 1024], ["Mb", 131072], ["MB", 1.049e+6], ["Gb", 1.342e+8], ["GB", 1.074e+9], ["Tb", 1.374e+11], ["TB", 1.1e+12], ["Pb", 1.407e+14], ["PB", 1.126e+15]], | ||
nth : 11 | ||
}, | ||
bitless : { | ||
increments : [["B", 1], ["KB", 1024], ["MB", 1.049e+6], ["GB", 1.074e+9], ["TB", 1.1e+12], ["PB", 1.126e+15]], | ||
nth : 6 | ||
} | ||
}; | ||
@@ -28,2 +36,3 @@ /** | ||
* @param {Boolean} short [Optional] Shorthand output, similar to "ls -lh", overrides pos to 1 | ||
* @param {Boolean} bits [Optional] Determines if `bit` sizes are used for result calculation, default is true | ||
* @return {String} Readable file size String | ||
@@ -33,38 +42,72 @@ */ | ||
var result = "", | ||
i = nth, | ||
neg, num, pos, short, size, suffix, z; | ||
bits = true, | ||
i, neg, num, pos, short, size, sizes, suffix, z; | ||
if (arguments[2] !== undefined) { | ||
// Determining arguments | ||
if (arguments[3] !== undefined) { | ||
pos = arguments[1]; | ||
short = arguments[2]; | ||
bits = arguments[3]; | ||
} | ||
else typeof arguments[1] === "boolean" ? short = arguments[1] : pos = arguments[1]; | ||
else { | ||
typeof arguments[1] === "boolean" ? short = arguments[1] : pos = arguments[1]; | ||
if (isNaN(arg) || (pos !== undefined && isNaN(pos))) throw Error("Invalid arguments"); | ||
if ( typeof arguments[2] === "boolean" ) { | ||
bits = arguments[2]; | ||
} | ||
} | ||
short = (short === true); | ||
pos = short ? 1 : (pos === undefined ? 2 : parseInt(pos, base)); | ||
num = Number(arg); | ||
neg = (num < 0); | ||
if ( isNaN( arg ) || ( pos !== undefined && isNaN( pos ) ) ) { | ||
throw Error("Invalid arguments"); | ||
} | ||
short = ( short === true ); | ||
bits = ( bits === true ); | ||
pos = short ? 1 : ( pos === undefined ? 2 : parseInt( pos, base ) ); | ||
num = Number( arg ); | ||
neg = ( num < 0 ); | ||
// Flipping a negative number to determine the size | ||
if (neg) num = -num; | ||
if ( neg ) { | ||
num = -num; | ||
} | ||
// Zero is now a special case because bytes divide by 1 | ||
if (num === 0) { | ||
if (short) pos = 0; | ||
result = Number(0).toFixed(pos) + "B"; | ||
if ( num === 0 ) { | ||
if ( short ) { | ||
pos = 0; | ||
} | ||
result = Number( 0 ).toFixed( pos ) + "B"; | ||
} | ||
else { | ||
while (i--) { | ||
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) { | ||
result = (num / size).toFixed(pos); | ||
if (short) { | ||
if (bit.test(suffix)) suffix = suffix.toLowerCase(); | ||
suffix = suffix.charAt(0); | ||
z = regex.exec(result); | ||
if (z !== null && z[1] !== undefined && zero.test(z[1])) result = parseInt(result, base); | ||
if ( num >= size ) { | ||
result = ( num / size ).toFixed( pos ); | ||
if ( short ) { | ||
if ( bits && bit.test( suffix ) ) { | ||
suffix = suffix.toLowerCase(); | ||
} | ||
suffix = suffix.charAt( 0 ); | ||
z = right.exec( result ); | ||
if ( z !== null && z[1] !== undefined && zero.test( z[1] ) ) { | ||
result = parseInt( result, base ); | ||
} | ||
} | ||
result += suffix; | ||
@@ -76,8 +119,22 @@ break; | ||
return (neg ? "-" : "") + result; | ||
// Decorating a 'diff' | ||
if ( neg ) { | ||
result = "-" + result; | ||
} | ||
return result; | ||
}; | ||
if (typeof exports !== "undefined") module.exports = filesize; | ||
else if (typeof define === "function") define(function () { return filesize; }); | ||
else global.filesize = filesize; | ||
})(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 ); |
@@ -9,4 +9,4 @@ /** | ||
* @module filesize | ||
* @version 1.8.0 | ||
* @version 1.9.0 | ||
*/ | ||
(function(e){"use strict";function filesize(e){var u,a,f,d,b,m,g,l="",c=n;if(void 0!==arguments[2]?(f=arguments[1],d=arguments[2]):"boolean"==typeof arguments[1]?d=arguments[1]:f=arguments[1],isNaN(e)||void 0!==f&&isNaN(f))throw Error("Invalid arguments");if(d=d===!0,f=d?1:void 0===f?2:parseInt(f,t),a=Number(e),u=0>a,u&&(a=-a),0===a)d&&(f=0),l=Number(0).toFixed(f)+"B";else for(;c--;)if(b=r[c][1],m=r[c][0],a>=b){l=(a/b).toFixed(f),d&&(o.test(m)&&(m=m.toLowerCase()),m=m.charAt(0),g=i.exec(l),null!==g&&void 0!==g[1]&&s.test(g[1])&&(l=parseInt(l,t))),l+=m;break}return(u?"-":"")+l}var t=10,r=[["B",1],["Kb",128],["KB",1024],["Mb",131072],["MB",1049e3],["Gb",1342e5],["GB",1074e6],["Tb",1374e8],["TB",11e11],["Pb",1407e11],["PB",1126e12]],n=r.length,i=/\.(.*)/,o=/b$/,s=/^0$/;"undefined"!=typeof exports?module.exports=filesize:"function"==typeof define?define(function(){return filesize}):e.filesize=filesize})(this); | ||
(function(e){"use strict";function filesize(e){var o,a,u,l,m,f,b,B,d,c="",g=!0;if(void 0!==arguments[3]?(l=arguments[1],m=arguments[2],g=arguments[3]):("boolean"==typeof arguments[1]?m=arguments[1]:l=arguments[1],"boolean"==typeof arguments[2]&&(g=arguments[2])),isNaN(e)||void 0!==l&&isNaN(l))throw Error("Invalid arguments");if(m=m===!0,g=g===!0,l=m?1:void 0===l?2:parseInt(l,t),u=Number(e),a=0>u,a&&(u=-u),0===u)m&&(l=0),c=Number(0).toFixed(l)+"B";else for(g?(b=i.all.increments,o=i.all.nth):(b=i.bitless.increments,o=i.bitless.nth);o--;)if(f=b[o][1],B=b[o][0],u>=f){c=(u/f).toFixed(l),m&&(g&&r.test(B)&&(B=B.toLowerCase()),B=B.charAt(0),d=n.exec(c),null!==d&&void 0!==d[1]&&s.test(d[1])&&(c=parseInt(c,t))),c+=B;break}return a&&(c="-"+c),c}var t=10,n=/\.(.*)/,r=/b$/,s=/^0$/,i={all:{increments:[["B",1],["Kb",128],["KB",1024],["Mb",131072],["MB",1049e3],["Gb",1342e5],["GB",1074e6],["Tb",1374e8],["TB",11e11],["Pb",1407e11],["PB",1126e12]],nth:11},bitless:{increments:[["B",1],["KB",1024],["MB",1049e3],["GB",1074e6],["TB",11e11],["PB",1126e12]],nth:6}};"undefined"!=typeof exports?module.exports=filesize:"function"==typeof define?define(function(){return filesize}):e.filesize=filesize})(this); |
{ | ||
"name": "filesize", | ||
"description": "JavaScript library to generate a human readable String describing the file size", | ||
"version": "1.8.0", | ||
"version": "1.9.0", | ||
"homepage": "http://filesizejs.com", | ||
@@ -25,3 +25,3 @@ "author": { | ||
"engines": { | ||
"node": ">= 0.8.0" | ||
"node": ">= 0.4.0" | ||
}, | ||
@@ -28,0 +28,0 @@ "scripts": { |
[![build status](https://secure.travis-ci.org/avoidwork/filesize.js.png)](http://travis-ci.org/avoidwork/filesize.js) | ||
# filesize.js | ||
filesize.js provides a simple way to get a human readable file size string from a number (float or integer) or string. An optional second parameter is the decimal place to round to (default is 2), or _true_ which triggers Unix style output. | ||
filesize.js provides a simple way to get a human readable file size string from a number (float or integer) or string. An optional second parameter is the decimal place to round to (default is 2), or _true_ which triggers Unix style output. An optional third parameter lets you disable `bit` sizes, e.g. "Kb". | ||
@@ -16,2 +16,3 @@ ## Examples | ||
filesize(1212312421412412, true); // "1.1P" - shorthand output, similar to *nix "ls -lh" | ||
filesize(265318, 2, false) // "259.10KB" - disabled `bit` sizes with third argument | ||
``` | ||
@@ -31,2 +32,2 @@ | ||
Copyright (c) 2012, Jason Mulligan <jason.mulligan@avoidwork.com> | ||
Copyright (c) 2013, Jason Mulligan <jason.mulligan@avoidwork.com> |
@@ -1,10 +0,18 @@ | ||
(function (global) { | ||
( function ( global ) { | ||
"use strict"; | ||
var base = 10, | ||
sizes = [["B", 1], ["Kb", 128], ["KB", 1024], ["Mb", 131072], ["MB", 1.049e+6], ["Gb", 1.342e+8], ["GB", 1.074e+9], ["Tb", 1.374e+11], ["TB", 1.1e+12], ["Pb", 1.407e+14], ["PB", 1.126e+15]], | ||
nth = sizes.length, | ||
regex = /\.(.*)/, | ||
bit = /b$/, | ||
zero = /^0$/; | ||
var base = 10, | ||
right = /\.(.*)/, | ||
bit = /b$/, | ||
zero = /^0$/, | ||
options = { | ||
all : { | ||
increments : [["B", 1], ["Kb", 128], ["KB", 1024], ["Mb", 131072], ["MB", 1.049e+6], ["Gb", 1.342e+8], ["GB", 1.074e+9], ["Tb", 1.374e+11], ["TB", 1.1e+12], ["Pb", 1.407e+14], ["PB", 1.126e+15]], | ||
nth : 11 | ||
}, | ||
bitless : { | ||
increments : [["B", 1], ["KB", 1024], ["MB", 1.049e+6], ["GB", 1.074e+9], ["TB", 1.1e+12], ["PB", 1.126e+15]], | ||
nth : 6 | ||
} | ||
}; | ||
@@ -17,2 +25,3 @@ /** | ||
* @param {Boolean} short [Optional] Shorthand output, similar to "ls -lh", overrides pos to 1 | ||
* @param {Boolean} bits [Optional] Determines if `bit` sizes are used for result calculation, default is true | ||
* @return {String} Readable file size String | ||
@@ -22,38 +31,72 @@ */ | ||
var result = "", | ||
i = nth, | ||
neg, num, pos, short, size, suffix, z; | ||
bits = true, | ||
i, neg, num, pos, short, size, sizes, suffix, z; | ||
if (arguments[2] !== undefined) { | ||
// Determining arguments | ||
if (arguments[3] !== undefined) { | ||
pos = arguments[1]; | ||
short = arguments[2]; | ||
bits = arguments[3]; | ||
} | ||
else typeof arguments[1] === "boolean" ? short = arguments[1] : pos = arguments[1]; | ||
else { | ||
typeof arguments[1] === "boolean" ? short = arguments[1] : pos = arguments[1]; | ||
if (isNaN(arg) || (pos !== undefined && isNaN(pos))) throw Error("Invalid arguments"); | ||
if ( typeof arguments[2] === "boolean" ) { | ||
bits = arguments[2]; | ||
} | ||
} | ||
short = (short === true); | ||
pos = short ? 1 : (pos === undefined ? 2 : parseInt(pos, base)); | ||
num = Number(arg); | ||
neg = (num < 0); | ||
if ( isNaN( arg ) || ( pos !== undefined && isNaN( pos ) ) ) { | ||
throw Error("Invalid arguments"); | ||
} | ||
short = ( short === true ); | ||
bits = ( bits === true ); | ||
pos = short ? 1 : ( pos === undefined ? 2 : parseInt( pos, base ) ); | ||
num = Number( arg ); | ||
neg = ( num < 0 ); | ||
// Flipping a negative number to determine the size | ||
if (neg) num = -num; | ||
if ( neg ) { | ||
num = -num; | ||
} | ||
// Zero is now a special case because bytes divide by 1 | ||
if (num === 0) { | ||
if (short) pos = 0; | ||
result = Number(0).toFixed(pos) + "B"; | ||
if ( num === 0 ) { | ||
if ( short ) { | ||
pos = 0; | ||
} | ||
result = Number( 0 ).toFixed( pos ) + "B"; | ||
} | ||
else { | ||
while (i--) { | ||
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) { | ||
result = (num / size).toFixed(pos); | ||
if (short) { | ||
if (bit.test(suffix)) suffix = suffix.toLowerCase(); | ||
suffix = suffix.charAt(0); | ||
z = regex.exec(result); | ||
if (z !== null && z[1] !== undefined && zero.test(z[1])) result = parseInt(result, base); | ||
if ( num >= size ) { | ||
result = ( num / size ).toFixed( pos ); | ||
if ( short ) { | ||
if ( bits && bit.test( suffix ) ) { | ||
suffix = suffix.toLowerCase(); | ||
} | ||
suffix = suffix.charAt( 0 ); | ||
z = right.exec( result ); | ||
if ( z !== null && z[1] !== undefined && zero.test( z[1] ) ) { | ||
result = parseInt( result, base ); | ||
} | ||
} | ||
result += suffix; | ||
@@ -65,8 +108,22 @@ break; | ||
return (neg ? "-" : "") + result; | ||
// Decorating a 'diff' | ||
if ( neg ) { | ||
result = "-" + result; | ||
} | ||
return result; | ||
}; | ||
if (typeof exports !== "undefined") module.exports = filesize; | ||
else if (typeof define === "function") define(function () { return filesize; }); | ||
else global.filesize = filesize; | ||
})(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 ); |
@@ -15,20 +15,38 @@ var filesize = require("../lib/filesize.js"); | ||
valid: function (test) { | ||
test.expect(16); | ||
test.equal(filesize(this.Kb), "3.91Kb", "Should match"); | ||
test.equal(filesize(this.Kb,true), "3.9k", "Should match"); | ||
test.equal(filesize(this.num), "1.00KB", "Should match"); | ||
test.equal(filesize(this.str), "1.00KB", "Should match"); | ||
test.equal(filesize(this.num, 1), "1.0KB", "Should match"); | ||
test.equal(filesize(this.str, 1), "1.0KB", "Should match"); | ||
test.equal(filesize(this.num, true), "1K", "Should match"); | ||
test.equal(filesize(this.str, true), "1K", "Should match"); | ||
test.equal(filesize(this.neg), "-1.00KB", "Should match"); | ||
test.equal(filesize(this.neg, true), "-1K", "Should match"); | ||
test.equal(filesize(this.byte), "1.00B", "Should match"); | ||
test.equal(filesize(this.byte, 1), "1.0B", "Should match"); | ||
test.equal(filesize(this.byte, true), "1B", "Should match"); | ||
test.equal(filesize(this.zero), "0.00B", "Should match"); | ||
test.equal(filesize(this.zero, 1), "0.0B", "Should match"); | ||
test.equal(filesize(this.zero, true), "0B", "Should match"); | ||
this.byte = 1; | ||
test.expect(28); | ||
test.equal(filesize(this.Kb), "3.91Kb", "Should be '3.91Kb'"); | ||
test.equal(filesize(this.Kb, 1), "3.9Kb", "Should be '3.9Kb'"); | ||
test.equal(filesize(this.Kb, 1, false), "500.0B", "Should be '500.0B'"); | ||
test.equal(filesize(this.Kb, true), "3.9k", "Should be '3.9k'"); | ||
test.equal(filesize(this.Kb, true, false), "500B", "Should be '500B'"); | ||
test.equal(filesize(this.num), "1.00KB", "Should be '1.00KB'"); | ||
test.equal(filesize(this.num, 1), "1.0KB", "Should be '1.0KB'"); | ||
test.equal(filesize(this.num, 1, false), "1.0KB", "Should be '1.0KB'"); | ||
test.equal(filesize(this.num, true), "1K", "Should be '1K'"); | ||
test.equal(filesize(this.num, true, false), "1K", "Should be '1K'"); | ||
test.equal(filesize(this.str), "1.00KB", "Should be '1.00KB'"); | ||
test.equal(filesize(this.str, 1), "1.0KB", "Should be '1.0KB'"); | ||
test.equal(filesize(this.str, 1, false), "1.0KB", "Should be '1.0KB'"); | ||
test.equal(filesize(this.str, true), "1K", "Should be '1K'"); | ||
test.equal(filesize(this.str, true, false), "1K", "Should be '1K'"); | ||
test.equal(filesize(this.neg), "-1.00KB", "Should be '-1.00KB'"); | ||
test.equal(filesize(this.neg, 1), "-1.0KB", "Should be '-1.0KB'"); | ||
test.equal(filesize(this.neg, 1, false), "-1.0KB", "Should be '-1.0KB'"); | ||
test.equal(filesize(this.neg, true), "-1K", "Should be '-1KB'"); | ||
test.equal(filesize(this.neg, true, false), "-1K", "Should be '-1KB'"); | ||
test.equal(filesize(this.byte), "1.00B", "Should be '1.00B'"); | ||
test.equal(filesize(this.byte, 1), "1.0B", "Should be '1.0B'"); | ||
test.equal(filesize(this.byte, 1, false), "1.0B", "Should be '1.0B'"); | ||
test.equal(filesize(this.byte, true), "1B", "Should be '1B'"); | ||
test.equal(filesize(this.byte, true, false), "1B", "Should be '1B'"); | ||
test.equal(filesize(this.zero), "0.00B", "Should be '0.00B'"); | ||
test.equal(filesize(this.zero, 1), "0.0B", "Should be '0.0B'"); | ||
test.equal(filesize(this.zero, true), "0B", "Should be '0B'"); | ||
test.done(); | ||
@@ -35,0 +53,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16506
351
32