mirror of
https://github.com/tenrok/maska.git
synced 2026-06-14 18:42:27 +03:00
allow custom escape character
This commit is contained in:
+14
-1
@@ -96,6 +96,19 @@ 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): {
|
||||
mask: string
|
||||
escaped: number[]
|
||||
@@ -103,7 +116,7 @@ export class Mask {
|
||||
const chars: string[] = []
|
||||
const escaped: number[] = []
|
||||
maskRaw.split('').forEach((ch, i) => {
|
||||
if (ch === '!' && maskRaw[i - 1] !== '!') {
|
||||
if (this.isEscape(ch) && !this.isEscape(maskRaw[i - 1])) {
|
||||
escaped.push(i - escaped.length)
|
||||
} else {
|
||||
chars.push(ch)
|
||||
|
||||
+2
-1
@@ -46,7 +46,8 @@ const parseTokens = (value: string): MaskTokens => {
|
||||
pattern: new RegExp(parts[1]),
|
||||
optional: parts[2] === 'optional',
|
||||
multiple: parts[2] === 'multiple',
|
||||
repeated: parts[2] === 'repeated'
|
||||
repeated: parts[2] === 'repeated',
|
||||
escape: parts[2] === 'escape'
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ interface MaskToken {
|
||||
multiple?: boolean
|
||||
optional?: boolean
|
||||
repeated?: boolean
|
||||
escape?: boolean
|
||||
transform?: (char: string) => string
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user