diff --git a/README.md b/README.md index bbd57ae..d7d8224 100644 --- a/README.md +++ b/README.md @@ -4,23 +4,22 @@ 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 -``` -var addCommas = require('add-commas'); +```js +import addCommas from '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 +addCommas() // => throws Error ``` ## License -Go nuts. \ No newline at end of file +Go nuts. diff --git a/index.js b/index.js index c7c9c81..4737783 100644 --- a/index.js +++ b/index.js @@ -1,24 +1,5 @@ -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; - } +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/package.json b/package.json index 95d9026..2dda478 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "add-commas", - "version": "0.0.4", + "version": "1.0.0", "description": "Add commas to a number", "main": "index.js", "scripts": { @@ -22,8 +22,5 @@ "homepage": "https://github.com/cappslock/add-commas", "devDependencies": { "mocha": "^1.19.0" - }, - "dependencies": { - "is-nan": "^1.0.1" } } 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