Skip to content

Commit 2222c6e

Browse files
committed
Fixing shorthand for bit suffixes
1 parent 1d0eb61 commit 2222c6e

File tree

6 files changed

+14
-7
lines changed

6 files changed

+14
-7
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ filesize.js provides a simple way to get a human readable file size string from
55
## Examples
66

77
``` js
8+
filesize(500); // "3.91Kb"
9+
filesize(500, true); // "3.9k"
810
filesize(1500); // "1.46KB"
911
filesize("1500000000"); // "1.40GB"
1012
filesize("1500000000", 0); // "1GB"
11-
filesize(1212312421412412); // "1102.59TB"
12-
filesize(1212312421412412, true); // "1102.6T" - shorthand output, similar to *nix "ls -lh"
13+
filesize(1212312421412412); // "1.08PB"
14+
filesize(1212312421412412, true); // "1.1P" - shorthand output, similar to *nix "ls -lh"
1315
```
1416

1517
## How can I load filesize.js?

lib/filesize.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @license BSD-3 <http://opensource.org/licenses/BSD-3-Clause>
77
* @link https://github.com/avoidwork/filesize.js
88
* @module filesize
9-
* @version 1.7.0
9+
* @version 1.7.1
1010
*/
1111

1212
(function (global) {
@@ -47,6 +47,7 @@
4747
if (num >= size) {
4848
result = (suffix === "B" ? num : (num / size)).toFixed(pos);
4949
if (short) {
50+
if (/b$/.test(suffix)) suffix = suffix.toLowerCase();
5051
suffix = suffix.slice(0, 1);
5152
z = regex.exec(result);
5253
if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result, base);

lib/filesize.min.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
* @license BSD-3 <http://opensource.org/licenses/BSD-3-Clause>
77
* @link https://github.com/avoidwork/filesize.js
88
* @module filesize
9-
* @version 1.7.0
9+
* @version 1.7.1
1010
*/
11-
(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);
11+
(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);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "filesize",
33
"description": "JavaScript library to generate a human readable String describing the file size",
4-
"version": "1.7.0",
4+
"version": "1.7.1",
55
"homepage": "https://github.com/avoidwork/filesize.js",
66
"author": {
77
"name": "Jason Mulligan",

src/filesize.js

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
if (num >= size) {
3737
result = (suffix === "B" ? num : (num / size)).toFixed(pos);
3838
if (short) {
39+
if (/b$/.test(suffix)) suffix = suffix.toLowerCase();
3940
suffix = suffix.slice(0, 1);
4041
z = regex.exec(result);
4142
if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result, base);

test/filesize_test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ exports["filesize"] = {
55
this.num = 1024;
66
this.str = "1024";
77
this.invld = "abc";
8+
this.Kb = 500;
89
done();
910
},
1011
valid: function (test) {
11-
test.expect(6);
12+
test.expect(8);
13+
test.equal(filesize(this.Kb), "3.91Kb", "Should match");
14+
test.equal(filesize(this.Kb,true), "3.9k", "Should match");
1215
test.equal(filesize(this.num), "1.00KB", "Should match");
1316
test.equal(filesize(this.str), "1.00KB", "Should match");
1417
test.equal(filesize(this.num, 1), "1.0KB", "Should match");

0 commit comments

Comments
 (0)