-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from avoidwork/edge
API change
- Loading branch information
Showing
5 changed files
with
221 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,126 @@ | ||
(function (global) { | ||
( 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$/; | ||
var base = 10, | ||
right = /\.(.*)/, | ||
bit = /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 | ||
} | ||
}; | ||
|
||
/** | ||
* filesize | ||
* | ||
* @param {Mixed} arg String, Int or Float to transform | ||
* @param {Number} pos [Optional] Position to round to, defaults to 2 if short is ommitted | ||
* @param {Boolean} short [Optional] Shorthand output, similar to "ls -lh", overrides pos to 1 | ||
* @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 = "", | ||
i = nth, | ||
neg, num, pos, short, size, suffix, z; | ||
bits = true, | ||
i, neg, num, pos, short, size, sizes, suffix, z; | ||
|
||
if (arguments[2] !== undefined) { | ||
// Determining arguments | ||
if (arguments[3] !== undefined) { | ||
pos = arguments[1]; | ||
short = arguments[2]; | ||
bits = arguments[3]; | ||
} | ||
else typeof arguments[1] === "boolean" ? short = arguments[1] : pos = arguments[1]; | ||
else { | ||
typeof arguments[1] === "boolean" ? short = arguments[1] : pos = arguments[1]; | ||
|
||
if (isNaN(arg) || (pos !== undefined && isNaN(pos))) throw Error("Invalid arguments"); | ||
if ( typeof arguments[2] === "boolean" ) { | ||
bits = arguments[2]; | ||
} | ||
} | ||
|
||
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); | ||
short = ( short === true ); | ||
bits = ( bits === 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; | ||
if ( neg ) { | ||
num = -num; | ||
} | ||
|
||
// Zero is now a special case because bytes divide by 1 | ||
if (num === 0) { | ||
if (short) pos = 0; | ||
result = Number(0).toFixed(pos) + "B"; | ||
if ( num === 0 ) { | ||
if ( short ) { | ||
pos = 0; | ||
} | ||
|
||
result = Number( 0 ).toFixed( pos ) + "B"; | ||
} | ||
else { | ||
while (i--) { | ||
if ( bits ) { | ||
sizes = options.all.increments; | ||
i = options.all.nth; | ||
} | ||
else { | ||
sizes = options.bitless.increments; | ||
i = options.bitless.nth; | ||
} | ||
|
||
while ( i-- ) { | ||
size = sizes[i][1]; | ||
suffix = sizes[i][0]; | ||
if (num >= size) { | ||
result = (num / size).toFixed(pos); | ||
if (short) { | ||
if (bit.test(suffix)) suffix = suffix.toLowerCase(); | ||
suffix = suffix.charAt(0); | ||
z = regex.exec(result); | ||
if (z !== null && z[1] !== undefined && zero.test(z[1])) result = parseInt(result, base); | ||
|
||
if ( num >= size ) { | ||
result = ( num / size ).toFixed( pos ); | ||
|
||
if ( short ) { | ||
if ( bits && bit.test( suffix ) ) { | ||
suffix = suffix.toLowerCase(); | ||
} | ||
|
||
suffix = suffix.charAt( 0 ); | ||
z = right.exec( result ); | ||
|
||
if ( z !== null && z[1] !== undefined && zero.test( z[1] ) ) { | ||
result = parseInt( result, base ); | ||
} | ||
} | ||
|
||
result += suffix; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return (neg ? "-" : "") + result; | ||
// Decorating a 'diff' | ||
if ( neg ) { | ||
result = "-" + result; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
if (typeof exports !== "undefined") module.exports = filesize; | ||
else if (typeof define === "function") define(function () { return filesize; }); | ||
else global.filesize = filesize; | ||
})(this); | ||
// CommonJS, AMD, script tag | ||
if ( typeof exports !== "undefined" ) { | ||
module.exports = filesize; | ||
} | ||
else if ( typeof define === "function" ) { | ||
define( function () { | ||
return filesize; | ||
}); | ||
} | ||
else { | ||
global.filesize = filesize; | ||
} | ||
})( this ); |
Oops, something went wrong.