diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8111245 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +language: node_js +node_js: + - 0.6 + - 0.8 \ No newline at end of file diff --git a/lib/filesize.js b/lib/filesize.js index e6c694c..5f3ae12 100644 --- a/lib/filesize.js +++ b/lib/filesize.js @@ -6,7 +6,7 @@ * @license BSD-3 * @link http://filesizejs.com * @module filesize - * @version 1.7.6 + * @version 1.7.7 */ (function (global) { @@ -22,7 +22,7 @@ */ var filesize = function (arg) { var base = 10, - bit, byte, i, neg, num, pos, regex, result, short, size, sizes, suffix, z, zero; + bit, i, neg, num, pos, regex, result, short, size, sizes, suffix, z, zero; if (arguments[2] !== undefined) { pos = arguments[1]; @@ -36,12 +36,11 @@ pos = short ? 1 : (pos === undefined ? 2 : parseInt(pos, base)); num = Number(arg); neg = (num < 0); - sizes = [["B", 0], ["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"]]; + 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]]; i = sizes.length; result = ""; regex = /\.(.*)/; bit = /b$/; - byte = /^B$/; zero = /^0$/; // Flipping a negative number to determine the size @@ -50,9 +49,8 @@ while (i--) { size = sizes[i][1]; suffix = sizes[i][0]; - if (i > 3) size = Number(size); if (num >= size) { - result = (byte.test(suffix) ? num : (num / size)).toFixed(pos); + result = (num / size).toFixed(pos); if (short) { if (bit.test(suffix)) suffix = suffix.toLowerCase(); suffix = suffix.slice(0, 1); diff --git a/lib/filesize.min.js b/lib/filesize.min.js index b198f74..5b8c687 100644 --- a/lib/filesize.min.js +++ b/lib/filesize.min.js @@ -6,6 +6,6 @@ * @license BSD-3 * @link http://filesizejs.com * @module filesize - * @version 1.7.6 + * @version 1.7.7 */ -(function(e){"use strict";var t=function(e){var t=10,n,r,i,s,o,u,a,f,l,c,h,p,d,v;arguments[2]!==undefined?(u=arguments[1],l=arguments[2]):typeof arguments[1]=="boolean"?l=arguments[1]:u=arguments[1];if(isNaN(e)||u!==undefined&&isNaN(u))throw Error("Invalid arguments");l=l===!0,u=l?1:u===undefined?2:parseInt(u,t),o=Number(e),s=o<0,h=[["B",0],["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"]],i=h.length,f="",a=/\.(.*)/,n=/b$/,r=/^B$/,v=/^0$/,s&&(o=-o);while(i--){c=h[i][1],p=h[i][0],i>3&&(c=Number(c));if(o>=c){f=(r.test(p)?o:o/c).toFixed(u),l&&(n.test(p)&&(p=p.toLowerCase()),p=p.slice(0,1),d=a.exec(f),d!==null&&d[1]!==undefined&&v.test(d[1])&&(f=parseInt(f,t))),f+=p;break}}return(s?"-":"")+f};switch(!0){case typeof exports!="undefined":module.exports=t;break;case typeof define=="function":define(function(){return t});break;default:e.filesize=t}})(this); \ No newline at end of file +(function(e){"use strict";var t=function(e){var t=10,n,r,i,s,o,u,a,f,l,c,h,p,d;arguments[2]!==undefined?(o=arguments[1],f=arguments[2]):typeof arguments[1]=="boolean"?f=arguments[1]:o=arguments[1];if(isNaN(e)||o!==undefined&&isNaN(o))throw Error("Invalid arguments");f=f===!0,o=f?1:o===undefined?2:parseInt(o,t),s=Number(e),i=s<0,c=[["B",1],["Kb",128],["KB",1024],["Mb",131072],["MB",1049e3],["Gb",1342e5],["GB",1074e6],["Tb",1374e8],["TB",11e11],["Pb",1407e11],["PB",1126e12]],r=c.length,a="",u=/\.(.*)/,n=/b$/,d=/^0$/,i&&(s=-s);while(r--){l=c[r][1],h=c[r][0];if(s>=l){a=(s/l).toFixed(o),f&&(n.test(h)&&(h=h.toLowerCase()),h=h.slice(0,1),p=u.exec(a),p!==null&&p[1]!==undefined&&d.test(p[1])&&(a=parseInt(a,t))),a+=h;break}}return(i?"-":"")+a};switch(!0){case typeof exports!="undefined":module.exports=t;break;case typeof define=="function":define(function(){return t});break;default:e.filesize=t}})(this); \ No newline at end of file diff --git a/package.json b/package.json index 24de2d1..f220d8c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "filesize", "description": "JavaScript library to generate a human readable String describing the file size", - "version": "1.7.6", + "version": "1.7.7", "homepage": "http://filesizejs.com", "author": { "name": "Jason Mulligan", diff --git a/src/filesize.js b/src/filesize.js index 7936b98..bd2c66c 100644 --- a/src/filesize.js +++ b/src/filesize.js @@ -11,7 +11,7 @@ */ var filesize = function (arg) { var base = 10, - bit, byte, i, neg, num, pos, regex, result, short, size, sizes, suffix, z, zero; + bit, i, neg, num, pos, regex, result, short, size, sizes, suffix, z, zero; if (arguments[2] !== undefined) { pos = arguments[1]; @@ -25,12 +25,11 @@ pos = short ? 1 : (pos === undefined ? 2 : parseInt(pos, base)); num = Number(arg); neg = (num < 0); - sizes = [["B", 0], ["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"]]; + 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]]; i = sizes.length; result = ""; regex = /\.(.*)/; bit = /b$/; - byte = /^B$/; zero = /^0$/; // Flipping a negative number to determine the size @@ -39,9 +38,8 @@ while (i--) { size = sizes[i][1]; suffix = sizes[i][0]; - if (i > 3) size = Number(size); if (num >= size) { - result = (byte.test(suffix) ? num : (num / size)).toFixed(pos); + result = (num / size).toFixed(pos); if (short) { if (bit.test(suffix)) suffix = suffix.toLowerCase(); suffix = suffix.slice(0, 1); diff --git a/test/filesize_test.js b/test/filesize_test.js index 3baef25..cdbf572 100644 --- a/test/filesize_test.js +++ b/test/filesize_test.js @@ -7,20 +7,25 @@ exports["filesize"] = { this.invld = "abc"; this.Kb = 500; this.neg = -1024; + this.byte = 1; done(); }, valid: function (test) { - test.expect(10); - 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.expect(13); + 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"); + this.byte = 1; test.done(); }, invalid: function (test) {