Skip to content

Commit 39e6529

Browse files
committed
Implemented a grunt build process & unit tests
1 parent ce70843 commit 39e6529

11 files changed

+261
-147
lines changed

.jamignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
2-
debug
3-
production
2+
dist
3+
src
4+
test

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules/

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# filesize.js
2+
23
filesize.js provides a simple way to get a human readable file size string from a number (float or integer) or string. An optional second parameter is the decimal place to round to (default is 2), or _true_ which triggers Unix style output. The maximum supported size is a terabyte. When hard drives get bigger, I'll add support for petabytes.
34

45
## Examples
6+
57
``` js
68
filesize(1500); // "1.46KB"
79
filesize("1500000000"); // "1.40GB"
@@ -11,11 +13,15 @@ filesize(1212312421412412, true); // "1102.6T" - shorthand output, similar to *n
1113
```
1214

1315
## How can I load filesize.js?
16+
1417
filesize.js supports AMD loaders (require.js, curl.js, etc.), node.js & npm (npm install filesize), or using a script tag.
1518

1619
## Information
20+
1721
#### License
22+
1823
filesize.js is licensed under BSD-3 http://www.opensource.org/licenses/BSD-3-Clause
1924

2025
#### Copyright
26+
2127
Copyright (c) 2012, Jason Mulligan <[email protected]>

debug/filesize.js

-93
This file was deleted.

dist/filesize.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* filesize
3+
*
4+
* @author Jason Mulligan <[email protected]>
5+
* @copyright Jason Mulligan 2012
6+
* @license BSD-3 <http://opensource.org/licenses/BSD-3-Clause>
7+
* @link https://github.com/avoidwork/filesize.js
8+
* @module filesize
9+
* @version 1.6.6
10+
*/
11+
12+
(function (global) {
13+
"use strict";
14+
15+
/**
16+
* filesize
17+
*
18+
* @param {Mixed} arg String, Int or Float to transform
19+
* @param {Number} pos [Optional] Position to round to, defaults to 2 if short is ommitted
20+
* @param {Boolean} short [Optional] Shorthand output, similar to "ls -lh", overrides pos to 1
21+
* @return {String} Readable file size String
22+
*/
23+
var filesize = function (arg) {
24+
var pos, short, num, sizes, size, result, regex, suffix, i, z;
25+
26+
if (typeof arguments[2] !== "undefined") {
27+
pos = arguments[1];
28+
short = arguments[2];
29+
}
30+
else typeof arguments[1] === "boolean" ? short = arguments[1] : pos = arguments[1];
31+
32+
if (isNaN(arg) || (typeof pos !== "undefined" && isNaN(pos))) throw Error("Invalid arguments");
33+
34+
short = (short === true);
35+
pos = short ? 1 : (typeof pos === "undefined" ? 2 : parseInt(pos));
36+
num = String(arg).indexOf(".") > -1 ? parseFloat(arg) : parseInt(arg);
37+
sizes = [["B", 0], ["KB", 1024], ["MB", 1048576], ["GB", 1073741824], ["TB", 1099511627776]];
38+
i = sizes.length;
39+
result = "";
40+
regex = /\.(.*)/;
41+
42+
while (i--) {
43+
size = sizes[i][1];
44+
suffix = sizes[i][0];
45+
if (num >= size) {
46+
result = (suffix === "B" ? num : (num / size)).toFixed(pos);
47+
if (short) {
48+
suffix = suffix.slice(0, 1);
49+
z = regex.exec(result);
50+
if (z !== null && typeof z[1] !== "undefined" && z[1] === "0") result = parseInt(result);
51+
}
52+
result += suffix;
53+
break;
54+
}
55+
}
56+
57+
return result;
58+
};
59+
60+
switch (true) {
61+
case typeof exports !== "undefined":
62+
module.exports = filesize;
63+
break;
64+
case typeof define === "function":
65+
define(function () { return filesize; });
66+
break;
67+
default:
68+
global.filesize = filesize;
69+
}
70+
})(this);

dist/filesize.min.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* filesize
3+
*
4+
* @author Jason Mulligan <[email protected]>
5+
* @copyright Jason Mulligan 2012
6+
* @license BSD-3 <http://opensource.org/licenses/BSD-3-Clause>
7+
* @link https://github.com/avoidwork/filesize.js
8+
* @module filesize
9+
* @version 1.6.6
10+
*/
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);

grunt.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
module.exports = function(grunt) {
2+
grunt.initConfig({
3+
pkg : "<json:package.json>",
4+
meta : {
5+
banner : "/**\n" +
6+
" * <%= pkg.name %>\n" +
7+
" *\n" +
8+
" * @author <%= pkg.author.name %> <<%= pkg.author.email %>>\n" +
9+
" * @copyright <%= pkg.author.name %> <%= grunt.template.today('yyyy') %>\n" +
10+
" * @license <%= pkg.licenses[0].type %> <<%= pkg.licenses[0].url %>>\n" +
11+
" * @link <%= pkg.homepage %>\n" +
12+
" * @module <%= pkg.name %>\n" +
13+
" * @version <%= pkg.version %>\n" +
14+
" */"
15+
},
16+
concat: {
17+
dist: {
18+
src : [
19+
"<banner>",
20+
"src/filesize.js"
21+
],
22+
dest : "dist/filesize.js"
23+
}
24+
},
25+
test : {
26+
files : ["test/**/*.js"]
27+
},
28+
lint : {
29+
files : ["grunt.js"]
30+
},
31+
min : {
32+
"dist/filesize.min.js" : ["<banner>", "dist/filesize.js"]
33+
},
34+
watch : {
35+
files : "<config:lint.files>",
36+
tasks : "default"
37+
},
38+
jshint : {
39+
options : {
40+
curly : true,
41+
eqeqeq : true,
42+
immed : true,
43+
latedef : true,
44+
newcap : true,
45+
noarg : true,
46+
sub : true,
47+
undef : true,
48+
boss : true,
49+
eqnull : true,
50+
node : true
51+
},
52+
globals: {
53+
exports : true
54+
}
55+
}
56+
});
57+
58+
grunt.registerTask("default", "concat min test");
59+
};

package.json

+25-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
{
2-
"author": "Jason Mulligan <[email protected]> (http://avoidwork.com/)",
32
"name": "filesize",
43
"description": "JavaScript library to generate a human readable String describing the file size",
5-
"version": "1.6.5",
6-
"homepage": "http://filesizejs.com",
4+
"version": "1.6.6",
5+
"homepage": "https://github.com/avoidwork/filesize.js",
6+
"author": {
7+
"name": "Jason Mulligan",
8+
"email": "[email protected]"
9+
},
710
"repository": {
811
"type": "git",
912
"url": "git://github.com/avoidwork/filesize.js.git"
1013
},
11-
"main": "debug/filesize.js",
14+
"bugs": {
15+
"url": "https://github.com/avoidwork/filesize.js/issues"
16+
},
17+
"licenses": [
18+
{
19+
"type": "BSD-3",
20+
"url": "http://opensource.org/licenses/BSD-3-Clause"
21+
}
22+
],
23+
"main": "lib/filesize",
1224
"engines": {
13-
"node": "~0.6.11"
25+
"node": ">= 0.6.0"
26+
},
27+
"scripts": {
28+
"test": "grunt test"
29+
},
30+
"devDependencies": {
31+
"grunt": "~0.3.12"
1432
},
15-
"dependencies": {},
16-
"devDependencies": {},
17-
"optionalDependencies": {}
18-
}
33+
"keywords": []
34+
}

production/filesize.js

-43
This file was deleted.

0 commit comments

Comments
 (0)