Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #33

Merged
merged 2 commits into from
Jun 13, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"smarttabs": true,
"latedef": true,
"undef": true,
"unused": true,
"proto": true,
"supernew": true,
"quotmark": "double",
"curly": true,
"newcap": true,
"noarg": true,
"noempty": false,
"nonew": true,
"quotmark": "double",
"regexp": false,
"trailing": true,
"boss": true,
"eqnull": true,
"expr": true,
"browser": true,
"globals": {
"require": true,
"module": true,
"define": true
}
}
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ lib/filesize.min.js
src
test
.jamignore
.jshintrc
.travis.yml
Gruntfile.js
70 changes: 37 additions & 33 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,53 @@ module.exports = function(grunt) {
],
dest : "lib/filesize.js"
}
},exec : {
closure : {
cmd : "cd lib\nclosure-compiler --js <%= pkg.name %>.js --js_output_file <%= pkg.name %>.min.js --create_source_map ./<%= pkg.name %>.map"
},
sourcemap : {
cmd : "echo //@ sourceMappingURL=<%= pkg.name %>.map >> lib/<%= pkg.name %>.min.js"
}
},
jshint : {
options : {
jshintrc : ".jshintrc"
},
src : "lib/<%= pkg.name %>.js"
},
nodeunit: {
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"]
}
sed : {
"version" : {
pattern : "{{VERSION}}",
replacement : "<%= pkg.version %>",
path : ["<%= concat.dist.dest %>"]
}
},
watch : {
js : {
files : "<%= concat.dist.src %>",
tasks : "build"
},
target: {
files: {
"lib/<%= pkg.name %>.min.js" : ["<%= concat.dist.dest %>"]
}
pkg: {
files : "package.json",
tasks : "build"
}
}
});

// tasks
grunt.loadNpmTasks("grunt-sed");
grunt.loadNpmTasks("grunt-exec");
grunt.loadNpmTasks("grunt-contrib-concat");
grunt.loadNpmTasks("grunt-contrib-nodeunit");
grunt.loadNpmTasks("grunt-contrib-uglify");
grunt.loadNpmTasks("grunt-contrib-jshint");
grunt.loadNpmTasks('grunt-contrib-watch');

// aliases
grunt.registerTask("lint", ["jshint"]);
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"]);
grunt.registerTask("build", ["concat", "sed", "exec"]);
grunt.registerTask("default", ["build", "test", "lint"]);
};
72 changes: 41 additions & 31 deletions lib/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,66 @@
* @license BSD-3 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE>
* @link http://filesizejs.com
* @module filesize
* @version 1.9.3
* @version 1.9.4
*/
( function ( global ) {
"use strict";

var base = 10,
right = /\.(.*)/,
bit = /b$/,
byte = /^B$/,
bite = /^B$/,
zero = /^0$/,
options = {
all : {
increments : [["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 : 11
},
bitless : {
increments : [["B", 1], ["KB", 1024], ["MB", 1.049e+6], ["GB", 1.074e+9], ["TB", 1.1e+12], ["PB", 1.126e+15]],
nth : 6
}
};
options;

options = {
all : {
increments : [["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 : 11
},
bitless : {
increments : [["B", 1], ["KB", 1024], ["MB", 1.049e+6], ["GB", 1.074e+9], ["TB", 1.1e+12], ["PB", 1.126e+15]],
nth : 6
}
};

/**
* filesize
*
*
* @param {Mixed} arg String, Int or Float to transform
* @param {Mixed} pos [Optional] Position to round to, defaults to 2 if short is ommitted, or `true` for shorthand output
* @param {Mixed} pos [Optional] Position to round to, defaults to 2 if shrt is ommitted, or `true` for shrthand output
* @param {Boolean} bits [Optional] Determines if `bit` sizes are used for result calculation, default is true
* @return {String} Readable file size String
*/
function filesize (arg) {
var result = "",
bits = true,
skip = false,
i, neg, num, pos, short, size, sizes, suffix, z;
i, neg, num, pos, shrt, size, sizes, suffix, z;

// Determining arguments
if (arguments[3] !== undefined) {
pos = arguments[1];
short = arguments[2];
bits = arguments[3];
pos = arguments[1];
shrt = arguments[2];
bits = arguments[3];
}
else {
typeof arguments[1] === "boolean" ? short = arguments[1] : pos = arguments[1];
typeof arguments[1] === "boolean" ? shrt = arguments[1] : pos = arguments[1];

if ( typeof arguments[2] === "boolean" ) {
bits = arguments[2];
}
}

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

short = ( short === true );
bits = ( bits === true );
pos = short ? 1 : ( pos === undefined ? 2 : parseInt( pos, base ) );
num = Number( arg );
neg = ( num < 0 );
shrt = ( shrt === true );
bits = ( bits === true );
pos = shrt ? 1 : ( pos === undefined ? 2 : parseInt( pos, base ) );
num = Number( arg );
neg = ( num < 0 );

// Flipping a negative number to determine the size
if ( neg ) {
Expand All @@ -72,7 +74,12 @@

// Zero is now a special case because bytes divide by 1
if ( num === 0 ) {
result = "0B";
if ( shrt ) {
result = "0";
}
else {
result = "0 B";
}
}
else {
if ( bits ) {
Expand All @@ -90,14 +97,14 @@

if ( num >= size ) {
// Treating bytes as cardinal
if ( byte.test( suffix ) ) {
if ( bite.test( suffix ) ) {
skip = true;
pos = 0;
}

result = ( num / size ).toFixed( pos );

if ( !skip && short ) {
if ( !skip && shrt ) {
if ( bits && bit.test( suffix ) ) {
suffix = suffix.toLowerCase();
}
Expand All @@ -108,9 +115,12 @@
if ( z !== null && z[1] !== undefined && zero.test( z[1] ) ) {
result = parseInt( result, base );
}
}

result += suffix;
result += suffix;
}
else if ( !shrt ) {
result += " " + suffix;
}
break;
}
}
Expand All @@ -122,7 +132,7 @@
}

return result;
};
}

// CommonJS, AMD, script tag
if ( typeof exports !== "undefined" ) {
Expand Down
8 changes: 8 additions & 0 deletions lib/filesize.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"version":3,
"file":"filesize.min.js",
"lineCount":10,
"mappings":"A;;;;;;;AAUE,SAAS,CAAEA,CAAF,CAAW,CA6BrBC,QAASA,EAAS,CAACC,CAAD,OAAM,CAAA,IACnBC,EAAS,EADU,CAEnBC,EAAS,CAAA,CAFU,CAGnBC,EAAS,CAAA,CAHU,CAINC,CAJM,CAIDC,CAJC,CAIKC,CAJL,CAIWC,CAGbC,KAAAA,EAArB,GAAI,CAAJ,EACCJ,CAEA,CAFO,CAEP,CADAC,CACA,CADO,CACP,CAAAH,CAAA,CAAO,CAHR,GAMyB,SAAxB,GAAA,MAAO,EAAP,CAAoCG,CAApC,CAA2C,CAA3C,CAA0DD,CAA1D,CAAgE,CAEhE,CAA6B,SAA7B,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CAAzB,CAA2C,CAAC,IAAD,CAAO,MAAP,CAA3C,CAA6D,CAAC,IAAD,CAAO,KAAP,CAA7D,CAA8E,CAAC,IAAD,CAAO,OAAP,CAA9E,CADJ,KAEI,CAFJ,CALD,CAqHc,YAAxB,GAAK,MAAOiB,QAAZ,CACCC,MAAAD,QADD;AACkBlC,CADlB,CAG4B,UAAvB,GAAK,MAAOoC,OAAZ,CACJA,MAAA,CAAQ,QAAS,EAAG,CACnB,MAAOpC,EADY,CAApB,CADI,CAMJD,CAAAC,SANI,CAMcA,CAxIE,CAApB,CAAA,CA0IE,IA1IF;",
"sources":["filesize.js"],
"names":["global","filesize","arg","result","bits","skip","pos","shrt","size","sizes","undefined","isNaN","Error","parseInt","base","num","Number","neg","options","all","increments","i","nth","bitless","suffix","bite","test","toFixed","bit","toLowerCase","charAt","z","right","exec","zero","exports","module","define"]
}
22 changes: 11 additions & 11 deletions lib/filesize.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 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.9.3",
"version": "1.9.4",
"homepage": "http://filesizejs.com",
"author": {
"name": "Jason Mulligan",
Expand All @@ -28,11 +28,13 @@
"test": "grunt test"
},
"devDependencies": {
"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"
"grunt": "~0.4.1",
"grunt-exec": "~0.4",
"grunt-sed": "~0.1",
"grunt-contrib-concat": "~0.1.3",
"grunt-contrib-jshint": "~0.1",
"grunt-contrib-nodeunit": "~0.1.2",
"grunt-contrib-watch": "~0.2"
},
"keywords": ["file", "filesize", "size", "readable", "filesystem"]
}
Loading