Comparing version 2.0.4 to 3.0.0
@@ -5,7 +5,7 @@ /** | ||
* @author Jason Mulligan <jason.mulligan@avoidwork.com> | ||
* @copyright 2014 Jason Mulligan | ||
* @copyright 2015 Jason Mulligan | ||
* @license BSD-3 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE> | ||
* @link http://filesizejs.com | ||
* @module filesize | ||
* @version 2.0.4 | ||
* @version 3.0.0 | ||
*/ | ||
@@ -15,6 +15,3 @@ ( function ( global ) { | ||
var bit = /b$/, | ||
radix = 10, | ||
left = /.*\./, | ||
zero = /^0$/; | ||
var bit = /b$/; | ||
@@ -30,6 +27,6 @@ /** | ||
function filesize ( arg, descriptor ) { | ||
var result = "", | ||
skip = false, | ||
val = 0, | ||
e, base, bits, ceil, neg, num, round, unix, spacer, suffix, z, suffixes; | ||
var result = [], | ||
skip = false, | ||
val = 0, | ||
e, base, bits, ceil, neg, num, output, round, unix, spacer, suffixes; | ||
@@ -41,11 +38,12 @@ if ( isNaN( arg ) ) { | ||
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 ? "" : " "; | ||
suffixes = descriptor.suffixes !== undefined ? descriptor.suffixes : {}; | ||
num = Number( arg ); | ||
neg = ( num < 0 ); | ||
ceil = base > 2 ? 1000 : 1024; | ||
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"; | ||
num = Number( arg ); | ||
neg = ( num < 0 ); | ||
ceil = base > 2 ? 1000 : 1024; | ||
@@ -59,8 +57,9 @@ // Flipping a negative number to determine the size | ||
if ( num === 0 ) { | ||
result[ 0 ] = 0; | ||
if ( unix ) { | ||
result = "0"; | ||
result[ 1 ] = ""; | ||
} | ||
else { | ||
suffix = "B"; | ||
result = "0" + spacer + ( suffixes[suffix] || suffix ); | ||
result[ 1 ] = "B"; | ||
} | ||
@@ -74,3 +73,3 @@ } | ||
val = val * ( 1000 * ( e - 8 ) ); | ||
e = 8; | ||
e = 8; | ||
} | ||
@@ -94,29 +93,20 @@ | ||
result = val.toFixed( e > 0 ? round : 0 ); | ||
suffix = si[bits ? "bits" : "bytes"][e]; | ||
result[ 0 ] = Number( val.toFixed( e > 0 ? round : 0 ) ); | ||
result[ 1 ] = si[ bits ? "bits" : "bytes" ][ e ]; | ||
if ( !skip && unix ) { | ||
if ( bits && bit.test( suffix ) ) { | ||
suffix = suffix.toLowerCase(); | ||
if ( bits && bit.test( result[ 1 ] ) ) { | ||
result[ 1 ] = result[ 1 ].toLowerCase(); | ||
} | ||
suffix = suffix.charAt( 0 ); | ||
z = result.replace( left, "" ); | ||
result[ 1 ] = result[ 1 ].charAt( 0 ); | ||
if ( suffix === "B" ) { | ||
suffix = ""; | ||
if ( result[ 1 ] === "B" ) { | ||
result[ 0 ] = Math.floor( result[ 0 ] ); | ||
result[ 1 ] = ""; | ||
} | ||
else if ( !bits && suffix === "k" ) { | ||
suffix = "K"; | ||
else if ( !bits && result[ 1 ] === "k" ) { | ||
result[ 1 ] = "K"; | ||
} | ||
if ( zero.test( z ) ) { | ||
result = parseInt( result, radix ).toString(); | ||
} | ||
result += spacer + ( suffixes[suffix] || suffix ); | ||
} | ||
else if ( !unix ) { | ||
result += spacer + ( suffixes[suffix] || suffix ); | ||
} | ||
} | ||
@@ -126,6 +116,18 @@ | ||
if ( neg ) { | ||
result = "-" + result; | ||
result[ 0 ] = -result[ 0 ]; | ||
} | ||
return result; | ||
// Applying custom suffix | ||
result[ 1 ] = suffixes[ result[ 1 ] ] || result[ 1 ]; | ||
// Returning Array, Object, or String (default) | ||
if ( output === "array" ) { | ||
return result; | ||
} | ||
else if ( output === "object" ) { | ||
return { value: result[ 0 ], suffix: result[ 1 ] }; | ||
} | ||
else { | ||
return result.join( spacer ); | ||
} | ||
} | ||
@@ -139,4 +141,4 @@ | ||
var si = { | ||
bits : ["B", "kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"], | ||
bytes : ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"] | ||
bits: [ "B", "kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb" ], | ||
bytes: [ "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" ] | ||
}; | ||
@@ -156,3 +158,2 @@ | ||
} | ||
} )( this ); |
{ | ||
"name": "filesize", | ||
"description": "JavaScript library to generate a human readable String describing the file size", | ||
"version": "2.0.4", | ||
"version": "3.0.0", | ||
"homepage": "http://filesizejs.com", | ||
@@ -33,3 +33,2 @@ "author": { | ||
"grunt-cli": "~0.1.6", | ||
"grunt-exec": "~0.4", | ||
"grunt-sed": "~0.1", | ||
@@ -39,5 +38,6 @@ "grunt-contrib-concat": "~0.1.3", | ||
"grunt-contrib-nodeunit": "~0.1.2", | ||
"grunt-contrib-watch": "~0.2" | ||
"grunt-contrib-watch": "~0.2", | ||
"grunt-contrib-uglify": "~0.6.0" | ||
}, | ||
"keywords": ["file", "filesize", "size", "readable", "file system"] | ||
} |
@@ -1,4 +0,5 @@ | ||
[![build status](https://secure.travis-ci.org/avoidwork/filesize.js.png)](http://travis-ci.org/avoidwork/filesize.js) | ||
# filesize.js | ||
[![build status](https://secure.travis-ci.org/avoidwork/filesize.js.png)](http://travis-ci.org/avoidwork/filesize.js) | ||
filesize.js provides a simple way to get a human readable file size string from a number (float or integer) or string. | ||
@@ -10,11 +11,11 @@ | ||
### base | ||
_***(number)***_ Number base, default is `2` | ||
### bits | ||
_***(boolean)***_ Enables `bit` sizes, default is `false` | ||
### unix | ||
_***(boolean)***_ Enables unix style human readable output, e.g `ls -lh`, default is `false` | ||
### output | ||
_***(string)***_ Output of function (`array`, `object`, or `string`), default is `string` | ||
### base | ||
_***(number)***_ Number base, default is `10` | ||
### round | ||
@@ -29,25 +30,23 @@ _***(number)***_ Decimal place, default is `2` | ||
### unix | ||
_***(boolean)***_ Enables unix style human readable output, e.g `ls -lh`, default is `false` | ||
## Examples | ||
```javascript | ||
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" | ||
filesize(1, {suffixes: {B: "Б"}}); // "1 Б" | ||
filesize(500); // "500 B" | ||
filesize(500, {bits: true}); // "4 kb" | ||
filesize(265318, {base: 10}); // "265.32 kB" | ||
filesize(265318); // "259.1 kB" | ||
filesize(265318, {round: 0}); // "259 kB" | ||
filesize(265318, {output: "array"}); // [259.1, "kB"] | ||
filesize(265318, {output: "object"}); // {value: 259.1, suffix: "kB"} | ||
filesize(1, {suffixes: {B: "Б"}}); // "1 Б" | ||
``` | ||
## How can I load filesize.js? | ||
filesize.js supports AMD loaders (require.js, curl.js, etc.), node.js & npm (npm install filesize), or using a script tag. | ||
## Support | ||
If you're having problems, use the support forum at CodersClan. | ||
<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 | ||
Copyright (c) 2013 Jason Mulligan | ||
Copyright (c) 2015 Jason Mulligan | ||
Licensed under the BSD-3 license. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35126
15
148
51