Comparing version 0.0.4 to 0.0.5
@@ -1,26 +0,20 @@ | ||
/* | ||
Simplest possible solution to turn numbers into nicely comma seperated amounts: | ||
1234 => 1 234 | ||
// Simplest possible solution to turn numbers into nicely seperated amounts: | ||
// 1234 => 1 234 | ||
// When addPrecision is set to false (or default is used = false), precision is stripped | ||
When addPrecision is set to false (or default is used = false), precision is stripped | ||
*/ | ||
function commaIt(number, options) { | ||
//Set up default seperators | ||
addPrecision = (options && options["addPrecision"] || false); | ||
thousandSeperator = (options && options["thousandSeperator"] || " "); | ||
decimalSeperator = (options && options["decimalSeperator"] || ","); | ||
var replacmentRegex ='$1' + thousandSeperator; | ||
addPrecision = (options && options['addPrecision'] || false); | ||
thousandSeperator = (options && options['thousandSeperator'] || ' '); | ||
decimalSeperator = (options && options['decimalSeperator'] || ','); | ||
var replacmentRegex = '$1' + thousandSeperator; | ||
//Conversion to string and default return managment | ||
number = number.toString(); | ||
if (number.length === 0) return "0" + decimalSeperator + "00"; | ||
number = number.toString(); | ||
if(number.length === 0) return '0' + decimalSeperator + '00'; | ||
//Actual parsing of two side of the number | ||
var amount = number.split(decimalSeperator)[0]; | ||
var floats = addPrecision ? (decimalSeperator + ((number.split(decimalSeperator)[1] || '') +'00').substr(0,2)) : ""; | ||
var numberified = amount.split('').reverse().join('') | ||
.replace(/(\d{3}(?!$))/g, replacmentRegex) | ||
.split('').reverse().join(''); | ||
var floats = addPrecision ? (decimalSeperator + ((number.split(decimalSeperator)[1] || '') + '00').substr(0, 2)) : ''; | ||
var numberified = amount.split('').reverse().join('').replace(/(\d{3}(?!$))/g, replacmentRegex).split('').reverse().join(''); | ||
@@ -27,0 +21,0 @@ return numberified + floats; |
{ | ||
"name": "comma-it", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Node.js module convert numbers to amounts", | ||
@@ -14,3 +14,6 @@ "main": "comma-it.js", | ||
}, | ||
"scripts" : { | ||
"test" : "node test/comma-it-test.js" | ||
}, | ||
"engines": { "node": "*" } | ||
} |
Comma-It.js | ||
=========== | ||
[![Build Status](https://travis-ci.org/AvnerCohen/comma-it.png)](https://travis-ci.org/AvnerCohen/comma-it) | ||
Tiny module to turn numbers into a more readble amounts by adding commas: | ||
@@ -8,2 +8,13 @@ > 1234.5 => 1,234.50 | ||
```js | ||
commaIt( '12341233,12', {addPrecision:true}), '12 341 233,12' | ||
``` | ||
Custom seperators: | ||
```js | ||
commaIt('12341233.12', {addPrecision:true, thousandSeperator : ',', decimalSeperator : '.'}); // => 12,341,233.12' | ||
``` | ||
At some point in time it's very easy to add support for multiple precisions and setup for precision and thousands seperator char, these are different in different parts of the world ofcourse. | ||
@@ -10,0 +21,0 @@ |
@@ -1,35 +0,35 @@ | ||
var commaIt = require("../comma-it").commaIt; | ||
var assert = require("assert"); | ||
var commaIt = require('../comma-it').commaIt; | ||
var assert = require('assert'); | ||
var zero = "0"; | ||
assert.equal(commaIt(zero), "0"); | ||
var zero = '0'; | ||
assert.equal(commaIt(zero), '0'); | ||
//## Without precision | ||
//Check simplest formatting | ||
var number = "1234"; | ||
assert.equal(commaIt(number), "1 234"); | ||
var number = '1234'; | ||
assert.equal(commaIt(number), '1 234'); | ||
//Check precision support | ||
var number1 = "1234,1234"; | ||
assert.equal(commaIt(number1), "1 234"); | ||
var number1 = '1234,1234'; | ||
assert.equal(commaIt(number1), '1 234'); | ||
//Check multi commas | ||
var number2 = "12341233,12"; | ||
assert.equal(commaIt(number2), "12 341 233"); | ||
var number2 = '12341233,12'; | ||
assert.equal(commaIt(number2), '12 341 233'); | ||
//### With Precision support | ||
//Check simplest formatting | ||
var number = "1234"; | ||
assert.equal(commaIt(number, {addPrecision:true}), "1 234,00"); | ||
var number = '1234'; | ||
assert.equal(commaIt(number, {addPrecision:true}), '1 234,00'); | ||
//Check precision support | ||
var number1 = "1234,1234"; | ||
assert.equal(commaIt(number1, {addPrecision:true}), "1 234,12"); | ||
var number1 = '1234,1234'; | ||
assert.equal(commaIt(number1, {addPrecision:true}), '1 234,12'); | ||
//Check multi commas | ||
var number2 = "12341233,12"; | ||
assert.equal(commaIt(number2, {addPrecision:true}), "12 341 233,12"); | ||
var number2 = '12341233,12'; | ||
assert.equal(commaIt(number2, {addPrecision:true}), '12 341 233,12'); | ||
@@ -39,3 +39,7 @@ | ||
//Check > 10000 (multi thousdand seperator) | ||
var number2 = "12341233.12"; | ||
assert.equal(commaIt(number2, {addPrecision:true, thousandSeperator : ",", decimalSeperator : "."}), "12,341,233.12"); | ||
var number2 = '12341233.12'; | ||
assert.equal(commaIt(number2, {addPrecision:true, thousandSeperator : ',', decimalSeperator : '.'}), '12,341,233.12'); | ||
console.log('testing done'); | ||
4477
7
24
47