Skip to content

Commit 147fa92

Browse files
committed
Merge pull request #13 from avoidwork/edge
Minor refactoring
2 parents e1a3ea5 + 0feae20 commit 147fa92

File tree

5 files changed

+31
-20
lines changed

5 files changed

+31
-20
lines changed

lib/filesize.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
* filesize
33
*
44
* @author Jason Mulligan <[email protected]>
5-
* @copyright Jason Mulligan 2012
5+
* @copyright Jason Mulligan 2013
66
* @license BSD-3 <http://opensource.org/licenses/BSD-3-Clause>
77
* @link https://github.com/avoidwork/filesize.js
88
* @module filesize
9-
* @version 1.7.2
9+
* @version 1.7.3
1010
*/
1111

1212
(function (global) {
@@ -22,7 +22,7 @@
2222
*/
2323
var filesize = function (arg) {
2424
var base = 10,
25-
bit, byte, i, num, pos, regex, result, short, size, sizes, suffix, z, zero;
25+
bit, byte, i, neg, num, pos, regex, result, short, size, sizes, suffix, z, zero;
2626

2727
if (typeof arguments[2] !== "undefined") {
2828
pos = arguments[1];
@@ -35,6 +35,7 @@
3535
short = (short === true);
3636
pos = short ? 1 : (typeof pos === "undefined" ? 2 : parseInt(pos, base));
3737
num = Number(arg);
38+
neg = (num < 0);
3839
sizes = [["B", 0], ["Kb", 128], ["KB", 1024], ["Mb", 131072], ["MB", "1.049e+6"], ["Gb", "1.342e+8"], ["GB", "1.074e+9"], ["Tb", "1.374e+11"], ["TB", "1.1e+12"], ["Pb", "1.407e+14"], ["PB", "1.126e+15"]];
3940
i = sizes.length;
4041
result = "";
@@ -43,6 +44,9 @@
4344
byte = /^B$/;
4445
zero = /^0$/;
4546

47+
// Flipping a negative number to determine the size
48+
if (neg) num = num - (num * 2);
49+
4650
while (i--) {
4751
size = sizes[i][1];
4852
suffix = sizes[i][0];
@@ -60,7 +64,7 @@
6064
}
6165
}
6266

63-
return result;
67+
return (neg ? "-" : "") + result;
6468
};
6569

6670
switch (true) {

lib/filesize.min.js

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

package.json

+2-2
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.7.2",
4+
"version": "1.7.3",
55
"homepage": "https://github.com/avoidwork/filesize.js",
66
"author": {
77
"name": "Jason Mulligan",
@@ -30,5 +30,5 @@
3030
"devDependencies": {
3131
"grunt": "~0.3.12"
3232
},
33-
"keywords": []
33+
"keywords": ["file", "filesize", "size", "readable", "filesystem"]
3434
}

src/filesize.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
var filesize = function (arg) {
1313
var base = 10,
14-
bit, byte, i, num, pos, regex, result, short, size, sizes, suffix, z, zero;
14+
bit, byte, i, neg, num, pos, regex, result, short, size, sizes, suffix, z, zero;
1515

1616
if (typeof arguments[2] !== "undefined") {
1717
pos = arguments[1];
@@ -24,6 +24,7 @@
2424
short = (short === true);
2525
pos = short ? 1 : (typeof pos === "undefined" ? 2 : parseInt(pos, base));
2626
num = Number(arg);
27+
neg = (num < 0);
2728
sizes = [["B", 0], ["Kb", 128], ["KB", 1024], ["Mb", 131072], ["MB", "1.049e+6"], ["Gb", "1.342e+8"], ["GB", "1.074e+9"], ["Tb", "1.374e+11"], ["TB", "1.1e+12"], ["Pb", "1.407e+14"], ["PB", "1.126e+15"]];
2829
i = sizes.length;
2930
result = "";
@@ -32,6 +33,9 @@
3233
byte = /^B$/;
3334
zero = /^0$/;
3435

36+
// Flipping a negative number to determine the size
37+
if (neg) num = num - (num * 2);
38+
3539
while (i--) {
3640
size = sizes[i][1];
3741
suffix = sizes[i][0];
@@ -49,7 +53,7 @@
4953
}
5054
}
5155

52-
return result;
56+
return (neg ? "-" : "") + result;
5357
};
5458

5559
switch (true) {

test/filesize_test.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@ exports["filesize"] = {
66
this.str = "1024";
77
this.invld = "abc";
88
this.Kb = 500;
9+
this.neg = -1024;
910
done();
1011
},
1112
valid: function (test) {
12-
test.expect(8);
13-
test.equal(filesize(this.Kb), "3.91Kb", "Should match");
14-
test.equal(filesize(this.Kb,true), "3.9k", "Should match");
15-
test.equal(filesize(this.num), "1.00KB", "Should match");
16-
test.equal(filesize(this.str), "1.00KB", "Should match");
17-
test.equal(filesize(this.num, 1), "1.0KB", "Should match");
18-
test.equal(filesize(this.str, 1), "1.0KB", "Should match");
19-
test.equal(filesize(this.num, true), "1K", "Should match");
20-
test.equal(filesize(this.str, true), "1K", "Should match");
13+
test.expect(10);
14+
test.equal(filesize(this.Kb), "3.91Kb", "Should match");
15+
test.equal(filesize(this.Kb,true), "3.9k", "Should match");
16+
test.equal(filesize(this.num), "1.00KB", "Should match");
17+
test.equal(filesize(this.str), "1.00KB", "Should match");
18+
test.equal(filesize(this.num, 1), "1.0KB", "Should match");
19+
test.equal(filesize(this.str, 1), "1.0KB", "Should match");
20+
test.equal(filesize(this.num, true), "1K", "Should match");
21+
test.equal(filesize(this.str, true), "1K", "Should match");
22+
test.equal(filesize(this.neg), "-1.00KB", "Should match");
23+
test.equal(filesize(this.neg, true), "-1K", "Should match");
2124
test.done();
2225
},
2326
invalid: function (test) {

0 commit comments

Comments
 (0)