Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"react": ">=0.14.0 || >=15.0.0"
},
"devDependencies": {
"babel": "^6.5.2",
"babel-cli": "^6.18.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.5.0",
"babel-register": "^6.9.0",
Expand All @@ -50,5 +50,6 @@
"rimraf": "^2.5.2",
"sinon": "^1.17.4",
"sinon-chai": "^2.8.0"
}
},
"dependencies": {}
}
54 changes: 31 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const CurrencyInput = React.createClass({
*/
getDefaultProps(){
return {
onChange: function(maskValue){/*no-op*/},
onChange: function(maskValue, value){/*no-op*/},
value: "0",
decimalSeparator: ".",
thousandSeparator: ",",
Expand Down Expand Up @@ -67,16 +67,20 @@ const CurrencyInput = React.createClass({
delete customProps.allowNegative;
delete customProps.prefix;
delete customProps.suffix;

const { maskedValue, value } = mask(
this.props.value,
this.props.precision,
this.props.decimalSeparator,
this.props.thousandSeparator,
this.props.allowNegative,
this.props.prefix,
this.props.suffix
);

return {
maskedValue: mask(
this.props.value,
this.props.precision,
this.props.decimalSeparator,
this.props.thousandSeparator,
this.props.allowNegative,
this.props.prefix,
this.props.suffix
),
maskedValue,
value,
customProps: customProps
}
},
Expand All @@ -100,16 +104,20 @@ const CurrencyInput = React.createClass({
delete customProps.allowNegative;
delete customProps.prefix;
delete customProps.suffix;

const {maskedValue, value} = mask(
nextProps.value,
nextProps.precision,
nextProps.decimalSeparator,
nextProps.thousandSeparator,
nextProps.allowNegative,
nextProps.prefix,
nextProps.suffix
);

this.setState({
maskedValue: mask(
nextProps.value,
nextProps.precision,
nextProps.decimalSeparator,
nextProps.thousandSeparator,
nextProps.allowNegative,
nextProps.prefix,
nextProps.suffix
),
maskedValue,
value,
customProps: customProps
});
},
Expand All @@ -131,7 +139,7 @@ const CurrencyInput = React.createClass({
*/
handleChange(event){
event.preventDefault();
let maskedValue = mask(
let {maskedValue, value} = mask(
event.target.value,
this.props.precision,
this.props.decimalSeparator,
Expand All @@ -140,8 +148,8 @@ const CurrencyInput = React.createClass({
this.props.prefix,
this.props.suffix
);
this.setState({maskedValue: maskedValue});
this.props.onChange(maskedValue);
this.setState({maskedValue, value});
this.props.onChange(maskedValue, value);
},


Expand All @@ -162,4 +170,4 @@ const CurrencyInput = React.createClass({
});


export default CurrencyInput
export default CurrencyInput
7 changes: 5 additions & 2 deletions src/mask.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default function mask(value, precision, decimalSeparator, thousandSeparat

// clean up extraneous digits like leading zeros.
digits = Number(digits.join('')).toFixed(precision).split('');

const raw = Number(digits.join(''));

let decimalpos = digits.length - precision - 1; // -1 needed to position the decimal separator before the digits.
if (precision > 0) {
Expand All @@ -73,5 +73,8 @@ export default function mask(value, precision, decimalSeparator, thousandSeparat
digits.unshift('-');
}

return digits.join('').trim();
return {
value: raw,
maskedValue: digits.join('').trim()
};
}
2 changes: 1 addition & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('react-currency-input', function(){
it('should call onChange', function() {
this.inputComponent.value=123456789;
ReactTestUtils.Simulate.change(this.inputComponent);
expect(this.handleChange).to.have.been.calledWith("1,234,567.89");
expect(this.handleChange).to.have.been.calledWith("1,234,567.89", 1234567.89);
});


Expand Down
123 changes: 75 additions & 48 deletions test/mask.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,68 @@ describe('mask', function(){


it('should change "0" to "0.00"', function(){
expect(mask("0")).to.equal("0.00");
const {maskedValue, value} = mask("0");

expect(maskedValue).to.equal("0.00");
expect(value).to.equal(0);
});

it('should change "00" to "0.00"', function(){
expect(mask("00")).to.equal("0.00");
const {maskedValue, value} = mask("00");

expect(maskedValue).to.equal("0.00");
expect(value).to.equal(0);
});

it('should change "000" to "0.00"', function(){
expect(mask("000")).to.equal("0.00");
const {maskedValue, value} = mask("000")
expect(maskedValue).to.equal("0.00");
expect(value).to.equal(0);
});

it('should change "0000" to "0.00"', function(){
expect(mask("0000")).to.equal("0.00");
const {maskedValue, value} = mask("0000")
expect(maskedValue).to.equal("0.00");
expect(value).to.equal(0);
});

it('should change "0001" to "0.01"', function(){
expect(mask("0001")).to.equal("0.01");
const {maskedValue, value} = mask("0001")
expect(maskedValue).to.equal("0.01");
expect(value).to.equal(0.01);
});

it('should change "1001" to "10.01"', function(){
expect(mask("1001")).to.equal("10.01");
const {maskedValue, value} = mask("1001")
expect(maskedValue).to.equal("10.01");
expect(value).to.equal(10.01);
});

it('should change "123456789" to "1,234,567.89"', function(){
expect(mask("123456789")).to.equal("1,234,567.89");
const {maskedValue, value} = mask("123456789")
expect(maskedValue).to.equal("1,234,567.89");
expect(value).to.equal(1234567.89);
});


describe('with separators', function(){

it('decimal:"," thousand:"." should change "123456789" to "1.234.567,89"', function(){
expect(mask("123456789", 2, ",", ".")).to.equal("1.234.567,89");
const {maskedValue, value} = mask("123456789", 2, ",", ".");
expect(maskedValue).to.equal("1.234.567,89");
expect(value).to.equal(1234567.89);
});

it('zero length thousand separator should change "123456789" to "1234567.89"', function(){
expect(mask("123456789", 2, ".", "")).to.equal("1234567.89");
const {maskedValue, value} = mask("123456789", 2, ".", "");
expect(maskedValue).to.equal("1234567.89");
expect(value).to.equal(1234567.89);
});

it('zero length decimal separator should change "123456789" to "1,234,56789"', function(){
expect(mask("123456789", 2, "", ",")).to.equal("1,234,56789");
const {maskedValue, value} = mask("123456789", 2, "", ",");
expect(maskedValue).to.equal("1,234,56789");
expect(value).to.equal(1234567.89);
});

});
Expand All @@ -54,15 +76,20 @@ describe('mask', function(){
describe('with precision', function(){

it('set to string value "3" should change "123456789" to "123,456.789"', function(){
expect(mask("123456789", "3")).to.equal("123,456.789");
const {maskedValue, value} = mask("123456789", "3");
expect(mask("123456789", "3").maskedValue).to.equal("123,456.789");
});

it('set to 3 should change "123456789" to "123,456.789"', function(){
expect(mask("123456789", 3)).to.equal("123,456.789");
const {maskedValue, value} = mask("123456789", 3);
expect(maskedValue).to.equal("123,456.789");
expect(value).to.equal(123456.789);
});

it('set to 0 should change "123456789" to "123,456,789"', function(){
expect(mask("123456789", 0)).to.equal("123,456,789");
const {maskedValue, value} = mask("123456789", 0);
expect(maskedValue).to.equal("123,456,789");
expect(value).to.equal(123456789);
});

});
Expand All @@ -71,48 +98,48 @@ describe('mask', function(){
describe('negative numbers', function(){

it('all "-" should be stripped out if allowNegative is false', function(){
expect(mask("123456")).to.equal("1,234.56");
expect(mask("-123456")).to.equal("1,234.56");
expect(mask("--123456")).to.equal("1,234.56");
expect(mask("--123--456")).to.equal("1,234.56");
expect(mask("--123--456--")).to.equal("1,234.56");
expect(mask("123456").maskedValue).to.equal("1,234.56");
expect(mask("-123456").maskedValue).to.equal("1,234.56");
expect(mask("--123456").maskedValue).to.equal("1,234.56");
expect(mask("--123--456").maskedValue).to.equal("1,234.56");
expect(mask("--123--456--").maskedValue).to.equal("1,234.56");
});

it('single "-" anywhere in the string should result in a negative number', function(){
expect(mask("-123456", "2", ".", ",", true)).to.equal("-1,234.56");
expect(mask("123-456", "2", ".", ",", true)).to.equal("-1,234.56");
expect(mask("123456-", "2", ".", ",", true)).to.equal("-1,234.56");
expect(mask("-123456", "2", ".", ",", true).maskedValue).to.equal("-1,234.56");
expect(mask("123-456", "2", ".", ",", true).maskedValue).to.equal("-1,234.56");
expect(mask("123456-", "2", ".", ",", true).maskedValue).to.equal("-1,234.56");
});

it('no or even number of "-" should result in a positive number', function(){
expect(mask("123456", "2", ".", ",", true)).to.equal("1,234.56");
expect(mask("--123456", "2", ".", ",", true)).to.equal("1,234.56");
expect(mask("123--456", "2", ".", ",", true)).to.equal("1,234.56");
expect(mask("123456--", "2", ".", ",", true)).to.equal("1,234.56");
expect(mask("--123456--", "2", ".", ",", true)).to.equal("1,234.56");
expect(mask("--123--456--", "2", ".", ",", true)).to.equal("1,234.56");
expect(mask("--1--234--56--", "2", ".", ",", true)).to.equal("1,234.56");
expect(mask("123456", "2", ".", ",", true).maskedValue).to.equal("1,234.56");
expect(mask("--123456", "2", ".", ",", true).maskedValue).to.equal("1,234.56");
expect(mask("123--456", "2", ".", ",", true).maskedValue).to.equal("1,234.56");
expect(mask("123456--", "2", ".", ",", true).maskedValue).to.equal("1,234.56");
expect(mask("--123456--", "2", ".", ",", true).maskedValue).to.equal("1,234.56");
expect(mask("--123--456--", "2", ".", ",", true).maskedValue).to.equal("1,234.56");
expect(mask("--1--234--56--", "2", ".", ",", true).maskedValue).to.equal("1,234.56");
});

it('odd number of "-" should result in a negative number', function(){
expect(mask("-123456", "2", ".", ",", true)).to.equal("-1,234.56");
expect(mask("123-456", "2", ".", ",", true)).to.equal("-1,234.56");
expect(mask("123456-", "2", ".", ",", true)).to.equal("-1,234.56");
expect(mask("-123-456-", "2", ".", ",", true)).to.equal("-1,234.56");
expect(mask("-1-23-45-6-", "2", ".", ",", true)).to.equal("-1,234.56");
expect(mask("-1-2-3-4-5-6-", "2", ".", ",", true)).to.equal("-1,234.56");
expect(mask("-123456", "2", ".", ",", true).maskedValue).to.equal("-1,234.56");
expect(mask("123-456", "2", ".", ",", true).maskedValue).to.equal("-1,234.56");
expect(mask("123456-", "2", ".", ",", true).maskedValue).to.equal("-1,234.56");
expect(mask("-123-456-", "2", ".", ",", true).maskedValue).to.equal("-1,234.56");
expect(mask("-1-23-45-6-", "2", ".", ",", true).maskedValue).to.equal("-1,234.56");
expect(mask("-1-2-3-4-5-6-", "2", ".", ",", true).maskedValue).to.equal("-1,234.56");
});

it('0 is never negative', function(){
expect(mask("", "2", ".", ",", true)).to.equal("0.00");
expect(mask("0", "2", ".", ",", true)).to.equal("0.00");
expect(mask("-0", "2", ".", ",", true)).to.equal("0.00");
expect(mask("-0-", "2", ".", ",", true)).to.equal("0.00");
expect(mask("--0-", "2", ".", ",", true)).to.equal("0.00");
expect(mask("", "2", ".", ",", true).maskedValue).to.equal("0.00");
expect(mask("0", "2", ".", ",", true).maskedValue).to.equal("0.00");
expect(mask("-0", "2", ".", ",", true).maskedValue).to.equal("0.00");
expect(mask("-0-", "2", ".", ",", true).maskedValue).to.equal("0.00");
expect(mask("--0-", "2", ".", ",", true).maskedValue).to.equal("0.00");
});

it('just "-" should result in 0.00', function(){
expect(mask("-", "2", ".", ",", true)).to.equal("0.00");
expect(mask("-", "2", ".", ",", true).maskedValue).to.equal("0.00");
});

});
Expand All @@ -122,30 +149,30 @@ describe('mask', function(){
describe('with currency symbol', function(){

it('"$" prefix should change "0" to "$0.00"', function(){
expect(mask("0","2",".",",",true,"$","")).to.equal("$0.00");
expect(mask("0","2",".",",",true,"$","").maskedValue).to.equal("$0.00");
});

it('"kr" suffix should change "0" to "0.00kr"', function(){
expect(mask("0","2",".",",",true,"","kr")).to.equal("0.00kr");
expect(mask("0","2",".",",",true,"","kr").maskedValue).to.equal("0.00kr");
});

it('can have both a prefix and a suffix', function(){
expect(mask("0","2",".",",",true,"$","kr")).to.equal("$0.00kr");
expect(mask("0","2",".",",",true,"$","kr").maskedValue).to.equal("$0.00kr");
});

it('does not strip whitespaces between amount and symbol', function(){
expect(mask("0","2",".",",",true,"$ ","")).to.equal("$ 0.00");
expect(mask("0","2",".",",",true,""," kr")).to.equal("0.00 kr");
expect(mask("0","2",".",",",true,"$ ","").maskedValue).to.equal("$ 0.00");
expect(mask("0","2",".",",",true,""," kr").maskedValue).to.equal("0.00 kr");
});

it('strips whitespaces before and after value', function(){
expect(mask("0","2",".",",",true," $ ","")).to.equal("$ 0.00");
expect(mask("0","2",".",",",true,""," kr ")).to.equal("0.00 kr");
expect(mask("0","2",".",",",true," $ ","").maskedValue).to.equal("$ 0.00");
expect(mask("0","2",".",",",true,""," kr ").maskedValue).to.equal("0.00 kr");
});


it('"-" should come before the prefix', function(){
expect(mask("-20.00","2",".",",",true,"$","")).to.equal("-$20.00");
expect(mask("-20.00","2",".",",",true,"$","").maskedValue).to.equal("-$20.00");
});

});
Expand Down
Loading