mirror of
https://github.com/tenrok/maska.git
synced 2026-05-15 11:59:38 +03:00
feat: use RegExp 'u' flag by default
This commit is contained in:
+4
-1
@@ -1,5 +1,6 @@
|
||||
import { MaskTokens, tokens } from './tokens'
|
||||
import { processNumber } from './number'
|
||||
import { supportsUnicodeRegex } from './parser'
|
||||
|
||||
export type MaskType = string | string[] | ((input: string) => string) | null
|
||||
|
||||
@@ -32,7 +33,9 @@ export class Mask {
|
||||
|
||||
for (const token of Object.values(opts.tokens)) {
|
||||
if (typeof token.pattern === 'string') {
|
||||
token.pattern = new RegExp(token.pattern)
|
||||
token.pattern = supportsUnicodeRegex()
|
||||
? new RegExp(token.pattern, 'u')
|
||||
: new RegExp(token.pattern)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
+10
-1
@@ -57,7 +57,7 @@ const parseTokens = (value: string): MaskTokens => {
|
||||
value.split('|').forEach((token) => {
|
||||
const parts = token.split(':')
|
||||
tokens[parts[0]] = {
|
||||
pattern: new RegExp(parts[1]),
|
||||
pattern: supportsUnicodeRegex() ? new RegExp(parts[1], 'u') : new RegExp(parts[1]),
|
||||
optional: parts[2] === 'optional',
|
||||
multiple: parts[2] === 'multiple',
|
||||
repeated: parts[2] === 'repeated'
|
||||
@@ -66,3 +66,12 @@ const parseTokens = (value: string): MaskTokens => {
|
||||
|
||||
return tokens
|
||||
}
|
||||
|
||||
export const supportsUnicodeRegex = (): boolean => {
|
||||
try {
|
||||
new RegExp('\\p{L}', 'u')
|
||||
return true
|
||||
} catch (e) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2162,3 +2162,25 @@ describe('Number mask', () => {
|
||||
expect(input).toHaveValue('1,234')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Unicode tokens mask', () => {
|
||||
test('default number', async () => {
|
||||
document.body.innerHTML = `<input id="input" data-maska="A" data-maska-tokens="A:[\\p{L}]:multiple">`
|
||||
const input = <HTMLInputElement>document.getElementById('input')
|
||||
new MaskInput(input)
|
||||
|
||||
await user.type(input, '1')
|
||||
expect(input).toHaveValue('')
|
||||
|
||||
await user.type(input, 'z')
|
||||
expect(input).toHaveValue('z')
|
||||
|
||||
await user.type(input, 'я')
|
||||
expect(input).toHaveValue('zя')
|
||||
|
||||
await user.type(input, '1')
|
||||
expect(input).toHaveValue('zя')
|
||||
|
||||
await user.clear(input)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -820,3 +820,16 @@ test('tokens replaced', () => {
|
||||
|
||||
expect(mask.unmasked('12')).toBe('1')
|
||||
})
|
||||
|
||||
test('unicode tokens', () => {
|
||||
const mask = new Mask({
|
||||
mask: 'A',
|
||||
// @ts-expect-error
|
||||
tokens: { A: { pattern: '[\\p{L}]', multiple: true } }
|
||||
})
|
||||
|
||||
expect(mask.masked('1')).toBe('')
|
||||
expect(mask.masked('z')).toBe('z')
|
||||
expect(mask.masked('zя')).toBe('zя')
|
||||
expect(mask.masked('zя1')).toBe('zя')
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user