Skip to content

Commit

Permalink
Fixing shorthand for bit suffixes
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Nov 3, 2012
1 parent 1d0eb61 commit 2222c6e
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 7 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
3 changes: 2 additions & 1 deletion lib/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @license BSD-3 <http://opensource.org/licenses/BSD-3-Clause>
* @link https://github.com/avoidwork/filesize.js
* @module filesize
* @version 1.7.0
* @version 1.7.1
*/

(function (global) {
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions lib/filesize.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
* @license BSD-3 <http://opensource.org/licenses/BSD-3-Clause>
* @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);
(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);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion test/filesize_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 2222c6e

Please sign in to comment.