Skip to content

Commit

Permalink
Specified the base/radix for parseInt() & simplified the number cast
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Nov 2, 2012
1 parent edae6f2 commit 8315027
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
11 changes: 6 additions & 5 deletions 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.6.6
* @version 1.6.7
*/

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

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

short = (short === true);
pos = short ? 1 : (typeof pos === "undefined" ? 2 : parseInt(pos));
num = String(arg).indexOf(".") > -1 ? parseFloat(arg) : parseInt(arg);
pos = short ? 1 : (typeof pos === "undefined" ? 2 : parseInt(pos, base));
num = Number(arg);
sizes = [["B", 0], ["KB", 1024], ["MB", 1048576], ["GB", 1073741824], ["TB", 1099511627776]];
i = sizes.length;
result = "";
Expand All @@ -47,7 +48,7 @@
if (short) {
suffix = suffix.slice(0, 1);
z = regex.exec(result);
if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result);
if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result, base);
}
result += suffix;
break;
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.6.6
* @version 1.6.7
*/
(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);
(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);
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.6.6",
"version": "1.6.7",
"homepage": "https://github.com/avoidwork/filesize.js",
"author": {
"name": "Jason Mulligan",
Expand Down
9 changes: 5 additions & 4 deletions src/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
* @return {String} Readable file size String
*/
var filesize = function (arg) {
var pos, short, num, sizes, size, result, regex, suffix, i, z;
var base = 10,
pos, short, num, sizes, size, result, regex, suffix, i, z;

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

short = (short === true);
pos = short ? 1 : (typeof pos === "undefined" ? 2 : parseInt(pos));
num = String(arg).indexOf(".") > -1 ? parseFloat(arg) : parseInt(arg);
pos = short ? 1 : (typeof pos === "undefined" ? 2 : parseInt(pos, base));
num = Number(arg);
sizes = [["B", 0], ["KB", 1024], ["MB", 1048576], ["GB", 1073741824], ["TB", 1099511627776]];
i = sizes.length;
result = "";
Expand All @@ -36,7 +37,7 @@
if (short) {
suffix = suffix.slice(0, 1);
z = regex.exec(result);
if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result);
if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result, base);
}
result += suffix;
break;
Expand Down

0 comments on commit 8315027

Please sign in to comment.