-
Notifications
You must be signed in to change notification settings - Fork 0
/
mask-util.ts
139 lines (116 loc) · 3.35 KB
/
mask-util.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import type { MaskGenerator } from '../types/mask-generator';
import MaskFunctions from './mask';
export const DEFAULT_MASK_RULES = new Map([
['A', /[A-Za-z]/],
['9', /\d/],
['?', /\w/],
]);
// Helper functions
export const mask = (value: string, maskGenerator: MaskGenerator): string =>
MaskFunctions.mask(value, maskGenerator)?.maskedValue ?? '';
export const createDefaultMaskGenerator = (mask: string): MaskGenerator => ({
rules: DEFAULT_MASK_RULES,
generateMask: () => mask,
});
const onlyDigits = (value: string) => {
const onlyDigits = value != null ? value.replace(/\D/g, '') : null;
return onlyDigits;
};
const hasMoreDigits = (v01: string, v02: string): boolean => {
const d01 = onlyDigits(v01);
const d02 = onlyDigits(v02);
const len01 = d01?.length ?? 0;
const len02 = d02?.length ?? 0;
const moreDigits = len01 > len02;
return moreDigits;
};
const dynamicNumberMask = (value: string, ...masks: string[]) => {
let currentMask = null;
for (const mask of masks) {
if (!hasMoreDigits(value, mask)) {
return mask;
}
currentMask = mask;
}
return currentMask ?? '';
};
export const createDynamicNumberMaskGenerator = (
...masks: string[]
): MaskGenerator => ({
rules: DEFAULT_MASK_RULES,
generateMask: (v) => dynamicNumberMask(v, ...masks),
});
export const getCurrencyMaskGenerator = ({
prefix = '',
thousandSeparator = '',
centsSeparator = '',
}: {
prefix?: string;
thousandSeparator?: string;
centsSeparator?: string;
}): MaskGenerator => {
const getRawValue = (value: string): string => {
return value?.replace(/\D/g, '') ?? '';
};
return {
rules: DEFAULT_MASK_RULES,
generateMask: (value) => {
const rawVal = getRawValue(value);
const len = rawVal.length;
const lenCents = centsSeparator ? Math.min(2, Math.max(len - 1, 0)) : 0;
const lenNoCents = Math.max(len - lenCents, 0);
const mask =
prefix +
'9'.repeat(lenNoCents % 3) +
(lenNoCents % 3 > 0 && lenNoCents >= 3 ? thousandSeparator : '') +
(lenNoCents >= 3
? Array(Math.trunc(lenNoCents / 3))
.fill('999')
.join(thousandSeparator)
: '') +
(lenCents ? centsSeparator : '') +
'9'.repeat(lenCents);
return mask;
},
transform: (value) => {
const valDigits = getRawValue(value);
const rawVal = valDigits?.replace(/^0+/, '');
const len = rawVal.length;
const prefixToUse = value?.startsWith(prefix) ? prefix : '';
if ((valDigits?.length ?? 0) === 0) {
return value;
} else if (centsSeparator && len < 3) {
return (
prefixToUse + '0' + centsSeparator + '0'.repeat(2 - len) + rawVal
);
} else if (valDigits?.length !== rawVal?.length) {
const initial: {
current: string;
parts: string[];
} = { current: '', parts: [] };
const { parts: thousandsParts } = rawVal
.substring(0, len - 2)
.split('')
.reverse()
.reduce(({ current, parts }, char, idx, arr) => {
const newCurrent = char + current;
if (idx === arr?.length - 1 || (idx + 1) % 3 === 0) {
return {
current: '',
parts: [newCurrent, ...parts],
};
}
return {
current: newCurrent,
parts,
};
}, initial);
const valNoCents = thousandsParts.join(thousandSeparator);
const newValue =
prefixToUse + valNoCents + centsSeparator + rawVal.substring(len - 2);
return newValue;
}
return value;
},
};
};