diff --git a/README.md b/README.md index 3fd44c1..e457fb1 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,13 @@ filesize.js provides a simple way to get a human readable file size string from ## Examples ``` js +filesize(500); // "3.91Kb" +filesize(500, true); // "3.9k" filesize(1500); // "1.46KB" filesize("1500000000"); // "1.40GB" filesize("1500000000", 0); // "1GB" -filesize(1212312421412412); // "1102.59TB" -filesize(1212312421412412, true); // "1102.6T" - shorthand output, similar to *nix "ls -lh" +filesize(1212312421412412); // "1.08PB" +filesize(1212312421412412, true); // "1.1P" - shorthand output, similar to *nix "ls -lh" ``` ## How can I load filesize.js? diff --git a/lib/filesize.js b/lib/filesize.js index 369cc0e..4d27824 100644 --- a/lib/filesize.js +++ b/lib/filesize.js @@ -6,7 +6,7 @@ * @license BSD-3 * @link https://github.com/avoidwork/filesize.js * @module filesize - * @version 1.7.0 + * @version 1.7.1 */ (function (global) { @@ -47,6 +47,7 @@ if (num >= size) { result = (suffix === "B" ? num : (num / size)).toFixed(pos); if (short) { + if (/b$/.test(suffix)) suffix = suffix.toLowerCase(); suffix = suffix.slice(0, 1); z = regex.exec(result); if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result, base); diff --git a/lib/filesize.min.js b/lib/filesize.min.js index 96dbcf1..dc1e357 100644 --- a/lib/filesize.min.js +++ b/lib/filesize.min.js @@ -6,6 +6,6 @@ * @license BSD-3 * @link https://github.com/avoidwork/filesize.js * @module filesize - * @version 1.7.0 + * @version 1.7.1 */ -(function(e){"use strict";var t=function(e){var t=10,n,r,i,s,o,u,a,f,l,c;typeof arguments[2]!="undefined"?(n=arguments[1],r=arguments[2]):typeof arguments[1]=="boolean"?r=arguments[1]:n=arguments[1];if(isNaN(e)||typeof n!="undefined"&&isNaN(n))throw Error("Invalid arguments");r=r===!0,n=r?1:typeof n=="undefined"?2:parseInt(n,t),i=Number(e),s=[["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"]],l=s.length,u="",a=/\.(.*)/;while(l--){o=s[l][1],f=s[l][0],l>3&&(o=Number(o));if(i>=o){u=(f==="B"?i:i/o).toFixed(n),r&&(f=f.slice(0,1),c=a.exec(u),c!==null&&typeof c[1]!="undefined"&&c[1]==="0"&&(u=parseInt(u,t))),u+=f;break}}return u};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;typeof arguments[2]!="undefined"?(n=arguments[1],r=arguments[2]):typeof arguments[1]=="boolean"?r=arguments[1]:n=arguments[1];if(isNaN(e)||typeof n!="undefined"&&isNaN(n))throw Error("Invalid arguments");r=r===!0,n=r?1:typeof n=="undefined"?2:parseInt(n,t),i=Number(e),s=[["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"]],l=s.length,u="",a=/\.(.*)/;while(l--){o=s[l][1],f=s[l][0],l>3&&(o=Number(o));if(i>=o){u=(f==="B"?i:i/o).toFixed(n),r&&(/b$/.test(f)&&(f=f.toLowerCase()),f=f.slice(0,1),c=a.exec(u),c!==null&&typeof c[1]!="undefined"&&c[1]==="0"&&(u=parseInt(u,t))),u+=f;break}}return u};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 a5f53b0..9332d55 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.0", + "version": "1.7.1", "homepage": "https://github.com/avoidwork/filesize.js", "author": { "name": "Jason Mulligan", diff --git a/src/filesize.js b/src/filesize.js index 044b93b..ca2c56a 100644 --- a/src/filesize.js +++ b/src/filesize.js @@ -36,6 +36,7 @@ if (num >= size) { result = (suffix === "B" ? num : (num / size)).toFixed(pos); if (short) { + if (/b$/.test(suffix)) suffix = suffix.toLowerCase(); suffix = suffix.slice(0, 1); z = regex.exec(result); if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result, base); diff --git a/test/filesize_test.js b/test/filesize_test.js index 4d45cf3..c69d609 100644 --- a/test/filesize_test.js +++ b/test/filesize_test.js @@ -5,10 +5,13 @@ exports["filesize"] = { this.num = 1024; this.str = "1024"; this.invld = "abc"; + this.Kb = 500; done(); }, valid: function (test) { - test.expect(6); + test.expect(8); + 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");