Skip to content

Commit 8315027

Browse files
committed
Specified the base/radix for parseInt() & simplified the number cast
1 parent edae6f2 commit 8315027

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

lib/filesize.js

+6-5
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.6.6
9+
* @version 1.6.7
1010
*/
1111

1212
(function (global) {
@@ -21,7 +21,8 @@
2121
* @return {String} Readable file size String
2222
*/
2323
var filesize = function (arg) {
24-
var pos, short, num, sizes, size, result, regex, suffix, i, z;
24+
var base = 10,
25+
pos, short, num, sizes, size, result, regex, suffix, i, z;
2526

2627
if (typeof arguments[2] !== "undefined") {
2728
pos = arguments[1];
@@ -32,8 +33,8 @@
3233
if (isNaN(arg) || (typeof pos !== "undefined" && isNaN(pos))) throw Error("Invalid arguments");
3334

3435
short = (short === true);
35-
pos = short ? 1 : (typeof pos === "undefined" ? 2 : parseInt(pos));
36-
num = String(arg).indexOf(".") > -1 ? parseFloat(arg) : parseInt(arg);
36+
pos = short ? 1 : (typeof pos === "undefined" ? 2 : parseInt(pos, base));
37+
num = Number(arg);
3738
sizes = [["B", 0], ["KB", 1024], ["MB", 1048576], ["GB", 1073741824], ["TB", 1099511627776]];
3839
i = sizes.length;
3940
result = "";
@@ -47,7 +48,7 @@
4748
if (short) {
4849
suffix = suffix.slice(0, 1);
4950
z = regex.exec(result);
50-
if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result);
51+
if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result, base);
5152
}
5253
result += suffix;
5354
break;

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.6.6
9+
* @version 1.6.7
1010
*/
11-
(function(a){"use strict";var b=function(a){var b,c,d,e,f,g,h,i,j,k;typeof arguments[2]!="undefined"?(b=arguments[1],c=arguments[2]):typeof arguments[1]=="boolean"?c=arguments[1]:b=arguments[1];if(isNaN(a)||typeof b!="undefined"&&isNaN(b))throw Error("Invalid arguments");c=c===!0,b=c?1:typeof b=="undefined"?2:parseInt(b),d=String(a).indexOf(".")>-1?parseFloat(a):parseInt(a),e=[["B",0],["KB",1024],["MB",1048576],["GB",1073741824],["TB",1099511627776]],j=e.length,g="",h=/\.(.*)/;while(j--){f=e[j][1],i=e[j][0];if(d>=f){g=(i==="B"?d:d/f).toFixed(b),c&&(i=i.slice(0,1),k=h.exec(g),k!==null&&typeof k[1]!="undefined"&&k[1]==="0"&&(g=parseInt(g))),g+=i;break}}return g};switch(!0){case typeof exports!="undefined":module.exports=b;break;case typeof define=="function":define(function(){return b});break;default:a.filesize=b}})(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",1024],["MB",1048576],["GB",1073741824],["TB",1099511627776]],l=s.length,u="",a=/\.(.*)/;while(l--){o=s[l][1],f=s[l][0];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);

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.6.6",
4+
"version": "1.6.7",
55
"homepage": "https://github.com/avoidwork/filesize.js",
66
"author": {
77
"name": "Jason Mulligan",

src/filesize.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
* @return {String} Readable file size String
1111
*/
1212
var filesize = function (arg) {
13-
var pos, short, num, sizes, size, result, regex, suffix, i, z;
13+
var base = 10,
14+
pos, short, num, sizes, size, result, regex, suffix, i, z;
1415

1516
if (typeof arguments[2] !== "undefined") {
1617
pos = arguments[1];
@@ -21,8 +22,8 @@
2122
if (isNaN(arg) || (typeof pos !== "undefined" && isNaN(pos))) throw Error("Invalid arguments");
2223

2324
short = (short === true);
24-
pos = short ? 1 : (typeof pos === "undefined" ? 2 : parseInt(pos));
25-
num = String(arg).indexOf(".") > -1 ? parseFloat(arg) : parseInt(arg);
25+
pos = short ? 1 : (typeof pos === "undefined" ? 2 : parseInt(pos, base));
26+
num = Number(arg);
2627
sizes = [["B", 0], ["KB", 1024], ["MB", 1048576], ["GB", 1073741824], ["TB", 1099511627776]];
2728
i = sizes.length;
2829
result = "";
@@ -36,7 +37,7 @@
3637
if (short) {
3738
suffix = suffix.slice(0, 1);
3839
z = regex.exec(result);
39-
if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result);
40+
if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result, base);
4041
}
4142
result += suffix;
4243
break;

0 commit comments

Comments
 (0)