Skip to content

Commit

Permalink
Alter custom-mask logic to protect against backspace of char immediat…
Browse files Browse the repository at this point in the history
…ely before delimiter
  • Loading branch information
Robin Southgate committed Mar 3, 2019
1 parent a8403bd commit cef8c88
Show file tree
Hide file tree
Showing 4 changed files with 9,641 additions and 17 deletions.
12 changes: 6 additions & 6 deletions __tests__/mask/custom.mask.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ test('123 with mask 999 results 123 and raw value 123(type Number)', () => {
var mask = new CustomMask()
var options = {
mask: '999',
getRawValue: function(maskedValue, settings) {
getRawValue: function (maskedValue, settings) {
return Number(maskedValue)
}
}
Expand All @@ -137,7 +137,7 @@ test('mask with custom translation and match', () => {
var options = {
mask: '999&AAA',
translation: {
'&': function(val) {
'&': function (val) {
return ['#', '.', ':'].indexOf(val) >= 0 ? val : null
}
}
Expand All @@ -154,13 +154,13 @@ test('mask with custom translation and not match', () => {
var options = {
mask: '999&AAA',
translation: {
'&': function(val) {
return ['#', '.', ':'].indexOf(val) >= 0 ? val : ''
'&': function (val) {
return ['#', '.', ':'].indexOf(val) >= 0 ? val : '#'
}
}
}

var expected = '123ABC'
var expected = '123#ABC'
var received = mask.getValue('123|ABC', options)

expect(received).toBe(expected)
Expand All @@ -171,7 +171,7 @@ test('mask with custom translation and optionals and matching', () => {
var options = {
mask: '999***AAA&',
translation: {
'&': function(val) {
'&': function (val) {
return ['#', '.', ':'].indexOf(val) >= 0 ? val : null
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/lib/masks/custom.mask.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions lib/masks/custom.mask.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import BaseMask from './_base.mask'

function getDefaultTranslation() {
return {
'9': function(val) {
'9': function (val) {
return val.replace(/[^0-9]+/g, '')
},
A: function(val) {
A: function (val) {
return val.replace(/[^a-zA-Z]+/g, '')
},
S: function(val) {
S: function (val) {
return val.replace(/[^a-zA-Z0-9]+/g, '')
},
'*': function(val) {
'*': function (val) {
return val
}
}
Expand Down Expand Up @@ -50,14 +50,16 @@ function toPattern(value, mask, translation) {

if (translationHandler) {
const resolverValue = translationHandler(valueChar || '')

if (resolverValue !== null) {
result += resolverValue
valueCharIndex += 1
if (resolverValue === '') {
//valueChar replaced so don't add it to result, keep the mask at the same point and continue to next value char
valueCharIndex += 1;
continue;
} else if (resolverValue !== null) {
result += resolverValue;
valueCharIndex += 1;
} else {
result += maskChar
result += maskChar;
}

maskCharIndex += 1
continue
}
Expand Down
Loading

0 comments on commit cef8c88

Please sign in to comment.