Skip to content

Commit 6bea726

Browse files
authored
fix(escape): first character escaped (#49)
Allow first character to be escaped
1 parent 77a7bf0 commit 6bea726

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/masker.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ export function formatter(value, config) {
7777
const masker = tokens[maskChar]
7878
let char = value[valueIndex]
7979

80-
// no more input characters and next character is a masked one
81-
if (!char && masker) break
82-
8380
if (masker && !escaped) {
8481
// when is escape char, do not mask, just continue
8582
if (masker.escape) {
@@ -88,6 +85,9 @@ export function formatter(value, config) {
8885
continue
8986
}
9087

88+
// no more input characters and next character is a masked one
89+
if (!char) break
90+
9191
if (masker.pattern.test(char)) {
9292
char = masker.transform ? masker.transform(char) : char
9393
output.unmasked += char

tests/formatter.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ test('empty -> +1 # 5', () => {
6666
expect(formatter('', { mask: '+1 # 5', prefill: true })).toMatchObject({ masked: '+1 ', unmasked: '' })
6767
})
6868

69+
test('escaped -> \\+1 # 5', () => {
70+
expect(formatter('', { mask: '\\+1 # 5', prefill: true })).toMatchObject({ masked: '+1 ', unmasked: '' })
71+
})
72+
73+
test('2 -> \\A # 5 \\A\\A', () => {
74+
expect(formatter('2', { mask: '\\A # 5 \\A\\A', prefill: true })).toMatchObject({ masked: 'A 2 5 AA', unmasked: '2' })
75+
})
76+
6977
test('France IBAN', () => {
7078
expect(
7179
formatter('FR7630006000011234567890189', {

0 commit comments

Comments
 (0)