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

Commit

Permalink
Add linter and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Nov 2, 2020
1 parent 61bb749 commit ed15b0c
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 89 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Change Log
All notable changes to this project will be documented in this file.

## [0.0.6] - 2020-11-01
### Changed
- Add linter and update dependencies.

## [0.0.5] - 2016-02-11
### Changed
- Stringify input value of any type first before attempting to commafy.
File renamed without changes.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# commafy

Add commas to a number.
> Add commas to a number.
# Install
## Install

```bash
npm install commafy
```

# Usage
## Usage

```javascript
var commafy = require('commafy');
var commafy = require('commafy')

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'
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'
```

# License
## License

MIT
[MIT](LICENSE)
22 changes: 0 additions & 22 deletions bower.json

This file was deleted.

30 changes: 14 additions & 16 deletions commafy.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
(function(){

function isNumeric(val) {
;(function () {
function isNumeric (val) {
if (typeof val === 'number' && !isNaN(val)) {
return true;
return true
}

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

function commafy(val) {
function commafy (val) {
if (typeof val === 'undefined' || val === null) {
val = '';
val = ''
}

val = val.toString();
val = val.toString()

if (!isNumeric(val)) {
return val;
return val
}

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

parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
return parts.join('.');
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
return parts.join('.')
}

if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = commafy;
module.exports = commafy
} else {
window.commafy = commafy;
window.commafy = commafy
}

})();
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "commafy",
"version": "0.0.5",
"version": "0.0.6",
"description": "Add commas to a number",
"main": "commafy.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "tape test/*.js"
"test": "tape test/*.js",
"lint": "standard --fix commafy.js test/commafy.test.js"
},
"repository": {
"type": "git",
Expand All @@ -23,7 +24,9 @@
"url": "https://github.com/miguelmota/commafy/issues"
},
"homepage": "https://github.com/miguelmota/commafy",
"dependencies": {
"tape": "^3.0.3"
"dependencies": {},
"devDependencies": {
"standard": "^16.0.1",
"tape": "^5.0.1"
}
}
36 changes: 0 additions & 36 deletions test/commafy.js

This file was deleted.

36 changes: 36 additions & 0 deletions test/commafy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var test = require('tape')
var commafy = require('../commafy')

test('commafy', function (t) {
t.plan(28)

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(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')
t.equal(commafy(1e6), '1,000,000')

t.equal(commafy(), '')
t.equal(commafy(''), '')
t.equal(commafy(null), '')
t.equal(commafy([]), '')
t.equal(commafy(true), 'true')
t.equal(commafy(false), 'false')
t.equal(commafy(NaN), 'NaN')
t.equal(commafy({}), '[object Object]')
t.equal(commafy(function () {}), 'function () {}')
t.equal(commafy(Infinity), 'Infinity')
t.equal(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')
})

0 comments on commit ed15b0c

Please sign in to comment.