mirror of
https://github.com/tenrok/maska.git
synced 2026-06-23 20:40:35 +03:00
+1
-14
@@ -96,19 +96,6 @@ export class Mask {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private isEscape (ch: string) {
|
|
||||||
if (ch === '!' && !this.opts.tokensReplace) {
|
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
for (let tokensKey in this.opts.tokens) {
|
|
||||||
if (tokensKey === ch && this.opts.tokens[tokensKey].escape) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private escapeMask (maskRaw: string): {
|
private escapeMask (maskRaw: string): {
|
||||||
mask: string
|
mask: string
|
||||||
escaped: number[]
|
escaped: number[]
|
||||||
@@ -116,7 +103,7 @@ export class Mask {
|
|||||||
const chars: string[] = []
|
const chars: string[] = []
|
||||||
const escaped: number[] = []
|
const escaped: number[] = []
|
||||||
maskRaw.split('').forEach((ch, i) => {
|
maskRaw.split('').forEach((ch, i) => {
|
||||||
if (this.isEscape(ch) && !this.isEscape(maskRaw[i - 1])) {
|
if (ch === '!' && maskRaw[i - 1] !== '!') {
|
||||||
escaped.push(i - escaped.length)
|
escaped.push(i - escaped.length)
|
||||||
} else {
|
} else {
|
||||||
chars.push(ch)
|
chars.push(ch)
|
||||||
|
|||||||
+1
-2
@@ -46,8 +46,7 @@ const parseTokens = (value: string): MaskTokens => {
|
|||||||
pattern: new RegExp(parts[1]),
|
pattern: new RegExp(parts[1]),
|
||||||
optional: parts[2] === 'optional',
|
optional: parts[2] === 'optional',
|
||||||
multiple: parts[2] === 'multiple',
|
multiple: parts[2] === 'multiple',
|
||||||
repeated: parts[2] === 'repeated',
|
repeated: parts[2] === 'repeated'
|
||||||
escape: parts[2] === 'escape'
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ interface MaskToken {
|
|||||||
multiple?: boolean
|
multiple?: boolean
|
||||||
optional?: boolean
|
optional?: boolean
|
||||||
repeated?: boolean
|
repeated?: boolean
|
||||||
escape?: boolean
|
|
||||||
transform?: (char: string) => string
|
transform?: (char: string) => string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1449,42 +1449,6 @@ describe("Optional with multiple '-9' mask", () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("Custom escape with '$#!99' mask", () => {
|
|
||||||
beforeAll(() => {
|
|
||||||
input = prepareInput({
|
|
||||||
mask: '$#!99',
|
|
||||||
tokens: {
|
|
||||||
'$': { pattern: /\$/, escape: true },
|
|
||||||
'9': { pattern: /\d/, multiple: true }
|
|
||||||
},
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
afterEach(async () => {
|
|
||||||
await user.clear(input)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('input { }', async () => {
|
|
||||||
await user.type(input, '{ }')
|
|
||||||
expect(input).toHaveValue('#9')
|
|
||||||
})
|
|
||||||
|
|
||||||
test('input 1', async () => {
|
|
||||||
await user.type(input, '1')
|
|
||||||
expect(input).toHaveValue('#91')
|
|
||||||
})
|
|
||||||
|
|
||||||
test('input 91', async () => {
|
|
||||||
await user.type(input, '91')
|
|
||||||
expect(input).toHaveValue('#91')
|
|
||||||
})
|
|
||||||
|
|
||||||
test('input 1234', async () => {
|
|
||||||
await user.type(input, '1234')
|
|
||||||
expect(input).toHaveValue('#91234')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('IP mask', () => {
|
describe('IP mask', () => {
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
input = prepareInput({
|
input = prepareInput({
|
||||||
|
|||||||
Reference in New Issue
Block a user