Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactoring #13

Merged
merged 2 commits into from
Feb 2, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Copyright (c) 2012, 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:

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.
12 changes: 8 additions & 4 deletions lib/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* filesize
*
* @author Jason Mulligan <[email protected]>
* @copyright Jason Mulligan 2012
* @copyright Jason Mulligan 2013
* @license BSD-3 <http://opensource.org/licenses/BSD-3-Clause>
* @link https://github.com/avoidwork/filesize.js
* @module filesize
* @version 1.7.2
* @version 1.7.3
*/

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

if (typeof arguments[2] !== "undefined") {
pos = arguments[1];
Expand All @@ -35,6 +35,7 @@
short = (short === true);
pos = short ? 1 : (typeof pos === "undefined" ? 2 : parseInt(pos, base));
num = Number(arg);
neg = (num < 0);
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"]];
i = sizes.length;
result = "";
Expand All @@ -43,6 +44,9 @@
byte = /^B$/;
zero = /^0$/;

// Flipping a negative number to determine the size
if (neg) num = num - (num * 2);

while (i--) {
size = sizes[i][1];
suffix = sizes[i][0];
Expand All @@ -60,7 +64,7 @@
}
}

return result;
return (neg ? "-" : "") + result;
};

switch (true) {
Expand Down
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.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "filesize",
"description": "JavaScript library to generate a human readable String describing the file size",
"version": "1.7.2",
"version": "1.7.3",
"homepage": "https://github.com/avoidwork/filesize.js",
"author": {
"name": "Jason Mulligan",
Expand Down Expand Up @@ -30,5 +30,5 @@
"devDependencies": {
"grunt": "~0.3.12"
},
"keywords": []
"keywords": ["file", "filesize", "size", "readable", "filesystem"]
}
8 changes: 6 additions & 2 deletions src/filesize.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
var filesize = function (arg) {
var base = 10,
bit, byte, i, num, pos, regex, result, short, size, sizes, suffix, z, zero;
bit, byte, i, neg, num, pos, regex, result, short, size, sizes, suffix, z, zero;

if (typeof arguments[2] !== "undefined") {
pos = arguments[1];
Expand All @@ -24,6 +24,7 @@
short = (short === true);
pos = short ? 1 : (typeof pos === "undefined" ? 2 : parseInt(pos, base));
num = Number(arg);
neg = (num < 0);
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"]];
i = sizes.length;
result = "";
Expand All @@ -32,6 +33,9 @@
byte = /^B$/;
zero = /^0$/;

// Flipping a negative number to determine the size
if (neg) num = num - (num * 2);

while (i--) {
size = sizes[i][1];
suffix = sizes[i][0];
Expand All @@ -49,7 +53,7 @@
}
}

return result;
return (neg ? "-" : "") + result;
};

switch (true) {
Expand Down
21 changes: 12 additions & 9 deletions test/filesize_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ exports["filesize"] = {
this.str = "1024";
this.invld = "abc";
this.Kb = 500;
this.neg = -1024;
done();
},
valid: function (test) {
test.expect(8);
test.equal(filesize(this.Kb), "3.91Kb", "Should match");
test.equal(filesize(this.Kb,true), "3.9k", "Should match");
test.equal(filesize(this.num), "1.00KB", "Should match");
test.equal(filesize(this.str), "1.00KB", "Should match");
test.equal(filesize(this.num, 1), "1.0KB", "Should match");
test.equal(filesize(this.str, 1), "1.0KB", "Should match");
test.equal(filesize(this.num, true), "1K", "Should match");
test.equal(filesize(this.str, true), "1K", "Should match");
test.expect(10);
test.equal(filesize(this.Kb), "3.91Kb", "Should match");
test.equal(filesize(this.Kb,true), "3.9k", "Should match");
test.equal(filesize(this.num), "1.00KB", "Should match");
test.equal(filesize(this.str), "1.00KB", "Should match");
test.equal(filesize(this.num, 1), "1.0KB", "Should match");
test.equal(filesize(this.str, 1), "1.0KB", "Should match");
test.equal(filesize(this.num, true), "1K", "Should match");
test.equal(filesize(this.str, true), "1K", "Should match");
test.equal(filesize(this.neg), "-1.00KB", "Should match");
test.equal(filesize(this.neg, true), "-1K", "Should match");
test.done();
},
invalid: function (test) {
Expand Down