From 4a167c20d273c0d4ffb507772ce2c9ca0fdeab65 Mon Sep 17 00:00:00 2001 From: Jason Mulligan Date: Fri, 16 Nov 2012 14:53:05 -0500 Subject: [PATCH 1/2] Adding license for proper attribution in a retail product --- LICENSE | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..af35006 --- /dev/null +++ b/LICENSE @@ -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. \ No newline at end of file From 0feae207ea268bdb8332ad1171274e79fe220f2a Mon Sep 17 00:00:00 2001 From: Jason Mulligan Date: Sat, 2 Feb 2013 14:29:25 -0500 Subject: [PATCH 2/2] Adding support for negative sizes, fixes #12 --- lib/filesize.js | 12 ++++++++---- lib/filesize.min.js | 6 +++--- package.json | 4 ++-- src/filesize.js | 8 ++++++-- test/filesize_test.js | 21 ++++++++++++--------- 5 files changed, 31 insertions(+), 20 deletions(-) diff --git a/lib/filesize.js b/lib/filesize.js index 09407e1..3cbfb27 100644 --- a/lib/filesize.js +++ b/lib/filesize.js @@ -2,11 +2,11 @@ * filesize * * @author Jason Mulligan - * @copyright Jason Mulligan 2012 + * @copyright Jason Mulligan 2013 * @license BSD-3 * @link https://github.com/avoidwork/filesize.js * @module filesize - * @version 1.7.2 + * @version 1.7.3 */ (function (global) { @@ -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]; @@ -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 = ""; @@ -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]; @@ -60,7 +64,7 @@ } } - return result; + return (neg ? "-" : "") + result; }; switch (true) { diff --git a/lib/filesize.min.js b/lib/filesize.min.js index 13adf26..f9c87d5 100644 --- a/lib/filesize.min.js +++ b/lib/filesize.min.js @@ -2,10 +2,10 @@ * filesize * * @author Jason Mulligan - * @copyright Jason Mulligan 2012 + * @copyright Jason Mulligan 2013 * @license BSD-3 * @link https://github.com/avoidwork/filesize.js * @module filesize - * @version 1.7.2 + * @version 1.7.3 */ -(function(e){"use strict";var t=function(e){var t=10,n,r,i,s,o,u,a,f,l,c,h,p,d;typeof arguments[2]!="undefined"?(o=arguments[1],f=arguments[2]):typeof arguments[1]=="boolean"?f=arguments[1]:o=arguments[1];if(isNaN(e)||typeof o!="undefined"&&isNaN(o))throw Error("Invalid arguments");f=f===!0,o=f?1:typeof o=="undefined"?2:parseInt(o,t),s=Number(e),c=[["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=c.length,a="",u=/\.(.*)/,n=/b$/,r=/^B$/,d=/^0$/;while(i--){l=c[i][1],h=c[i][0],i>3&&(l=Number(l));if(s>=l){a=(r.test(h)?s:s/l).toFixed(o),f&&(n.test(h)&&(h=h.toLowerCase()),h=h.slice(0,1),p=u.exec(a),p!==null&&typeof p[1]!="undefined"&&d.test(p[1])&&(a=parseInt(a,t))),a+=h;break}}return a};switch(!0){case typeof exports!="undefined":module.exports=t;break;case typeof define=="function":define(function(){return t});break;default:e.filesize=t}})(this); \ No newline at end of file +(function(e){"use strict";var t=function(e){var t=10,n,r,i,s,o,u,a,f,l,c,h,p,d,v;typeof arguments[2]!="undefined"?(u=arguments[1],l=arguments[2]):typeof arguments[1]=="boolean"?l=arguments[1]:u=arguments[1];if(isNaN(e)||typeof u!="undefined"&&isNaN(u))throw Error("Invalid arguments");l=l===!0,u=l?1:typeof u=="undefined"?2:parseInt(u,t),o=Number(e),s=o<0,h=[["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=h.length,f="",a=/\.(.*)/,n=/b$/,r=/^B$/,v=/^0$/,s&&(o-=o*2);while(i--){c=h[i][1],p=h[i][0],i>3&&(c=Number(c));if(o>=c){f=(r.test(p)?o:o/c).toFixed(u),l&&(n.test(p)&&(p=p.toLowerCase()),p=p.slice(0,1),d=a.exec(f),d!==null&&typeof d[1]!="undefined"&&v.test(d[1])&&(f=parseInt(f,t))),f+=p;break}}return(s?"-":"")+f};switch(!0){case typeof exports!="undefined":module.exports=t;break;case typeof define=="function":define(function(){return t});break;default:e.filesize=t}})(this); \ No newline at end of file diff --git a/package.json b/package.json index 1a578eb..ed117a3 100644 --- a/package.json +++ b/package.json @@ -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", @@ -30,5 +30,5 @@ "devDependencies": { "grunt": "~0.3.12" }, - "keywords": [] + "keywords": ["file", "filesize", "size", "readable", "filesystem"] } \ No newline at end of file diff --git a/src/filesize.js b/src/filesize.js index fd48c1a..cc8ec2f 100644 --- a/src/filesize.js +++ b/src/filesize.js @@ -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]; @@ -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 = ""; @@ -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]; @@ -49,7 +53,7 @@ } } - return result; + return (neg ? "-" : "") + result; }; switch (true) { diff --git a/test/filesize_test.js b/test/filesize_test.js index c69d609..3baef25 100644 --- a/test/filesize_test.js +++ b/test/filesize_test.js @@ -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) {