Skip to content

Commit

Permalink
Completing 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Oct 12, 2013
1 parent c401052 commit 09a7dbc
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 110 deletions.
1 change: 0 additions & 1 deletion .jamignore

This file was deleted.

9 changes: 4 additions & 5 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/node_modules/
lib/filesize.min.js
lib/filesize.map
src
test
.jamignore
/lib/filesize.min.js
/lib/filesize.map
/src/
/test/
.jshintrc
.travis.yml
Gruntfile.js
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ module.exports = function(grunt) {
grunt.registerTask("test", ["nodeunit"]);
grunt.registerTask("build", ["concat", "sed", "exec"]);
grunt.registerTask("default", ["build", "test", "lint"]);
};
};
30 changes: 24 additions & 6 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
Copyright (c) 2012, Jason Mulligan
Copyright (c) 2013, Jason Mulligan
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of avoidwork inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

Neither the name of the {organization} nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 changes: 25 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
[![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.

## Examples
## Optional settings

`filesize()` accepts an optional descriptor Object as a second argument, so you can customize the output.

#### bits
Enables `bit` sizes, default is `false`

#### unix
Enables unix style human readable output, e.g `ls -lh`, default is `false`

1.10.0 switched to base 10, all previous versions use base 2.
#### base
Number base, default is `10`

#### round
Decimal place, default is `2`

#### spacer
Character between the `result` and `suffix`, default is `" "`

## Examples

```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"
```

## How can I load filesize.js?
Expand Down
37 changes: 20 additions & 17 deletions lib/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ var bit = /b$/,
bite = /^B$/,
radix = 10,
right = /\.(.*)/,
zero = /^0$/,
options;
zero = /^0$/;

/**
* filesize
Expand All @@ -30,18 +29,18 @@ function filesize ( arg, descriptor ) {
var result = "",
skip = false,
i = 6,
base, bits, pos, neg, num, size, sizes, shrt, spacer, suffix, z;
base, bits, neg, num, round, size, sizes, unix, spacer, suffix, z;

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

descriptor = descriptor || {};
bits = ( descriptor.bits === true );
shrt = ( descriptor["short"] === true );
base = descriptor.base !== undefined ? descriptor.base : 10;
pos = descriptor.pos !== undefined ? descriptor.pos : shrt ? 0 : 2;
spacer = descriptor.spacer !== undefined ? descriptor.spacer : shrt ? "" : " ";
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 );

Expand All @@ -52,11 +51,11 @@ function filesize ( arg, descriptor ) {

// Zero is now a special case because bytes divide by 1
if ( num === 0 ) {
if ( shrt ) {
if ( unix ) {
result = "0";
}
else {
result = "0 B";
result = "0" + spacer + "B";
}
}
else {
Expand All @@ -69,30 +68,34 @@ function filesize ( arg, descriptor ) {
if ( num >= size ) {
// Treating bytes as cardinal
if ( bite.test( suffix ) ) {
skip = true;
pos = 0;
skip = true;
round = 0;
}

result = ( num / size ).toFixed( pos );
result = ( num / size ).toFixed( round );

if ( !skip && shrt ) {
if ( !skip && unix ) {
if ( bits && bit.test( suffix ) ) {
suffix = suffix.toLowerCase();
}

suffix = suffix.charAt( 0 );
z = right.exec( result );

if ( suffix === "k" ) {
if ( !bits && suffix === "k" ) {
suffix = "K";
}

if ( z !== null && z[1] !== undefined && zero.test( z[1] ) ) {
result = parseInt( result, radix );
}

result += spacer + suffix;
}
else if ( !unix ) {
result += spacer + suffix;
}

result += spacer + suffix;
break;
}
}
Expand All @@ -111,7 +114,7 @@ function filesize ( arg, descriptor ) {
*
* @type {Object}
*/
options = {
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]]
Expand Down
4 changes: 2 additions & 2 deletions lib/filesize.map
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"version":3,
"file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D,CAAO,OAAP,CAAzB,CAA0C,CAAC,IAAD,CAAO,UAAP,CAA1C,CAA8D,CAAC,IAAD,CAAO,aAAP,CAA9D,CAAqF,CAAC,IAAD,CAAO,eAAP,CAArF,CAFL,CADS,IAKR,MACI,CAAC,CAAC,GAAD,CAAM,CAAN,CAAD,CAAW,CAAC,IAAD,CAAO,GAAP,CAAX,CAAyB,CAAC,IAAD,CAAO,KAAP,CAAzB,CAA0C,CAAC,IAAD,CAAO,KAAP,CAA1C,CAA8D,CAAC,IAAD,CAAO,KAAP,CAA9D,CAAqF,CAAC,IAAD,CAAO,MAAP,CAArF,CADJ,OAEI,CAAC,CAAC,GAAD,CAAM,CAAN,CAAD,CAAW,CAAC,IAAD,CAAO,GAAP,CAAX,CAAyB,CAAC,IAAD,CAAO,GAAP,CAAzB,CAA0C,CAAC,IAAD,CAAO,GAAP,CAA1C,CAA8D,CAAC,IAAD,CAAO,IAAP,CAA9D,CAAqF,CAAC,IAAD;AAAO,IAAP,CAArF,CAFJ,CALQ,CAYU,YAAxB,GAAK,MAAOc,QAAZ,CACCC,MAAAD,QADD,CACkBjC,CADlB,CAG4B,UAAvB,GAAK,MAAOmC,OAAZ,CACJA,MAAA,CAAQ,QAAS,EAAG,CACnB,MAAOnC,EADY,CAApB,CADI,CAMJD,CAAAC,SANI,CAMcA,CA/HG,CAApB,CAAA,CAkIG,IAlIH;",
"sources":["filesize.js"],
"names":["global","filesize","arg","descriptor","result","skip","i","base","bits","pos","neg","num","size","sizes","shrt","spacer","isNaN","Error","undefined","Number","options","suffix","bite","test","toFixed","bit","toLowerCase","charAt","z","right","exec","zero","parseInt","radix","exports","module","define"]
"names":["global","filesize","arg","descriptor","result","skip","i","base","bits","neg","num","round","size","sizes","unix","spacer","isNaN","Error","undefined","Number","options","suffix","bite","test","toFixed","bit","toLowerCase","charAt","z","right","exec","zero","parseInt","radix","exports","module","define"]
}
6 changes: 3 additions & 3 deletions lib/filesize.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 18 additions & 14 deletions src/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ function filesize ( arg, descriptor ) {
var result = "",
skip = false,
i = 6,
base, bits, pos, neg, num, size, sizes, shrt, spacer, suffix, z;
base, bits, neg, num, round, size, sizes, unix, spacer, suffix, z;

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

descriptor = descriptor || {};
bits = ( descriptor.bits === true );
shrt = ( descriptor["short"] === true );
base = descriptor.base !== undefined ? descriptor.base : 10;
pos = descriptor.pos !== undefined ? descriptor.pos : shrt ? 0 : 2;
spacer = descriptor.spacer !== undefined ? descriptor.spacer : shrt ? "" : " ";
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 );

Expand All @@ -32,11 +32,11 @@ function filesize ( arg, descriptor ) {

// Zero is now a special case because bytes divide by 1
if ( num === 0 ) {
if ( shrt ) {
if ( unix ) {
result = "0";
}
else {
result = "0 B";
result = "0" + spacer + "B";
}
}
else {
Expand All @@ -49,30 +49,34 @@ function filesize ( arg, descriptor ) {
if ( num >= size ) {
// Treating bytes as cardinal
if ( bite.test( suffix ) ) {
skip = true;
pos = 0;
skip = true;
round = 0;
}

result = ( num / size ).toFixed( pos );
result = ( num / size ).toFixed( round );

if ( !skip && shrt ) {
if ( !skip && unix ) {
if ( bits && bit.test( suffix ) ) {
suffix = suffix.toLowerCase();
}

suffix = suffix.charAt( 0 );
z = right.exec( result );

if ( suffix === "k" ) {
if ( !bits && suffix === "k" ) {
suffix = "K";
}

if ( z !== null && z[1] !== undefined && zero.test( z[1] ) ) {
result = parseInt( result, radix );
}

result += spacer + suffix;
}
else if ( !unix ) {
result += spacer + suffix;
}

result += spacer + suffix;
break;
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/intro.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ var bit = /b$/,
bite = /^B$/,
radix = 10,
right = /\.(.*)/,
zero = /^0$/,
options;
zero = /^0$/;
2 changes: 1 addition & 1 deletion src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @type {Object}
*/
options = {
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]]
Expand Down

0 comments on commit 09a7dbc

Please sign in to comment.