Skip to content

Commit

Permalink
Refactoring filesize() to output Array & Object shapes, & chang…
Browse files Browse the repository at this point in the history
…ing default `base` to `2`, reformatting code based on IDE settings (not included)
  • Loading branch information
avoidwork committed Jan 4, 2015
1 parent 9cbe9a6 commit a7fe91b
Show file tree
Hide file tree
Showing 14 changed files with 204 additions and 197 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/*
.idea/*
24 changes: 19 additions & 5 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ module.exports = function(grunt) {
path : ["<%= concat.dist.dest %>"]
}
},
uglify: {
options: {
banner : "/*\n" +
" <%= grunt.template.today('yyyy') %> <%= pkg.author.name %>\n" +
" @version <%= pkg.version %>\n" +
" */",
sourceMap: true,
sourceMapIncludeSources: true
},
target: {
files: {
"lib/filesize.min.js" : ["lib/filesize.js"]
}
}
},
watch : {
js : {
files : "<%= concat.dist.src %>",
Expand All @@ -63,15 +78,14 @@ module.exports = function(grunt) {

// tasks
grunt.loadNpmTasks("grunt-sed");
grunt.loadNpmTasks("grunt-exec");
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");

// aliases
grunt.registerTask("lint", ["jshint"]);
grunt.registerTask("test", ["nodeunit"]);
grunt.registerTask("build", ["concat", "sed", "exec"]);
grunt.registerTask("default", ["build", "test", "lint"]);
grunt.registerTask("test", ["jshint", "nodeunit"]);
grunt.registerTask("build", ["concat", "sed"]);
grunt.registerTask("default", ["build", "test", "uglify"]);
};
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2013, Jason Mulligan
Copyright (c) 2015, Jason Mulligan
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
Expand Down
39 changes: 19 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
[![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.

## Optional settings

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

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

### base
_***(number)***_ Number base, default is `10`
### output
_***(string)***_ Output of function (`array`, `object`, or `string`), default is `string`

### round
_***(number)***_ Decimal place, default is `2`
Expand All @@ -25,27 +26,25 @@ _***(string)***_ Character between the `result` and `suffix`, default is `" "`
### suffixes
_***(object)***_ Dictionary of SI suffixes to replace for localization, defaults to english if no match is found

### 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.
95 changes: 48 additions & 47 deletions lib/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@
* filesize
*
* @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
*/
( function ( global ) {
"use strict";

var bit = /b$/,
radix = 10,
left = /.*\./,
zero = /^0$/;
var bit = /b$/;

/**
* filesize
Expand All @@ -25,25 +22,26 @@ var bit = /b$/,
* @return {String} Readable file size String
*/
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;

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

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;

// Flipping a negative number to determine the size
if ( neg ) {
Expand All @@ -52,12 +50,13 @@ function filesize ( arg, descriptor ) {

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

if ( unix ) {
result = "0";
result[ 1 ] = "";
}
else {
suffix = "B";
result = "0" + spacer + ( suffixes[suffix] || suffix );
result[ 1 ] = "B";
}
}
else {
Expand All @@ -66,7 +65,7 @@ function filesize ( arg, descriptor ) {
// Exceeding supported length, time to reduce & multiply
if ( e > 8 ) {
val = val * ( 1000 * ( e - 8 ) );
e = 8;
e = 8;
}

if ( base === 2 ) {
Expand All @@ -85,41 +84,44 @@ function filesize ( arg, descriptor ) {
}
}

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 = "";
}
else if ( !bits && suffix === "k" ) {
suffix = "K";
if ( result[ 1 ] === "B" ) {
result[ 0 ] = Math.floor( result[ 0 ] );
result[ 1 ] = "";
}

if ( zero.test( z ) ) {
result = parseInt( result, radix ).toString();
else if ( !bits && result[ 1 ] === "k" ) {
result[ 1 ] = "K";
}

result += spacer + ( suffixes[suffix] || suffix );
}
else if ( !unix ) {
result += spacer + ( suffixes[suffix] || suffix );
}
}

// Decorating a 'diff'
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 );
}
}

/**
Expand All @@ -128,8 +130,8 @@ function filesize ( arg, descriptor ) {
* @type {Object}
*/
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" ]
};

// CommonJS, AMD, script tag
Expand All @@ -144,5 +146,4 @@ else if ( typeof define === "function" ) {
else {
global.filesize = filesize;
}

} )( this );
8 changes: 0 additions & 8 deletions lib/filesize.map

This file was deleted.

14 changes: 5 additions & 9 deletions lib/filesize.min.js

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

1 change: 1 addition & 0 deletions lib/filesize.min.js.map

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

Loading

0 comments on commit a7fe91b

Please sign in to comment.