Skip to content

Commit

Permalink
Refactored to ES6, with transpiling to ES5
Browse files Browse the repository at this point in the history
Reformatting `else` statements
  • Loading branch information
avoidwork committed Feb 4, 2015
1 parent db1df8e commit f214346
Show file tree
Hide file tree
Showing 10 changed files with 326 additions and 187 deletions.
20 changes: 12 additions & 8 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = function(grunt) {
"src/si.js",
"src/outro.js"
],
dest : "lib/filesize.js"
dest : "lib/filesize.es6.js"
}
},
exec : {
Expand All @@ -33,11 +33,15 @@ module.exports = function(grunt) {
cmd : "echo //@ sourceMappingURL=<%= pkg.name %>.map >> lib/<%= pkg.name %>.min.js"
}
},
jshint : {
options : {
jshintrc : ".jshintrc"
"6to5": {
options: {
sourceMap: false
},
src : "lib/<%= pkg.name %>.js"
dist: {
files: {
"lib/<%= pkg.name %>.js": "lib/<%= pkg.name %>.es6.js"
}
}
},
nodeunit : {
all : ["test/*.js"]
Expand Down Expand Up @@ -80,12 +84,12 @@ module.exports = function(grunt) {
grunt.loadNpmTasks("grunt-sed");
grunt.loadNpmTasks("grunt-contrib-concat");
grunt.loadNpmTasks("grunt-contrib-nodeunit");
grunt.loadNpmTasks("grunt-contrib-jshint");
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks("grunt-contrib-uglify");
grunt.loadNpmTasks("grunt-6to5");

// aliases
grunt.registerTask("test", ["jshint", "nodeunit"]);
grunt.registerTask("build", ["concat", "sed"]);
grunt.registerTask("test", [ "nodeunit"]);
grunt.registerTask("build", ["concat", "sed", "6to5"]);
grunt.registerTask("default", ["build", "test", "uglify"]);
};
145 changes: 145 additions & 0 deletions lib/filesize.es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**
* filesize
*
* @author Jason Mulligan <jason.mulligan@avoidwork.com>
* @copyright 2015 Jason Mulligan
* @license BSD-3 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE>
* @link http://filesizejs.com
* @module filesize
* @version 3.1.0
*/
( global ) => {
const bit = /b$/;

/**
* filesize
*
* @method filesize
* @param {Mixed} arg String, Int or Float to transform
* @param {Object} descriptor [Optional] Flags
* @return {String} Readable file size String
*/
let filesize = ( arg, descriptor ) => {
let result = [],
skip = false,
val = 0,
e, base, bits, ceil, neg, num, output, round, unix, spacer, suffixes;

if ( isNaN( arg ) ) {
throw new Error( "Invalid arguments" );
}

descriptor = descriptor || {};
bits = ( descriptor.bits === true );
unix = ( descriptor.unix === true );
base = descriptor.base !== undefined ? descriptor.base : 2;
round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2;
spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " ";
suffixes = descriptor.suffixes !== undefined ? descriptor.suffixes : {};
output = descriptor.output !== undefined ? descriptor.output : "string";
e = descriptor.exponent !== undefined ? descriptor.exponent : -1;
num = Number( arg );
neg = ( num < 0 );
ceil = base > 2 ? 1000 : 1024;

// Flipping a negative number to determine the size
if ( neg ) {
num = -num;
}

// Zero is now a special case because bytes divide by 1
if ( num === 0 ) {
result[ 0 ] = 0;

if ( unix ) {
result[ 1 ] = "";
} else {
result[ 1 ] = "B";
}
} else {
// Determining the exponent
if ( e === -1 || isNaN( e ) ) {
e = Math.floor( Math.log( num ) / Math.log( ceil ) );
}

// Exceeding supported length, time to reduce & multiply
if ( e > 8 ) {
val = val * ( 1000 * ( e - 8 ) );
e = 8;
}

if ( base === 2 ) {
val = num / Math.pow( 2, ( e * 10 ) );
} else {
val = num / Math.pow( 1000, e );
}

if ( bits ) {
val = ( val * 8 );

if ( val > ceil ) {
val = val / ceil;
e++;
}
}

result[ 0 ] = Number( val.toFixed( e > 0 ? round : 0 ) );
result[ 1 ] = si[ bits ? "bits" : "bytes" ][ e ];

if ( !skip && unix ) {
if ( bits && bit.test( result[ 1 ] ) ) {
result[ 1 ] = result[ 1 ].toLowerCase();
}

result[ 1 ] = result[ 1 ].charAt( 0 );

if ( result[ 1 ] === "B" ) {
result[ 0 ] = Math.floor( result[ 0 ] );
result[ 1 ] = "";
} else if ( !bits && result[ 1 ] === "k" ) {
result[ 1 ] = "K";
}
}
}

// Decorating a 'diff'
if ( neg ) {
result[ 0 ] = -result[ 0 ];
}

// Applying custom suffix
result[ 1 ] = suffixes[ result[ 1 ] ] || result[ 1 ];

// Returning Array, Object, or String (default)
if ( output === "array" ) {
return result;
} else if ( output === "exponent" ) {
return e;
} else if ( output === "object" ) {
return { value: result[ 0 ], suffix: result[ 1 ] };
} else {
return result.join( spacer );
}
}

/**
* SI suffixes
*
* @type {Object}
*/
const si = {
bits: [ "B", "kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb" ],
bytes: [ "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" ]
};

// CommonJS, AMD, script tag
if ( typeof exports !== "undefined" ) {
module.exports = filesize;
} else if ( typeof define === "function" ) {
define( () => {
return filesize;
} );
} else {
global.filesize = filesize;
}
}( this );

0 comments on commit f214346

Please sign in to comment.