Skip to content

Support decimals #2

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Go nuts.
27 changes: 4 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -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(".");
};
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -22,8 +22,5 @@
"homepage": "https://github.com/cappslock/add-commas",
"devDependencies": {
"mocha": "^1.19.0"
},
"dependencies": {
"is-nan": "^1.0.1"
}
}
22 changes: 8 additions & 14 deletions test/addcommas.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,18 @@ 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 () {
assert.throws(function () {
addCommas();
});
});

test('fails on non-numeric strings', function () {
assert.throws(function () {
addCommas('divide by dogs');
});
})