Skip to content

Commit d81fa30

Browse files
committed
small refinements to handle empty or null values
1 parent ea37a46 commit d81fa30

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

src/mask.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,23 @@ export default function mask(value, precision = 2, decimalSeparator = '.', thous
44
if (precision < 0) { precision = 0; } // precision cannot be negative
55
if (precision > 20) { precision = 20; } // precision cannot be greater than 20
66

7-
if (value === null) {
7+
if (value === null || value===undefined) {
88
return {
9-
value: '',
9+
value: 0,
1010
maskedValue: ''
1111
};
1212
}
1313

1414
value = String(value); //if the given value is a Number, let's convert into String to manipulate that
15-
15+
16+
if (value.length == 0) {
17+
return {
18+
value: 0,
19+
maskedValue: ''
20+
};
21+
}
22+
23+
1624
// extract digits. if no digits, fill in a zero.
1725
let digits = value.match(/\d/g) || ['0'];
1826

test/mask.spec.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@ import mask from '../src/mask'
44

55
describe('mask', function(){
66

7+
it('should return empty strings when value is not set"', function(){
8+
const {maskedValue, value} = mask();
9+
10+
expect(maskedValue).to.equal("");
11+
expect(value).to.equal(0);
12+
});
13+
14+
it('should return empty strings when value is empty string"', function(){
15+
const {maskedValue, value} = mask("");
16+
17+
expect(maskedValue).to.equal("");
18+
expect(value).to.equal(0);
19+
});
20+
21+
it('should return empty strings when value is null"', function(){
22+
const {maskedValue, value} = mask(null);
23+
24+
expect(maskedValue).to.equal("");
25+
expect(value).to.equal(0);
26+
});
727

828
it('should change "0" to "0.00"', function(){
929
const {maskedValue, value} = mask("0");
@@ -20,31 +40,31 @@ describe('mask', function(){
2040
});
2141

2242
it('should change "000" to "0.00"', function(){
23-
const {maskedValue, value} = mask("000")
43+
const {maskedValue, value} = mask("000");
2444
expect(maskedValue).to.equal("0.00");
2545
expect(value).to.equal(0);
2646
});
2747

2848
it('should change "0000" to "0.00"', function(){
29-
const {maskedValue, value} = mask("0000")
49+
const {maskedValue, value} = mask("0000");
3050
expect(maskedValue).to.equal("0.00");
3151
expect(value).to.equal(0);
3252
});
3353

3454
it('should change "0001" to "0.01"', function(){
35-
const {maskedValue, value} = mask("0001")
55+
const {maskedValue, value} = mask("0001");
3656
expect(maskedValue).to.equal("0.01");
3757
expect(value).to.equal(0.01);
3858
});
3959

4060
it('should change "1001" to "10.01"', function(){
41-
const {maskedValue, value} = mask("1001")
61+
const {maskedValue, value} = mask("1001");
4262
expect(maskedValue).to.equal("10.01");
4363
expect(value).to.equal(10.01);
4464
});
4565

4666
it('should change "123456789" to "1,234,567.89"', function(){
47-
const {maskedValue, value} = mask("123456789")
67+
const {maskedValue, value} = mask("123456789");
4868
expect(maskedValue).to.equal("1,234,567.89");
4969
expect(value).to.equal(1234567.89);
5070
});
@@ -77,7 +97,8 @@ describe('mask', function(){
7797

7898
it('set to string value "3" should change "123456789" to "123,456.789"', function(){
7999
const {maskedValue, value} = mask("123456789", "3");
80-
expect(mask("123456789", "3").maskedValue).to.equal("123,456.789");
100+
expect(maskedValue).to.equal("123,456.789");
101+
expect(value).to.equal(123456.789)
81102
});
82103

83104
it('set to 3 should change "123456789" to "123,456.789"', function(){
@@ -131,7 +152,6 @@ describe('mask', function(){
131152
});
132153

133154
it('0 is never negative', function(){
134-
expect(mask("", "2", ".", ",", true).maskedValue).to.equal("0.00");
135155
expect(mask("0", "2", ".", ",", true).maskedValue).to.equal("0.00");
136156
expect(mask("-0", "2", ".", ",", true).maskedValue).to.equal("0.00");
137157
expect(mask("-0-", "2", ".", ",", true).maskedValue).to.equal("0.00");

0 commit comments

Comments
 (0)