2
0
mirror of https://github.com/tenrok/maska.git synced 2026-06-08 17:22:27 +03:00

New version code prepare

This commit is contained in:
Alexander Shabunevich
2022-12-04 16:50:18 +03:00
parent fadf78f5bb
commit 31c286bb6b
48 changed files with 15195 additions and 5502 deletions
+29
View File
@@ -0,0 +1,29 @@
import { MaskType } from './mask'
import { MaskTokens } from './tokens'
const parseJson = (value: string): any => JSON.parse(value.replaceAll("'", '"'))
export const parseOpts = (value: string): boolean =>
value !== '' ? Boolean(JSON.parse(value)) : true
export const parseMask = (value: string): MaskType =>
value.startsWith('[') && value.endsWith(']') ? parseJson(value) : value
export const parseTokens = (value: string): MaskTokens => {
if (value.startsWith('{') && value.endsWith('}')) {
return parseJson(value)
}
const tokens: MaskTokens = {}
value.split('|').forEach((token) => {
const parts = token.split(':')
tokens[parts[0]] = {
pattern: new RegExp(parts[1]),
optional: parts[2] === 'optional',
multiple: parts[2] === 'multiple',
repeated: parts[2] === 'repeated'
}
})
return tokens
}