-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
mask.ts
87 lines (73 loc) · 2.29 KB
/
mask.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
export interface MaskItem {
test: (char: string) => boolean
convert?: (char: string) => string
}
export type MaskType = '#' | 'A' | 'a' | 'N' | 'n' | 'X'
export const defaultDelimiters = /[-!$%^&*()_+|~=`{}[\]:";'<>?,./\\ ]/
export const isMaskDelimiter = (char: string): boolean => char ? defaultDelimiters.test(char) : false
const allowedMasks: Dictionary<MaskItem> = {
'#': {
test: char => /[0-9]/.test(char)
},
'A': {
test: char => /[A-Z]/i.test(char),
convert: char => char.toUpperCase()
},
'a': {
test: char => /[a-z]/i.test(char),
convert: char => char.toLowerCase()
},
'N': {
test: char => /[0-9A-Z]/i.test(char),
convert: char => char.toUpperCase()
},
'n': {
test: char => /[0-9a-z]/i.test(char),
convert: char => char.toLowerCase()
},
'X': {
test: isMaskDelimiter
}
}
const isMask = (char: string): boolean => allowedMasks.hasOwnProperty(char)
const convert = (mask: MaskType, char: string): string => {
return allowedMasks[mask].convert ? allowedMasks[mask].convert!(char) : char
}
const maskValidates = (mask: MaskType, char: string): boolean => {
if (char == null || !isMask(mask)) return false
return allowedMasks[mask].test(char)
}
export const maskText = (text: string | null | undefined, masked: string | string[], dontFillMaskBlanks: boolean): string => {
if (text == null) return ''
text = String(text)
if (!masked.length || !text.length) return text
if (!Array.isArray(masked)) masked = masked.split('')
let textIndex = 0
let maskIndex = 0
let newText = ''
while (maskIndex < masked.length) {
const mask = masked[maskIndex]
// Assign the next character
const char = text[textIndex]
// Check if mask is delimiter
// and current char matches
if (!isMask(mask) && char === mask) {
newText += mask
textIndex++
// Check if not mask
} else if (!isMask(mask) && !dontFillMaskBlanks) {
newText += mask
// Check if is mask and validates
} else if (maskValidates(mask as MaskType, char)) {
newText += convert(mask as MaskType, char)
textIndex++
} else {
return newText
}
maskIndex++
}
return newText
}
export const unmaskText = (text: string): string => {
return text ? String(text).replace(new RegExp(defaultDelimiters, 'g'), '') : text
}