Skip to content

Commit fdf8b22

Browse files
committedApr 11, 2013
Treating bytes as cardinal numbers
Implementing a regex test instead of a string comparison
1 parent 1914018 commit fdf8b22

File tree

5 files changed

+30
-22
lines changed

5 files changed

+30
-22
lines changed
 

‎lib/filesize.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
* @license BSD-3 <https://raw.github.com/avoidwork/filesize.js/master/LICENSE>
77
* @link http://filesizejs.com
88
* @module filesize
9-
* @version 1.9.1
9+
* @version 1.9.2
1010
*/
1111
( function ( global ) {
1212
"use strict";
1313

1414
var base = 10,
1515
right = /\.(.*)/,
1616
bit = /b$/,
17+
byte = /^B$/,
1718
zero = /^0$/,
1819
options = {
1920
all : {
@@ -38,6 +39,7 @@
3839
function filesize (arg) {
3940
var result = "",
4041
bits = true,
42+
skip = false,
4143
i, neg, num, pos, short, size, sizes, suffix, z;
4244

4345
// Determining arguments
@@ -71,11 +73,7 @@
7173

7274
// Zero is now a special case because bytes divide by 1
7375
if ( num === 0 ) {
74-
if ( short ) {
75-
pos = 0;
76-
}
77-
78-
result = Number( 0 ).toFixed( pos ) + "B";
76+
result = Number( 0 ).toFixed( 0 ) + "B";
7977
}
8078
else {
8179
if ( bits ) {
@@ -92,9 +90,15 @@
9290
suffix = sizes[i][0];
9391

9492
if ( num >= size ) {
93+
// Treating bytes as cardinal
94+
if ( byte.test( suffix ) ) {
95+
skip = true;
96+
pos = 0;
97+
}
98+
9599
result = ( num / size ).toFixed( pos );
96100

97-
if ( short ) {
101+
if ( !skip && short ) {
98102
if ( bits && bit.test( suffix ) ) {
99103
suffix = suffix.toLowerCase();
100104
}

‎lib/filesize.min.js

+2-2
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": "1.9.1",
4+
"version": "1.9.2",
55
"homepage": "http://filesizejs.com",
66
"author": {
77
"name": "Jason Mulligan",

‎src/filesize.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
var base = 10,
55
right = /\.(.*)/,
66
bit = /b$/,
7+
byte = /^B$/,
78
zero = /^0$/,
89
options = {
910
all : {
@@ -28,6 +29,7 @@
2829
function filesize (arg) {
2930
var result = "",
3031
bits = true,
32+
skip = false,
3133
i, neg, num, pos, short, size, sizes, suffix, z;
3234

3335
// Determining arguments
@@ -61,11 +63,7 @@
6163

6264
// Zero is now a special case because bytes divide by 1
6365
if ( num === 0 ) {
64-
if ( short ) {
65-
pos = 0;
66-
}
67-
68-
result = Number( 0 ).toFixed( pos ) + "B";
66+
result = Number( 0 ).toFixed( 0 ) + "B";
6967
}
7068
else {
7169
if ( bits ) {
@@ -82,9 +80,15 @@
8280
suffix = sizes[i][0];
8381

8482
if ( num >= size ) {
83+
// Treating bytes as cardinal
84+
if ( byte.test( suffix ) ) {
85+
skip = true;
86+
pos = 0;
87+
}
88+
8589
result = ( num / size ).toFixed( pos );
8690

87-
if ( short ) {
91+
if ( !skip && short ) {
8892
if ( bits && bit.test( suffix ) ) {
8993
suffix = suffix.toLowerCase();
9094
}

‎test/filesize_test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exports["filesize"] = {
1616

1717
test.equal(filesize(this.Kb), "3.91Kb", "Should be '3.91Kb'");
1818
test.equal(filesize(this.Kb, 1), "3.9Kb", "Should be '3.9Kb'");
19-
test.equal(filesize(this.Kb, 1, false), "500.0B", "Should be '500.0B'");
19+
test.equal(filesize(this.Kb, 1, false), "500B", "Should be '500B'");
2020
test.equal(filesize(this.Kb, true), "3.9k", "Should be '3.9k'");
2121
test.equal(filesize(this.Kb, true, false), "500B", "Should be '500B'");
2222

@@ -38,14 +38,14 @@ exports["filesize"] = {
3838
test.equal(filesize(this.neg, true), "-1K", "Should be '-1KB'");
3939
test.equal(filesize(this.neg, true, false), "-1K", "Should be '-1KB'");
4040

41-
test.equal(filesize(this.byte), "1.00B", "Should be '1.00B'");
42-
test.equal(filesize(this.byte, 1), "1.0B", "Should be '1.0B'");
43-
test.equal(filesize(this.byte, 1, false), "1.0B", "Should be '1.0B'");
41+
test.equal(filesize(this.byte), "1B", "Should be '1B'");
42+
test.equal(filesize(this.byte, 1), "1B", "Should be '1B'");
43+
test.equal(filesize(this.byte, 1, false), "1B", "Should be '1B'");
4444
test.equal(filesize(this.byte, true), "1B", "Should be '1B'");
4545
test.equal(filesize(this.byte, true, false), "1B", "Should be '1B'");
4646

47-
test.equal(filesize(this.zero), "0.00B", "Should be '0.00B'");
48-
test.equal(filesize(this.zero, 1), "0.0B", "Should be '0.0B'");
47+
test.equal(filesize(this.zero), "0B", "Should be '0B'");
48+
test.equal(filesize(this.zero, 1), "0B", "Should be '0B'");
4949
test.equal(filesize(this.zero, true), "0B", "Should be '0B'");
5050

5151
test.done();

0 commit comments

Comments
 (0)
Please sign in to comment.