Skip to content

Commit f214346

Browse files
committed
Refactored to ES6, with transpiling to ES5
Reformatting `else` statements
1 parent db1df8e commit f214346

10 files changed

+326
-187
lines changed

Gruntfile.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = function(grunt) {
2222
"src/si.js",
2323
"src/outro.js"
2424
],
25-
dest : "lib/filesize.js"
25+
dest : "lib/filesize.es6.js"
2626
}
2727
},
2828
exec : {
@@ -33,11 +33,15 @@ module.exports = function(grunt) {
3333
cmd : "echo //@ sourceMappingURL=<%= pkg.name %>.map >> lib/<%= pkg.name %>.min.js"
3434
}
3535
},
36-
jshint : {
37-
options : {
38-
jshintrc : ".jshintrc"
36+
"6to5": {
37+
options: {
38+
sourceMap: false
3939
},
40-
src : "lib/<%= pkg.name %>.js"
40+
dist: {
41+
files: {
42+
"lib/<%= pkg.name %>.js": "lib/<%= pkg.name %>.es6.js"
43+
}
44+
}
4145
},
4246
nodeunit : {
4347
all : ["test/*.js"]
@@ -80,12 +84,12 @@ module.exports = function(grunt) {
8084
grunt.loadNpmTasks("grunt-sed");
8185
grunt.loadNpmTasks("grunt-contrib-concat");
8286
grunt.loadNpmTasks("grunt-contrib-nodeunit");
83-
grunt.loadNpmTasks("grunt-contrib-jshint");
8487
grunt.loadNpmTasks('grunt-contrib-watch');
8588
grunt.loadNpmTasks("grunt-contrib-uglify");
89+
grunt.loadNpmTasks("grunt-6to5");
8690

8791
// aliases
88-
grunt.registerTask("test", ["jshint", "nodeunit"]);
89-
grunt.registerTask("build", ["concat", "sed"]);
92+
grunt.registerTask("test", [ "nodeunit"]);
93+
grunt.registerTask("build", ["concat", "sed", "6to5"]);
9094
grunt.registerTask("default", ["build", "test", "uglify"]);
9195
};

lib/filesize.es6.js

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/**
2+
* filesize
3+
*
4+
* @author Jason Mulligan <[email protected]>
5+
* @copyright 2015 Jason Mulligan
6+
* @license BSD-3 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE>
7+
* @link http://filesizejs.com
8+
* @module filesize
9+
* @version 3.1.0
10+
*/
11+
( global ) => {
12+
const bit = /b$/;
13+
14+
/**
15+
* filesize
16+
*
17+
* @method filesize
18+
* @param {Mixed} arg String, Int or Float to transform
19+
* @param {Object} descriptor [Optional] Flags
20+
* @return {String} Readable file size String
21+
*/
22+
let filesize = ( arg, descriptor ) => {
23+
let result = [],
24+
skip = false,
25+
val = 0,
26+
e, base, bits, ceil, neg, num, output, round, unix, spacer, suffixes;
27+
28+
if ( isNaN( arg ) ) {
29+
throw new Error( "Invalid arguments" );
30+
}
31+
32+
descriptor = descriptor || {};
33+
bits = ( descriptor.bits === true );
34+
unix = ( descriptor.unix === true );
35+
base = descriptor.base !== undefined ? descriptor.base : 2;
36+
round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2;
37+
spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " ";
38+
suffixes = descriptor.suffixes !== undefined ? descriptor.suffixes : {};
39+
output = descriptor.output !== undefined ? descriptor.output : "string";
40+
e = descriptor.exponent !== undefined ? descriptor.exponent : -1;
41+
num = Number( arg );
42+
neg = ( num < 0 );
43+
ceil = base > 2 ? 1000 : 1024;
44+
45+
// Flipping a negative number to determine the size
46+
if ( neg ) {
47+
num = -num;
48+
}
49+
50+
// Zero is now a special case because bytes divide by 1
51+
if ( num === 0 ) {
52+
result[ 0 ] = 0;
53+
54+
if ( unix ) {
55+
result[ 1 ] = "";
56+
} else {
57+
result[ 1 ] = "B";
58+
}
59+
} else {
60+
// Determining the exponent
61+
if ( e === -1 || isNaN( e ) ) {
62+
e = Math.floor( Math.log( num ) / Math.log( ceil ) );
63+
}
64+
65+
// Exceeding supported length, time to reduce & multiply
66+
if ( e > 8 ) {
67+
val = val * ( 1000 * ( e - 8 ) );
68+
e = 8;
69+
}
70+
71+
if ( base === 2 ) {
72+
val = num / Math.pow( 2, ( e * 10 ) );
73+
} else {
74+
val = num / Math.pow( 1000, e );
75+
}
76+
77+
if ( bits ) {
78+
val = ( val * 8 );
79+
80+
if ( val > ceil ) {
81+
val = val / ceil;
82+
e++;
83+
}
84+
}
85+
86+
result[ 0 ] = Number( val.toFixed( e > 0 ? round : 0 ) );
87+
result[ 1 ] = si[ bits ? "bits" : "bytes" ][ e ];
88+
89+
if ( !skip && unix ) {
90+
if ( bits && bit.test( result[ 1 ] ) ) {
91+
result[ 1 ] = result[ 1 ].toLowerCase();
92+
}
93+
94+
result[ 1 ] = result[ 1 ].charAt( 0 );
95+
96+
if ( result[ 1 ] === "B" ) {
97+
result[ 0 ] = Math.floor( result[ 0 ] );
98+
result[ 1 ] = "";
99+
} else if ( !bits && result[ 1 ] === "k" ) {
100+
result[ 1 ] = "K";
101+
}
102+
}
103+
}
104+
105+
// Decorating a 'diff'
106+
if ( neg ) {
107+
result[ 0 ] = -result[ 0 ];
108+
}
109+
110+
// Applying custom suffix
111+
result[ 1 ] = suffixes[ result[ 1 ] ] || result[ 1 ];
112+
113+
// Returning Array, Object, or String (default)
114+
if ( output === "array" ) {
115+
return result;
116+
} else if ( output === "exponent" ) {
117+
return e;
118+
} else if ( output === "object" ) {
119+
return { value: result[ 0 ], suffix: result[ 1 ] };
120+
} else {
121+
return result.join( spacer );
122+
}
123+
}
124+
125+
/**
126+
* SI suffixes
127+
*
128+
* @type {Object}
129+
*/
130+
const si = {
131+
bits: [ "B", "kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb" ],
132+
bytes: [ "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" ]
133+
};
134+
135+
// CommonJS, AMD, script tag
136+
if ( typeof exports !== "undefined" ) {
137+
module.exports = filesize;
138+
} else if ( typeof define === "function" ) {
139+
define( () => {
140+
return filesize;
141+
} );
142+
} else {
143+
global.filesize = filesize;
144+
}
145+
}( this );

0 commit comments

Comments
 (0)