This repository has been archived by the owner on Feb 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61bb749
commit ed15b0c
Showing
8 changed files
with
72 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) |