From 0cedf71acdfb7d5139f36a92786f58ee33f042f5 Mon Sep 17 00:00:00 2001 From: Gabriel Florit Date: Fri, 25 Dec 2015 23:48:15 -0500 Subject: [PATCH 1/5] Make it work with decimals. --- index.js | 28 +++++----------------------- test/addcommas.js | 22 ++++++++-------------- 2 files changed, 13 insertions(+), 37 deletions(-) diff --git a/index.js b/index.js index c7c9c81..151d0a0 100644 --- a/index.js +++ b/index.js @@ -1,24 +1,6 @@ -var isNaN = require('is-nan'); - -module.exports = function (n) { - var nInt = parseInt(n, 10); - - if (isNaN(nInt)) { - throw new Error('not a number'); - } - else if (nInt.toString() !== n.toString()) { - throw new Error('not an integer'); - } - else { - var nStr = n.toString(); - var x = nStr.split('.'); - var x1 = x[0]; - var x2 = x.length > 1 ? '.' + x[1] : ''; - var rgx = /(\d+)(\d{3})/; - - while (rgx.test(x1)) { - x1 = x1.replace(rgx, '$1' + ',' + '$2'); - } - return x1 + x2; - } +// from http://stackoverflow.com/a/2901298/64372 +module.exports = function(n) { + var parts = n.toString().split("."); + parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ","); + return parts.join("."); }; diff --git a/test/addcommas.js b/test/addcommas.js index 8d064b3..7f09e26 100644 --- a/test/addcommas.js +++ b/test/addcommas.js @@ -21,14 +21,14 @@ test('works with strings', function () { assert.equal(addCommas('1234567'), '1,234,567', 'seven digits should match'); }); -test('fails on decimals', function () { - assert.throws(function () { - addCommas(123.45); - }, 'should throw error on decimals'); - - assert.throws(function () { - addCommas('123.45'); - }, 'should throw error on decimal strings'); +test('works with decimals', function () { + assert.equal(addCommas('1.0123'), '1.0123', 'single digits with decimals should match'); + assert.equal(addCommas('12.0123'), '12.0123', 'double digits with decimals should match'); + assert.equal(addCommas('123.0123'), '123.0123', 'triple digits with decimals should match'); + assert.equal(addCommas('1234.0123'), '1,234.0123', 'four digits with decimals should match'); + assert.equal(addCommas('12345.0123'), '12,345.0123', 'five digits with decimals should match'); + assert.equal(addCommas('123456.0123'), '123,456.0123', 'six digits with decimals should match'); + assert.equal(addCommas('1234567.0123'), '1,234,567.0123', 'seven digits with decimals should match'); }); test('fails on null or undefined', function () { @@ -36,9 +36,3 @@ test('fails on null or undefined', function () { addCommas(); }); }); - -test('fails on non-numeric strings', function () { - assert.throws(function () { - addCommas('divide by dogs'); - }); -}) \ No newline at end of file From 68b0721b671d23f270f07ae1ba9dd5bd4014606b Mon Sep 17 00:00:00 2001 From: Gabriel Florit Date: Sat, 26 Dec 2015 00:02:46 -0500 Subject: [PATCH 2/5] No need for is-nan, since we're not going to bother checking whether something is a number or not. --- package.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/package.json b/package.json index 95d9026..8fb8974 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,5 @@ "homepage": "https://github.com/cappslock/add-commas", "devDependencies": { "mocha": "^1.19.0" - }, - "dependencies": { - "is-nan": "^1.0.1" } } From 5598f1c512113164682a892ebcf03636b66b105f Mon Sep 17 00:00:00 2001 From: Gabriel Florit Date: Sat, 26 Dec 2015 00:06:55 -0500 Subject: [PATCH 3/5] Add decimal usage to README --- README.md | 11 +++++------ index.js | 1 - package.json | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index bbd57ae..7ee97d7 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Add commas to a number ## Credit -Core logic adapted from [this thread on Stack Overflow](http://stackoverflow.com/questions/14636536/how-to-check-if-a-variable-is-an-integer-in-javascript) +Core logic adapted from [this answer on Stack Overflow](http://stackoverflow.com/a/2901298/64372) ## Usage @@ -12,15 +12,14 @@ Core logic adapted from [this thread on Stack Overflow](http://stackoverflow.com var addCommas = require('add-commas'); // yes -addCommas(12345); // 12,345 -addCommas('12345'); // 12,345 +addCommas(12345); // 12,345 +addCommas('12345'); // 12,345 +addCommas(12345.67); // 12,345.67 // no addCommas(); // throws Error -addCommas(123.45) // throws Error -addCommas('dogs') // throws Error ``` ## License -Go nuts. \ No newline at end of file +Go nuts. diff --git a/index.js b/index.js index 151d0a0..4737783 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,3 @@ -// from http://stackoverflow.com/a/2901298/64372 module.exports = function(n) { var parts = n.toString().split("."); parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ","); diff --git a/package.json b/package.json index 8fb8974..558e195 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "add-commas", - "version": "0.0.4", + "version": "0.0.5", "description": "Add commas to a number", "main": "index.js", "scripts": { From ca1e61f167307b6156afd22cec41c92eefee51f4 Mon Sep 17 00:00:00 2001 From: Gabriel Florit Date: Sat, 26 Dec 2015 00:35:49 -0500 Subject: [PATCH 4/5] 1.0.0 because of the breaking change to not error on strings --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 558e195..2dda478 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "add-commas", - "version": "0.0.5", + "version": "1.0.0", "description": "Add commas to a number", "main": "index.js", "scripts": { From 878aeb49f9e1fc77d1beebc661a5bad782e8a773 Mon Sep 17 00:00:00 2001 From: Gabriel Florit Date: Tue, 13 Sep 2016 15:37:53 -0400 Subject: [PATCH 5/5] Update README.md --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7ee97d7..d7d8224 100644 --- a/README.md +++ b/README.md @@ -8,16 +8,16 @@ Core logic adapted from [this answer on Stack Overflow](http://stackoverflow.com ## Usage -``` -var addCommas = require('add-commas'); +```js +import addCommas from 'add-commas' // yes -addCommas(12345); // 12,345 -addCommas('12345'); // 12,345 -addCommas(12345.67); // 12,345.67 +addCommas(12345) // => 12,345 +addCommas('12345') // => 12,345 +addCommas(12345.67) // => 12,345.67 // no -addCommas(); // throws Error +addCommas() // => throws Error ``` ## License