-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
74 lines (62 loc) · 1.79 KB
/
index.js
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
const DIGIT = "9";
const ALPHA = "A";
const ALPHANUM = "S";
const ALL = "*";
function msk(value, mask) {
const removeExceedingChars =
arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
if (!value || !mask) return "";
value = value.toString();
for (var i = 0; i < value.length; i++) {
const maskToken = mask[i];
// If the character is OK with the mask
if (maskToken && match(value[i], maskToken)) {
continue;
}
// If it's there's a space or a non-word character in the mask
// just insert the character inbetween the value
if (isSpaceOrNonWordChar(maskToken)) {
const firstPart = value.slice(0, i);
const secondPart = trimLeft(value.slice(i));
value = firstPart + maskToken + secondPart;
value = msk(value, mask, removeExceedingChars);
break;
}
if (removeExceedingChars) {
// If it doesn't match, remove the character
const firstPart = value.slice(0, i);
const secondPart = value.slice(i + 1);
value = firstPart + secondPart;
value = msk(value, mask, removeExceedingChars);
}
}
return value;
}
function fit(value, mask) {
return msk(value, mask, true);
}
function match(char, token) {
switch (token) {
case DIGIT:
return /[0-9]/.test(char);
case ALPHA:
return /[A-ú]/.test(char);
case ALPHANUM:
return /[A-ú0-9]/.test(char);
case ALL:
return true;
default:
return new RegExp(escapeRegExp(token)).test(char);
}
}
function isSpaceOrNonWordChar(maskToken) {
return maskToken === " " || new RegExp(/\W/).test(maskToken);
}
function trimLeft(str) {
return str.replace(/^\s+/, "");
}
function escapeRegExp(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
module.exports = msk;
module.exports.fit = fit;