filesize
Advanced tools
Comparing version 1.10.0 to 2.0.0
@@ -9,146 +9,134 @@ /** | ||
* @module filesize | ||
* @version 1.10.0 | ||
* @version 2.0.0 | ||
*/ | ||
( function ( global ) { | ||
"use strict"; | ||
"use strict"; | ||
var base = 10, | ||
right = /\.(.*)/, | ||
bit = /b$/, | ||
bite = /^B$/, | ||
zero = /^0$/, | ||
options; | ||
var bit = /b$/, | ||
bite = /^B$/, | ||
radix = 10, | ||
right = /\.(.*)/, | ||
zero = /^0$/; | ||
options = { | ||
all : { | ||
increments : [["B", 1], ["kb", 125], ["kB", 1000], ["Mb", 125000], ["MB", 1000000], ["Gb", 125000000], ["GB", 1000000000], ["Tb", 125000000000], ["TB", 1000000000000], ["Pb", 125000000000000], ["PB", 1000000000000000]], | ||
nth : 11 | ||
}, | ||
bitless : { | ||
increments : [["B", 1], ["kB", 1000], ["MB", 1000000], ["GB", 1000000000], ["TB", 1000000000000], ["PB", 1000000000000000]], | ||
nth : 6 | ||
} | ||
}; | ||
/** | ||
* filesize | ||
* | ||
* @method filesize | ||
* @param {Mixed} arg String, Int or Float to transform | ||
* @param {Object} descriptor [Optional] Flags | ||
* @return {String} Readable file size String | ||
*/ | ||
function filesize ( arg, descriptor ) { | ||
var result = "", | ||
skip = false, | ||
i = 6, | ||
base, bits, neg, num, round, size, sizes, unix, spacer, suffix, z; | ||
/** | ||
* filesize | ||
* | ||
* @param {Mixed} arg String, Int or Float to transform | ||
* @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, shrt, size, sizes, suffix, z; | ||
if ( isNaN( arg ) ) { | ||
throw new Error( "Invalid arguments" ); | ||
} | ||
// Determining arguments | ||
if (arguments[3] !== undefined) { | ||
pos = arguments[1]; | ||
shrt = arguments[2]; | ||
bits = arguments[3]; | ||
descriptor = descriptor || {}; | ||
bits = ( descriptor.bits === true ); | ||
unix = ( descriptor.unix === true ); | ||
base = descriptor.base !== undefined ? descriptor.base : unix ? 2 : 10; | ||
round = descriptor.round !== undefined ? descriptor.round : unix ? 1 : 2; | ||
spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " "; | ||
num = Number( arg ); | ||
neg = ( num < 0 ); | ||
// 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 ) { | ||
if ( unix ) { | ||
result = "0"; | ||
} | ||
else { | ||
typeof arguments[1] === "boolean" ? shrt = arguments[1] : pos = arguments[1]; | ||
if ( typeof arguments[2] === "boolean" ) { | ||
bits = arguments[2]; | ||
} | ||
result = "0" + spacer + "B"; | ||
} | ||
} | ||
else { | ||
sizes = options[base][bits ? "bits" : "bytes"]; | ||
if ( isNaN( arg ) || ( pos !== undefined && isNaN( pos ) ) ) { | ||
throw new Error("Invalid arguments"); | ||
} | ||
while ( i-- ) { | ||
size = sizes[i][1]; | ||
suffix = sizes[i][0]; | ||
shrt = ( shrt === true ); | ||
bits = ( bits === true ); | ||
pos = shrt ? 1 : ( pos === undefined ? 2 : parseInt( pos, base ) ); | ||
num = Number( arg ); | ||
neg = ( num < 0 ); | ||
if ( num >= size ) { | ||
// Treating bytes as cardinal | ||
if ( bite.test( suffix ) ) { | ||
skip = true; | ||
round = 0; | ||
} | ||
// Flipping a negative number to determine the size | ||
if ( neg ) { | ||
num = -num; | ||
} | ||
result = ( num / size ).toFixed( round ); | ||
// Zero is now a special case because bytes divide by 1 | ||
if ( num === 0 ) { | ||
if ( shrt ) { | ||
result = "0"; | ||
} | ||
else { | ||
result = "0 B"; | ||
} | ||
} | ||
else { | ||
if ( bits ) { | ||
sizes = options.all.increments; | ||
i = options.all.nth; | ||
} | ||
else { | ||
sizes = options.bitless.increments; | ||
i = options.bitless.nth; | ||
} | ||
if ( !skip && unix ) { | ||
if ( bits && bit.test( suffix ) ) { | ||
suffix = suffix.toLowerCase(); | ||
} | ||
while ( i-- ) { | ||
size = sizes[i][1]; | ||
suffix = sizes[i][0]; | ||
suffix = suffix.charAt( 0 ); | ||
z = right.exec( result ); | ||
if ( num >= size ) { | ||
// Treating bytes as cardinal | ||
if ( bite.test( suffix ) ) { | ||
skip = true; | ||
pos = 0; | ||
if ( !bits && suffix === "k" ) { | ||
suffix = "K"; | ||
} | ||
result = ( num / size ).toFixed( pos ); | ||
if ( z !== null && z[1] !== undefined && zero.test( z[1] ) ) { | ||
result = parseInt( result, radix ); | ||
} | ||
if ( !skip && shrt ) { | ||
if ( bits && bit.test( suffix ) ) { | ||
suffix = suffix.toLowerCase(); | ||
} | ||
result += spacer + suffix; | ||
} | ||
else if ( !unix ) { | ||
result += spacer + suffix; | ||
} | ||
suffix = suffix.charAt( 0 ); | ||
z = right.exec( result ); | ||
if ( suffix === "k" ) { | ||
suffix = "K"; | ||
} | ||
if ( z !== null && z[1] !== undefined && zero.test( z[1] ) ) { | ||
result = parseInt( result, base ); | ||
} | ||
result += suffix; | ||
} | ||
else if ( !shrt ) { | ||
result += " " + suffix; | ||
} | ||
break; | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
// Decorating a 'diff' | ||
if ( neg ) { | ||
result = "-" + result; | ||
} | ||
// Decorating a 'diff' | ||
if ( neg ) { | ||
result = "-" + result; | ||
} | ||
return result; | ||
return result; | ||
} | ||
/** | ||
* Size options | ||
* | ||
* @type {Object} | ||
*/ | ||
var options = { | ||
2 : { | ||
bits : [["B", 1], ["kb", 128], ["Mb", 131072], ["Gb", 134217728], ["Tb", 137438953472], ["Pb", 140737488355328]], | ||
bytes : [["B", 1], ["kB", 1024], ["MB", 1048576], ["GB", 1073741824], ["TB", 1099511627776], ["PB", 1125899906842624]] | ||
}, | ||
10 : { | ||
bits : [["B", 1], ["kb", 125], ["Mb", 125000], ["Gb", 125000000], ["Tb", 125000000000], ["Pb", 125000000000000]], | ||
bytes : [["B", 1], ["kB", 1000], ["MB", 1000000], ["GB", 1000000000], ["TB", 1000000000000], ["PB", 1000000000000000]] | ||
} | ||
}; | ||
// 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 ); | ||
// 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 ); |
{ | ||
"name": "filesize", | ||
"description": "JavaScript library to generate a human readable String describing the file size", | ||
"version": "1.10.0", | ||
"version": "2.0.0", | ||
"homepage": "http://filesizejs.com", | ||
@@ -6,0 +6,0 @@ "author": { |
[![build status](https://secure.travis-ci.org/avoidwork/filesize.js.png)](http://travis-ci.org/avoidwork/filesize.js) | ||
# filesize.js | ||
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. An optional third parameter lets you disable `bit` sizes, e.g. "kb". | ||
filesize.js provides a simple way to get a human readable file size string from a number (float or integer) or string. | ||
## Optional settings | ||
`filesize()` accepts an optional descriptor Object as a second argument, so you can customize the output. | ||
### bits | ||
_***(boolean)***_ Enables `bit` sizes, default is `false` | ||
### unix | ||
_***(boolean)***_ Enables unix style human readable output, e.g `ls -lh`, default is `false` | ||
### base | ||
_***(number)***_ Number base, default is `10` | ||
### round | ||
_***(number)***_ Decimal place, default is `2` | ||
### spacer | ||
_***(string)***_ Character between the `result` and `suffix`, default is `" "` | ||
## Examples | ||
1.10.0 switched to base 10, all previous versions use base 2. | ||
```javascript | ||
filesize(500); // "4.00 Kb" | ||
filesize(500, true); // "4.0k" | ||
filesize(1500); // "1.50 KB" | ||
filesize("1500000000"); // "1.50 GB" | ||
filesize("1500000000", 0); // "2GB" | ||
filesize(1212312421412412); // "1.21 PB PB" | ||
filesize(1212312421412412, true); // "1.1P" - shorthand output, similar to "ls -h" | ||
filesize(265318, 2, false) // "265.32 kB" - disabled `bit` sizes with third argument | ||
filesize(500); // "500 B" | ||
filesize(500, {bits: true}); // "4.00 kb" | ||
filesize(265318); // "265.32 kB" | ||
filesize(265318, {base: 2}); // "259.10 kB" | ||
filesize(265318, {base: 2, round: 1}); // "259.1 kB" | ||
``` | ||
@@ -27,3 +41,3 @@ | ||
If you're having problems with using the project, use the support forum at CodersClan. | ||
If you're having problems, use the support forum at CodersClan. | ||
@@ -33,7 +47,3 @@ <a href="http://codersclan.net/forum/index.php?repo_id=11"><img src="http://www.codersclan.net/graphics/getSupport_blue_big.png" width="160"></a> | ||
## License | ||
filesize.js is licensed under BSD-3 https://raw.github.com/avoidwork/filesize.js/master/LICENSE | ||
## Copyright | ||
Copyright (c) 2013, Jason Mulligan <jason.mulligan@avoidwork.com> | ||
Copyright (c) 2013 Jason Mulligan | ||
Licensed under the BSD-3 license. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
48
7382
121