Skip to content

Commit ab99be7

Browse files
committed
Fixed issue #11.
1 parent 94e30dd commit ab99be7

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-currency-input",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "React component for inputing currency amounts",
55
"main": "lib/index.js",
66
"scripts": {

src/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ const CurrencyInput = React.createClass({
6767
}else{
6868

6969
if (typeof initialValue == 'string') {
70+
// Some people, when confronted with a problem, think "I know, I'll use regular expressions."
71+
// Now they have two problems.
72+
73+
// Strip out thousand separators, prefix, and suffix, etc.
74+
if (props.thousandSeparator === "."){
75+
// special handle the . thousand separator
76+
initialValue = initialValue.replace(/\./g, '');
77+
}
78+
79+
if (props.decimalSeparator != "."){
80+
// fix the decimal separator
81+
initialValue = initialValue.replace(new RegExp(props.decimalSeparator, 'g'), '.');
82+
}
83+
84+
//Strip out anything that is not a digit, -, or decimal separator
85+
initialValue = initialValue.replace(/[^0-9-.]/g, '');
86+
87+
// now we can parse.
7088
initialValue = Number.parseFloat(initialValue);
7189
}
7290
initialValue = Number(initialValue).toLocaleString(undefined, {

test/index.spec.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,91 @@ describe('react-currency-input', function(){
145145
expect (renderedComponent.getMaskedValue()).to.equal('1,234,568')
146146
});
147147

148+
149+
it('Handles strings with separators', function() {
150+
var renderedComponent = ReactTestUtils.renderIntoDocument(
151+
<CurrencyInput value="1,000.01" />
152+
);
153+
expect (renderedComponent.getMaskedValue()).to.equal('1,000.01')
154+
});
155+
156+
157+
it('Handles strings with prefixes', function() {
158+
var renderedComponent = ReactTestUtils.renderIntoDocument(
159+
<CurrencyInput value="$10.01" prefix="$" />
160+
);
161+
expect (renderedComponent.getMaskedValue()).to.equal('$10.01')
162+
});
163+
164+
it('Handles strings with suffixes', function() {
165+
var renderedComponent = ReactTestUtils.renderIntoDocument(
166+
<CurrencyInput value="10.01 kr" suffix=" kr" />
167+
);
168+
expect (renderedComponent.getMaskedValue()).to.equal('10.01 kr')
169+
});
170+
171+
172+
it('Handles strings with custom separators', function() {
173+
var renderedComponent = ReactTestUtils.renderIntoDocument(
174+
<CurrencyInput value="123.456.789,12" decimalSeparator="," thousandSeparator="."/>
175+
);
176+
expect (renderedComponent.getMaskedValue()).to.equal('123.456.789,12')
177+
});
178+
179+
180+
it("Handles 1,234,567.89 format", function() {
181+
var renderedComponent = ReactTestUtils.renderIntoDocument(
182+
<CurrencyInput value="1,234,567.89" decimalSeparator="." thousandSeparator=","/>
183+
);
184+
expect (renderedComponent.getMaskedValue()).to.equal('1,234,567.89')
185+
});
186+
187+
188+
it("Handles 1 234 567.89 format", function() {
189+
var renderedComponent = ReactTestUtils.renderIntoDocument(
190+
<CurrencyInput value="1,234,567.89" decimalSeparator="." thousandSeparator=" "/>
191+
);
192+
expect (renderedComponent.getMaskedValue()).to.equal('1 234 567.89')
193+
});
194+
195+
it("Handles 1 234 567,89 format", function() {
196+
var renderedComponent = ReactTestUtils.renderIntoDocument(
197+
<CurrencyInput value="1 234 567,89" decimalSeparator="," thousandSeparator=" "/>
198+
);
199+
expect (renderedComponent.getMaskedValue()).to.equal('1 234 567,89')
200+
});
201+
202+
it("Handles 1,234,567·89 format", function() {
203+
var renderedComponent = ReactTestUtils.renderIntoDocument(
204+
<CurrencyInput value="1,234,567·89" decimalSeparator="·" thousandSeparator=","/>
205+
);
206+
expect (renderedComponent.getMaskedValue()).to.equal('1,234,567·89')
207+
});
208+
209+
it("Handles 1.234.567,89 format", function() {
210+
var renderedComponent = ReactTestUtils.renderIntoDocument(
211+
<CurrencyInput value="1.234.567,89" decimalSeparator="," thousandSeparator="."/>
212+
);
213+
expect (renderedComponent.getMaskedValue()).to.equal('1.234.567,89')
214+
});
215+
216+
it("Handles 1˙234˙567,89 format", function() {
217+
var renderedComponent = ReactTestUtils.renderIntoDocument(
218+
<CurrencyInput value="1˙234˙567,89" decimalSeparator="," thousandSeparator="˙"/>
219+
);
220+
expect (renderedComponent.getMaskedValue()).to.equal('1˙234˙567,89')
221+
});
222+
223+
224+
it("Handles 1'234'567.89 format", function() {
225+
var renderedComponent = ReactTestUtils.renderIntoDocument(
226+
<CurrencyInput value="1'234'567.89" decimalSeparator="." thousandSeparator="'"/>
227+
);
228+
expect (renderedComponent.getMaskedValue()).to.equal("1'234'567.89")
229+
});
230+
231+
232+
148233
});
149234

150235
describe('change events', function(){

0 commit comments

Comments
 (0)