Skip to content

Commit 396cb33

Browse files
committed
Merge pull request #19 from avoidwork/edge
Minor refactoring
2 parents 07ba4db + 5075979 commit 396cb33

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

lib/filesize.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @license BSD-3 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE>
77
* @link http://filesizejs.com
88
* @module filesize
9-
* @version 1.7.7
9+
* @version 1.7.8
1010
*/
1111

1212
(function (global) {
@@ -53,7 +53,7 @@
5353
result = (num / size).toFixed(pos);
5454
if (short) {
5555
if (bit.test(suffix)) suffix = suffix.toLowerCase();
56-
suffix = suffix.slice(0, 1);
56+
suffix = suffix.charAt(0);
5757
z = regex.exec(result);
5858
if (z !== null && z[1] !== undefined && zero.test(z[1])) result = parseInt(result, base);
5959
}
@@ -62,6 +62,12 @@
6262
}
6363
}
6464

65+
// Zero
66+
if (result === "") {
67+
if (short) pos = 0;
68+
result = Number(0).toFixed(pos) + suffix;
69+
}
70+
6571
return (neg ? "-" : "") + result;
6672
};
6773

lib/filesize.min.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
* @license BSD-3 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE>
77
* @link http://filesizejs.com
88
* @module filesize
9-
* @version 1.7.7
9+
* @version 1.7.8
1010
*/
11-
(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);
11+
(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.charAt(0),p=u.exec(a),p!==null&&p[1]!==undefined&&d.test(p[1])&&(a=parseInt(a,t))),a+=h;break}}return a===""&&(f&&(o=0),a=Number(0).toFixed(o)+h),(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);

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.7",
4+
"version": "1.7.8",
55
"homepage": "http://filesizejs.com",
66
"author": {
77
"name": "Jason Mulligan",

src/filesize.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
result = (num / size).toFixed(pos);
4343
if (short) {
4444
if (bit.test(suffix)) suffix = suffix.toLowerCase();
45-
suffix = suffix.slice(0, 1);
45+
suffix = suffix.charAt(0);
4646
z = regex.exec(result);
4747
if (z !== null && z[1] !== undefined && zero.test(z[1])) result = parseInt(result, base);
4848
}
@@ -51,6 +51,12 @@
5151
}
5252
}
5353

54+
// Zero
55+
if (result === "") {
56+
if (short) pos = 0;
57+
result = Number(0).toFixed(pos) + suffix;
58+
}
59+
5460
return (neg ? "-" : "") + result;
5561
};
5662

test/filesize_test.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ exports["filesize"] = {
88
this.Kb = 500;
99
this.neg = -1024;
1010
this.byte = 1;
11+
this.zero = 0;
1112
done();
1213
},
1314
valid: function (test) {
14-
test.expect(13);
15+
test.expect(16);
1516
test.equal(filesize(this.Kb), "3.91Kb", "Should match");
1617
test.equal(filesize(this.Kb,true), "3.9k", "Should match");
1718
test.equal(filesize(this.num), "1.00KB", "Should match");
@@ -25,6 +26,9 @@ exports["filesize"] = {
2526
test.equal(filesize(this.byte), "1.00B", "Should match");
2627
test.equal(filesize(this.byte, 1), "1.0B", "Should match");
2728
test.equal(filesize(this.byte, true), "1B", "Should match");
29+
test.equal(filesize(this.zero), "0.00B", "Should match");
30+
test.equal(filesize(this.zero, 1), "0.0B", "Should match");
31+
test.equal(filesize(this.zero, true), "0B", "Should match");
2832
this.byte = 1;
2933
test.done();
3034
},

0 commit comments

Comments
 (0)