Skip to content
This repository has been archived by the owner on Feb 19, 2022. It is now read-only.

Commit

Permalink
Check if value is numeric
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Feb 7, 2016
1 parent f9492ed commit 271d0f7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ npm install commafy
```javascript
var commafy = require('commafy');

commafy(1000000) // '1,000,000'
console.log(commafy(1000000)); // '1,000,000'
console.log(commafy(1000)); // '1,000'
console.log(commafy(1000.123)); // '1,000.123'
console.log(commafy(100)); // '100'
console.log(commafy(1e4)); '10,000'
```

If the value is not numeric it will immediately return the value back.

# License

MIT
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "commafy",
"main": "commafy.js",
"version": "0.0.1",
"version": "0.0.3",
"homepage": "https://github.com/miguelmota/commafy",
"authors": [
"Miguel Mota <miguelmota2@gmail.com>"
Expand Down
19 changes: 16 additions & 3 deletions commafy.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
(function(){

function commafy(n) {
if (!n) { return n; }
var parts = n.toString().split('.');
function isNumeric(val) {
if (typeof val === 'number' && !isNaN(val)) {
return true;
}

val = (val||'').toString().trim();
return val && !isNaN(val);
}

function commafy(val) {
if (!isNumeric(val) || Math.abs(val) === Infinity) {
return val;
}

var parts = val.toString().split('.');

parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
return parts.join('.');
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "commafy",
"version": "0.0.2",
"version": "0.0.3",
"description": "Add commas to a number",
"main": "commafy.js",
"directories": {
Expand Down
19 changes: 17 additions & 2 deletions test/commafy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@ var test = require('tape');
var commafy = require('../commafy');

test('commafy', function (t) {
t.plan(8);
t.plan(22);

t.equal(commafy(), undefined);
t.equal(commafy(null), null);
t.deepEqual(commafy([]), []);
t.deepEqual(commafy({}), {});
t.deepEqual(commafy(Infinity), Infinity);
t.deepEqual(commafy(-Infinity), -Infinity);
t.equal(commafy('10'), '10');
t.equal(commafy('10000'), '10,000');
t.equal(commafy('10000.500'), '10,000.500');
t.equal(commafy('$58303'), '$58303');
t.equal(commafy('1024px'), '1024px');
t.equal(commafy('amount $1000.20'), 'amount $1000.20');

t.equal(commafy(0), '0');
t.equal(commafy(1000000), '1,000,000');
t.equal(commafy(1000), '1,000');
t.equal(commafy(100), '100');
t.equal(commafy(85), '85');
t.equal(commafy(10200.50), '10,200.5');
t.equal(commafy((10200.50).toFixed(3)), '10,200.500');
t.equal(commafy(1000.5023), '1,000.5023');
t.equal(commafy(14734534.53), '14,734,534.53');
t.equal(commafy((10200.50).toFixed(3)), '10,200.500');
t.equal(commafy(1e4), '10,000');
});

0 comments on commit 271d0f7

Please sign in to comment.