Skip to content

Commit

Permalink
Caching variables in closure scope, upgraded to grunt 0.4.0, fixed li…
Browse files Browse the repository at this point in the history
…cense link in README.md

Minor version bump

Dropping support for node.js 0.6.x (too old for build tool)

Updating travis-ci.org file

Major version bump due to change of node.js requirements

Alignment
  • Loading branch information
avoidwork committed Mar 3, 2013
1 parent d1428f6 commit 7648fee
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 118 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/*
5 changes: 1 addition & 4 deletions .jamignore
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
.idea
dist
src
test
/node_modules/
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
language: node_js
node_js:
- 0.6
- 0.8
69 changes: 69 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module.exports = function(grunt) {
grunt.initConfig({
pkg : grunt.file.readJSON("package.json"),
concat: {
options : {
banner : "/**\n" +
" * <%= pkg.name %>\n" +
" *\n" +
" * @author <%= pkg.author.name %> <<%= pkg.author.email %>>\n" +
" * @copyright <%= grunt.template.today('yyyy') %> <%= pkg.author.name %>\n" +
" * @license <%= pkg.licenses[0].type %> <<%= pkg.licenses[0].url %>>\n" +
" * @link <%= pkg.homepage %>\n" +
" * @module <%= pkg.name %>\n" +
" * @version <%= pkg.version %>\n" +
" */\n"
},
dist: {
src : [
"<banner>",
"src/filesize.js"
],
dest : "lib/filesize.js"
}
},
nodeunit: {
all : ["test/*.js"]
},
uglify: {
options: {
banner : "/**\n" +
" * <%= pkg.name %>\n" +
" *\n" +
" * @author <%= pkg.author.name %> <<%= pkg.author.email %>>\n" +
" * @copyright <%= grunt.template.today('yyyy') %> <%= pkg.author.name %>\n" +
" * @license <%= pkg.licenses[0].type %> <<%= pkg.licenses[0].url %>>\n" +
" * @link <%= pkg.homepage %>\n" +
" * @module <%= pkg.name %>\n" +
" * @version <%= pkg.version %>\n" +
" */\n",
mangle: {
except: ["filesize"]
}
},
target: {
files: {
"lib/<%= pkg.name %>.min.js" : ["<%= concat.dist.dest %>"]
}
}
}
});

grunt.loadNpmTasks("grunt-contrib-concat");
grunt.loadNpmTasks("grunt-contrib-nodeunit");
grunt.loadNpmTasks("grunt-contrib-uglify");

grunt.registerTask("test", ["nodeunit"]);

grunt.registerTask("version", function () {
var cfg = grunt.config("pkg"),
ver = cfg.version,
fn = "lib/" + cfg.name + ".js",
fp = grunt.file.read(fn);

console.log("Setting version to: " + ver);
grunt.file.write(fn, fp.replace(/\{\{VERSION\}\}/g, ver));
});

grunt.registerTask("default", ["concat", "version", "uglify", "test"]);
};
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ filesize.js supports AMD loaders (require.js, curl.js, etc.), node.js & npm (npm

#### License

filesize.js is licensed under BSD-3 http://www.opensource.org/licenses/BSD-3-Clause
filesize.js is licensed under BSD-3 https://raw.github.com/avoidwork/filesize.js/master/LICENSE

#### Copyright

Expand Down
59 changes: 0 additions & 59 deletions grunt.js

This file was deleted.

44 changes: 19 additions & 25 deletions lib/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
* @license BSD-3 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE>
* @link http://filesizejs.com
* @module filesize
* @version 1.7.9
* @version 1.8.0
*/

(function (global) {
"use strict";

var base = 10,
sizes = [["B", 1], ["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]],
nth = sizes.length,
regex = /\.(.*)/,
bit = /b$/,
zero = /^0$/;

/**
* filesize
*
Expand All @@ -20,9 +26,10 @@
* @param {Boolean} short [Optional] Shorthand output, similar to "ls -lh", overrides pos to 1
* @return {String} Readable file size String
*/
var filesize = function (arg) {
var base = 10,
bit, i, neg, num, pos, regex, result, short, size, sizes, suffix, z, zero;
function filesize (arg) {
var result = "",
i = nth,
neg, num, pos, short, size, suffix, z;

if (arguments[2] !== undefined) {
pos = arguments[1];
Expand All @@ -32,16 +39,10 @@

if (isNaN(arg) || (pos !== undefined && isNaN(pos))) throw Error("Invalid arguments");

short = (short === true);
pos = short ? 1 : (pos === undefined ? 2 : parseInt(pos, base));
num = Number(arg);
neg = (num < 0);
sizes = [["B", 1], ["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]];
i = sizes.length;
result = "";
regex = /\.(.*)/;
bit = /b$/;
zero = /^0$/;
short = (short === true);
pos = short ? 1 : (pos === undefined ? 2 : parseInt(pos, base));
num = Number(arg);
neg = (num < 0);

// Flipping a negative number to determine the size
if (neg) num = -num;
Expand Down Expand Up @@ -72,14 +73,7 @@
return (neg ? "-" : "") + result;
};

switch (true) {
case typeof exports !== "undefined":
module.exports = filesize;
break;
case typeof define === "function":
define(function () { return filesize; });
break;
default:
global.filesize = filesize;
}
if (typeof exports !== "undefined") module.exports = filesize;
else if (typeof define === "function") define(function () { return filesize; });
else global.filesize = filesize;
})(this);
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 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE>
* @link http://filesizejs.com
* @module filesize
* @version 1.7.9
* @version 1.8.0
*/
(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);if(s===0)f&&(o=0),a=Number(0).toFixed(o)+"B";else 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(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);
(function(e){"use strict";function filesize(e){var u,a,f,d,b,m,g,l="",c=n;if(void 0!==arguments[2]?(f=arguments[1],d=arguments[2]):"boolean"==typeof arguments[1]?d=arguments[1]:f=arguments[1],isNaN(e)||void 0!==f&&isNaN(f))throw Error("Invalid arguments");if(d=d===!0,f=d?1:void 0===f?2:parseInt(f,t),a=Number(e),u=0>a,u&&(a=-a),0===a)d&&(f=0),l=Number(0).toFixed(f)+"B";else for(;c--;)if(b=r[c][1],m=r[c][0],a>=b){l=(a/b).toFixed(f),d&&(o.test(m)&&(m=m.toLowerCase()),m=m.charAt(0),g=i.exec(l),null!==g&&void 0!==g[1]&&s.test(g[1])&&(l=parseInt(l,t))),l+=m;break}return(u?"-":"")+l}var t=10,r=[["B",1],["Kb",128],["KB",1024],["Mb",131072],["MB",1049e3],["Gb",1342e5],["GB",1074e6],["Tb",1374e8],["TB",11e11],["Pb",1407e11],["PB",1126e12]],n=r.length,i=/\.(.*)/,o=/b$/,s=/^0$/;"undefined"!=typeof exports?module.exports=filesize:"function"==typeof define?define(function(){return filesize}):e.filesize=filesize})(this);
10 changes: 7 additions & 3 deletions 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.9",
"version": "1.8.0",
"homepage": "http://filesizejs.com",
"author": {
"name": "Jason Mulligan",
Expand All @@ -22,13 +22,17 @@
],
"main": "lib/filesize",
"engines": {
"node": ">= 0.6.0"
"node": ">= 0.8.0"
},
"scripts": {
"test": "grunt test"
},
"devDependencies": {
"grunt": "~0.3.12"
"grunt" : "~0.4.0",
"grunt-cli" : "~ 0.1.6",
"grunt-contrib-concat" : "~ 0.1.3",
"grunt-contrib-nodeunit" : "~ 0.1.2",
"grunt-contrib-uglify" : "~ 0.1.2"
},
"keywords": ["file", "filesize", "size", "readable", "filesystem"]
}
41 changes: 18 additions & 23 deletions src/filesize.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
(function (global) {
"use strict";

var base = 10,
sizes = [["B", 1], ["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]],
nth = sizes.length,
regex = /\.(.*)/,
bit = /b$/,
zero = /^0$/;

/**
* filesize
*
Expand All @@ -9,9 +16,10 @@
* @param {Boolean} short [Optional] Shorthand output, similar to "ls -lh", overrides pos to 1
* @return {String} Readable file size String
*/
var filesize = function (arg) {
var base = 10,
bit, i, neg, num, pos, regex, result, short, size, sizes, suffix, z, zero;
function filesize (arg) {
var result = "",
i = nth,
neg, num, pos, short, size, suffix, z;

if (arguments[2] !== undefined) {
pos = arguments[1];
Expand All @@ -21,16 +29,10 @@

if (isNaN(arg) || (pos !== undefined && isNaN(pos))) throw Error("Invalid arguments");

short = (short === true);
pos = short ? 1 : (pos === undefined ? 2 : parseInt(pos, base));
num = Number(arg);
neg = (num < 0);
sizes = [["B", 1], ["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]];
i = sizes.length;
result = "";
regex = /\.(.*)/;
bit = /b$/;
zero = /^0$/;
short = (short === true);
pos = short ? 1 : (pos === undefined ? 2 : parseInt(pos, base));
num = Number(arg);
neg = (num < 0);

// Flipping a negative number to determine the size
if (neg) num = -num;
Expand Down Expand Up @@ -61,14 +63,7 @@
return (neg ? "-" : "") + result;
};

switch (true) {
case typeof exports !== "undefined":
module.exports = filesize;
break;
case typeof define === "function":
define(function () { return filesize; });
break;
default:
global.filesize = filesize;
}
if (typeof exports !== "undefined") module.exports = filesize;
else if (typeof define === "function") define(function () { return filesize; });
else global.filesize = filesize;
})(this);

0 comments on commit 7648fee

Please sign in to comment.