Skip to content

Commit 228844f

Browse files
committed
Fixed bug in numbers with left zeros and removed support to placeholders.
1 parent 3cb94cf commit 228844f

File tree

2 files changed

+11
-28
lines changed

2 files changed

+11
-28
lines changed

src/string-mask.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var StringMask = (function() {
22
var tokens = {
3-
'0': {pattern: /\d/},
3+
'0': {pattern: /\d/, default: '0'},
44
'9': {pattern: /\d/, optional: true},
55
'#': {pattern: /\d/, optional: true, recursive: true},
66
'S': {pattern: /[a-zA-Z]/},
@@ -40,10 +40,8 @@ var StringMask = (function() {
4040
var StringMask = function(pattern, opt) {
4141
this.options = opt || {};
4242
this.options = {
43-
continuewheninvalid: this.options.continuewheninvalid || false,
4443
reverse: this.options.reverse || false,
45-
placeholder: this.options.placeholder === undefined ||
46-
this.options.placeholder === null ? '' : this.options.placeholder
44+
usedefaults: this.options.usedefaults || this.options.reverse
4745
};
4846
this.pattern = pattern;
4947

@@ -136,13 +134,11 @@ var StringMask = (function() {
136134
} else if (token.pattern.test(vc)) {
137135
formatted = concatChar(formatted, vc, this.options);
138136
valuePos = valuePos + steps.inc;
139-
} else if (!this.options.continuewheninvalid) {
140-
valid = false;
141-
break;
137+
} else if (!vc && token.default && this.options.usedefaults) {
138+
formatted = concatChar(formatted, token.default, this.options);
142139
} else {
143-
formatted = concatChar(formatted, this.options.placeholder, this.options);
144-
valuePos = valuePos + steps.inc;
145140
valid = false;
141+
break;
146142
}
147143
}
148144

src/string-mask.test.js

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ describe('mask-formatter', function(){
115115
test(p);
116116
done();
117117
});
118+
it('reverse \'R$ #.##0,00\' should format \'1\' to \'R$ 0,01\'', function(done) {
119+
p.text = '1';
120+
p.expected = 'R$ 0,01';
121+
test(p);
122+
done();
123+
});
118124
it('reverse \'R$ #.##0,00\' should format \'123a4567810\' to \'45.678,10\' and be invalid', function(done) {
119125
p.text = '123a4567810';
120126
p.expected = '45.678,10';
@@ -156,25 +162,6 @@ describe('mask-formatter', function(){
156162
test(p);
157163
done();
158164
});
159-
it('\'000.000.000-00\' should format \'12a\' to \'12..-\'', function(done) {
160-
p.options = {reverse: false, continuewheninvalid: true};
161-
p.text = '12a';
162-
p.expected = '12..-';
163-
p.valid = false;
164-
test(p);
165-
done();
166-
});
167-
it('\'000.000.000-00\' should format \'1234\' to \'123.4__.___-__\'', function(done) {
168-
p = {
169-
text: '1234',
170-
pattern: '000.000.000-00',
171-
expected: '123.4__.___-__',
172-
valid: false,
173-
options: {placeholder: '_', continuewheninvalid: true}
174-
};
175-
test(p);
176-
done();
177-
});
178165
});
179166

180167
describe('Date:', function() {

0 commit comments

Comments
 (0)