Skip to content

Commit

Permalink
Fixed issue #11.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsillitoe committed Feb 15, 2017
1 parent 94e30dd commit ab99be7
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-currency-input",
"version": "1.2.2",
"version": "1.2.3",
"description": "React component for inputing currency amounts",
"main": "lib/index.js",
"scripts": {
Expand Down
18 changes: 18 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ const CurrencyInput = React.createClass({
}else{

if (typeof initialValue == 'string') {
// Some people, when confronted with a problem, think "I know, I'll use regular expressions."
// Now they have two problems.

// Strip out thousand separators, prefix, and suffix, etc.
if (props.thousandSeparator === "."){
// special handle the . thousand separator
initialValue = initialValue.replace(/\./g, '');
}

if (props.decimalSeparator != "."){
// fix the decimal separator
initialValue = initialValue.replace(new RegExp(props.decimalSeparator, 'g'), '.');
}

//Strip out anything that is not a digit, -, or decimal separator
initialValue = initialValue.replace(/[^0-9-.]/g, '');

// now we can parse.
initialValue = Number.parseFloat(initialValue);
}
initialValue = Number(initialValue).toLocaleString(undefined, {
Expand Down
85 changes: 85 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,91 @@ describe('react-currency-input', function(){
expect (renderedComponent.getMaskedValue()).to.equal('1,234,568')
});


it('Handles strings with separators', function() {
var renderedComponent = ReactTestUtils.renderIntoDocument(
<CurrencyInput value="1,000.01" />
);
expect (renderedComponent.getMaskedValue()).to.equal('1,000.01')
});


it('Handles strings with prefixes', function() {
var renderedComponent = ReactTestUtils.renderIntoDocument(
<CurrencyInput value="$10.01" prefix="$" />
);
expect (renderedComponent.getMaskedValue()).to.equal('$10.01')
});

it('Handles strings with suffixes', function() {
var renderedComponent = ReactTestUtils.renderIntoDocument(
<CurrencyInput value="10.01 kr" suffix=" kr" />
);
expect (renderedComponent.getMaskedValue()).to.equal('10.01 kr')
});


it('Handles strings with custom separators', function() {
var renderedComponent = ReactTestUtils.renderIntoDocument(
<CurrencyInput value="123.456.789,12" decimalSeparator="," thousandSeparator="."/>
);
expect (renderedComponent.getMaskedValue()).to.equal('123.456.789,12')
});


it("Handles 1,234,567.89 format", function() {
var renderedComponent = ReactTestUtils.renderIntoDocument(
<CurrencyInput value="1,234,567.89" decimalSeparator="." thousandSeparator=","/>
);
expect (renderedComponent.getMaskedValue()).to.equal('1,234,567.89')
});


it("Handles 1 234 567.89 format", function() {
var renderedComponent = ReactTestUtils.renderIntoDocument(
<CurrencyInput value="1,234,567.89" decimalSeparator="." thousandSeparator=" "/>
);
expect (renderedComponent.getMaskedValue()).to.equal('1 234 567.89')
});

it("Handles 1 234 567,89 format", function() {
var renderedComponent = ReactTestUtils.renderIntoDocument(
<CurrencyInput value="1 234 567,89" decimalSeparator="," thousandSeparator=" "/>
);
expect (renderedComponent.getMaskedValue()).to.equal('1 234 567,89')
});

it("Handles 1,234,567·89 format", function() {
var renderedComponent = ReactTestUtils.renderIntoDocument(
<CurrencyInput value="1,234,567·89" decimalSeparator="·" thousandSeparator=","/>
);
expect (renderedComponent.getMaskedValue()).to.equal('1,234,567·89')
});

it("Handles 1.234.567,89 format", function() {
var renderedComponent = ReactTestUtils.renderIntoDocument(
<CurrencyInput value="1.234.567,89" decimalSeparator="," thousandSeparator="."/>
);
expect (renderedComponent.getMaskedValue()).to.equal('1.234.567,89')
});

it("Handles 1˙234˙567,89 format", function() {
var renderedComponent = ReactTestUtils.renderIntoDocument(
<CurrencyInput value="1˙234˙567,89" decimalSeparator="," thousandSeparator="˙"/>
);
expect (renderedComponent.getMaskedValue()).to.equal('1˙234˙567,89')
});


it("Handles 1'234'567.89 format", function() {
var renderedComponent = ReactTestUtils.renderIntoDocument(
<CurrencyInput value="1'234'567.89" decimalSeparator="." thousandSeparator="'"/>
);
expect (renderedComponent.getMaskedValue()).to.equal("1'234'567.89")
});



});

describe('change events', function(){
Expand Down

0 comments on commit ab99be7

Please sign in to comment.