Skip to content

Commit 0af5ea1

Browse files
committed
Adding the ability to specify the exponent used for determining the file size, and adding exponent as an output value (feature request by email)
1 parent a7fe91b commit 0af5ea1

7 files changed

+31
-9
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ _***(number)***_ Number base, default is `2`
1414
### bits
1515
_***(boolean)***_ Enables `bit` sizes, default is `false`
1616

17+
### exponent
18+
_***(number)***_ Specifies the SI suffix via exponent, e.g. `2` is `MB` for bytes, default is `-1`
19+
1720
### output
18-
_***(string)***_ Output of function (`array`, `object`, or `string`), default is `string`
21+
_***(string)***_ Output of function (`array`, `exponent`, `object`, or `string`), default is `string`
1922

2023
### round
2124
_***(number)***_ Decimal place, default is `2`
@@ -40,6 +43,9 @@ filesize(265318, {round: 0}); // "259 kB"
4043
filesize(265318, {output: "array"}); // [259.1, "kB"]
4144
filesize(265318, {output: "object"}); // {value: 259.1, suffix: "kB"}
4245
filesize(1, {suffixes: {B: "Б"}}); // "1 Б"
46+
filesize(1024); // "1 kB"
47+
filesize(1024, {exponent: 0}); // "1024 B"
48+
filesize(1024, {output: "exponent"}); // 1
4349
```
4450

4551
## How can I load filesize.js?

lib/filesize.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @license BSD-3 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE>
77
* @link http://filesizejs.com
88
* @module filesize
9-
* @version 3.0.0
9+
* @version 3.0.1
1010
*/
1111
( function ( global ) {
1212
"use strict";
@@ -39,6 +39,7 @@ function filesize ( arg, descriptor ) {
3939
spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " ";
4040
suffixes = descriptor.suffixes !== undefined ? descriptor.suffixes : {};
4141
output = descriptor.output !== undefined ? descriptor.output : "string";
42+
e = descriptor.exponent !== undefined ? descriptor.exponent : -1;
4243
num = Number( arg );
4344
neg = ( num < 0 );
4445
ceil = base > 2 ? 1000 : 1024;
@@ -60,7 +61,10 @@ function filesize ( arg, descriptor ) {
6061
}
6162
}
6263
else {
63-
e = Math.floor( Math.log( num ) / Math.log( 1000 ) );
64+
// Determining the exponent
65+
if ( e === -1 || isNaN( e ) ) {
66+
e = Math.floor( Math.log( num ) / Math.log( 1000 ) );
67+
}
6468

6569
// Exceeding supported length, time to reduce & multiply
6670
if ( e > 8 ) {
@@ -116,6 +120,9 @@ function filesize ( arg, descriptor ) {
116120
if ( output === "array" ) {
117121
return result;
118122
}
123+
else if ( output === "exponent" ) {
124+
return e;
125+
}
119126
else if ( output === "object" ) {
120127
return { value: result[ 0 ], suffix: result[ 1 ] };
121128
}

lib/filesize.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/filesize.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "filesize",
33
"description": "JavaScript library to generate a human readable String describing the file size",
4-
"version": "3.0.0",
4+
"version": "3.0.1",
55
"homepage": "http://filesizejs.com",
66
"author": {
77
"name": "Jason Mulligan",

src/filesize.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function filesize ( arg, descriptor ) {
2424
spacer = descriptor.spacer !== undefined ? descriptor.spacer : unix ? "" : " ";
2525
suffixes = descriptor.suffixes !== undefined ? descriptor.suffixes : {};
2626
output = descriptor.output !== undefined ? descriptor.output : "string";
27+
e = descriptor.exponent !== undefined ? descriptor.exponent : -1;
2728
num = Number( arg );
2829
neg = ( num < 0 );
2930
ceil = base > 2 ? 1000 : 1024;
@@ -45,7 +46,10 @@ function filesize ( arg, descriptor ) {
4546
}
4647
}
4748
else {
48-
e = Math.floor( Math.log( num ) / Math.log( 1000 ) );
49+
// Determining the exponent
50+
if ( e === -1 || isNaN( e ) ) {
51+
e = Math.floor( Math.log( num ) / Math.log( 1000 ) );
52+
}
4953

5054
// Exceeding supported length, time to reduce & multiply
5155
if ( e > 8 ) {
@@ -101,6 +105,9 @@ function filesize ( arg, descriptor ) {
101105
if ( output === "array" ) {
102106
return result;
103107
}
108+
else if ( output === "exponent" ) {
109+
return e;
110+
}
104111
else if ( output === "object" ) {
105112
return { value: result[ 0 ], suffix: result[ 1 ] };
106113
}

test/filesize_test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ exports["filesize"] = {
1111
done();
1212
},
1313
base2: function (test) {
14-
test.expect(37);
14+
test.expect(39);
1515

1616
test.equal(filesize(this.kilobit), "500 B", "Should be '500 B'");
1717
test.equal(filesize(this.kilobit, {round: 1}), "500 B", "Should be '500 B'");
@@ -30,6 +30,8 @@ exports["filesize"] = {
3030
test.equal(filesize(this.kilobyte, {bits :true}), "8 kb", "Should be '8 kb'");
3131
test.equal(filesize(this.kilobyte, {round: 1, bits: true}), "8 kb", "Should be '8 kb'");
3232
test.equal(filesize(this.kilobyte, {unix: true, bits: true}), "8k", "Should be '8k'");
33+
test.equal(filesize(this.kilobyte, {exponent: 0}), "1024 B", "Should be '1024 B'");
34+
test.equal(filesize(this.kilobyte, {output: "exponent"}), 1, "Should be '1'");
3335

3436
test.equal(filesize(this.neg), "-1 kB", "Should be '-1 kB'");
3537
test.equal(filesize(this.neg, {round: 1}), "-1 kB", "Should be '-1 kB'");

0 commit comments

Comments
 (0)