-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathmask.js
357 lines (304 loc) · 10.3 KB
/
mask.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* eslint no-use-before-define: ["error", { functions: false }] */
import { findLastIndex, repeat } from "./helpers";
import parseMask from "./parse-mask";
export default class MaskUtils {
constructor(options) {
this.maskOptions = parseMask(options);
}
isCharacterAllowedAtPosition = (character, position) => {
const { maskPlaceholder } = this.maskOptions;
if (this.isCharacterFillingPosition(character, position)) {
return true;
}
if (!maskPlaceholder) {
return false;
}
return maskPlaceholder[position] === character;
};
isCharacterFillingPosition = (character, position) => {
const { mask } = this.maskOptions;
if (!character || position >= mask.length) {
return false;
}
if (!this.isPositionEditable(position)) {
return mask[position] === character;
}
const charRule = mask[position];
return new RegExp(charRule).test(character);
};
isPositionEditable = position => {
const { mask, permanents } = this.maskOptions;
return position < mask.length && permanents.indexOf(position) === -1;
};
isValueEmpty = value => {
return value.split("").every((character, position) => {
return (
!this.isPositionEditable(position) ||
!this.isCharacterFillingPosition(character, position)
);
});
};
isValueFilled = value => {
return (
this.getFilledLength(value) === this.maskOptions.lastEditablePosition + 1
);
};
getDefaultSelectionForValue = value => {
const filledLength = this.getFilledLength(value);
const cursorPosition = this.getRightEditablePosition(filledLength);
return { start: cursorPosition, end: cursorPosition };
};
getFilledLength = value => {
const characters = value.split("");
const lastFilledIndex = findLastIndex(characters, (character, position) => {
return (
this.isPositionEditable(position) &&
this.isCharacterFillingPosition(character, position)
);
});
return lastFilledIndex + 1;
};
getStringFillingLengthAtPosition = (string, position) => {
const characters = string.split("");
const insertedValue = characters.reduce((value, character) => {
return this.insertCharacterAtPosition(value, character, value.length);
}, repeat(" ", position));
return insertedValue.length - position;
};
getLeftEditablePosition = position => {
for (let i = position; i >= 0; i--) {
if (this.isPositionEditable(i)) {
return i;
}
}
return null;
};
getRightEditablePosition = position => {
const { mask } = this.maskOptions;
for (let i = position; i < mask.length; i++) {
if (this.isPositionEditable(i)) {
return i;
}
}
return null;
};
formatValue = value => {
const { maskPlaceholder, mask } = this.maskOptions;
if (!maskPlaceholder) {
value = this.insertStringAtPosition("", value, 0);
while (
value.length < mask.length &&
!this.isPositionEditable(value.length)
) {
value += mask[value.length];
}
return value;
}
return this.insertStringAtPosition(maskPlaceholder, value, 0);
};
clearRange = (value, start, len) => {
if (!len) {
return value;
}
const end = start + len;
const { maskPlaceholder, mask } = this.maskOptions;
const clearedValue = value
.split("")
.map((character, i) => {
const isEditable = this.isPositionEditable(i);
if (!maskPlaceholder && i >= end && !isEditable) {
return "";
}
if (i < start || i >= end) {
return character;
}
if (!isEditable) {
return mask[i];
}
if (maskPlaceholder) {
return maskPlaceholder[i];
}
return "";
})
.join("");
return this.formatValue(clearedValue);
};
insertCharacterAtPosition = (value, character, position) => {
const { mask, maskPlaceholder } = this.maskOptions;
if (position >= mask.length) {
return value;
}
const isAllowed = this.isCharacterAllowedAtPosition(character, position);
const isEditable = this.isPositionEditable(position);
const nextEditablePosition = this.getRightEditablePosition(position);
const isNextPlaceholder =
maskPlaceholder && nextEditablePosition
? character === maskPlaceholder[nextEditablePosition]
: null;
const valueBefore = value.slice(0, position);
if (isAllowed || !isEditable) {
const insertedCharacter = isAllowed ? character : mask[position];
value = valueBefore + insertedCharacter;
}
if (!isAllowed && !isEditable && !isNextPlaceholder) {
value = this.insertCharacterAtPosition(value, character, position + 1);
}
return value;
};
insertStringAtPosition = (value, string, position) => {
const { mask, maskPlaceholder } = this.maskOptions;
if (!string || position >= mask.length) {
return value;
}
const characters = string.split("");
const isFixedLength = this.isValueFilled(value) || !!maskPlaceholder;
const valueAfter = value.slice(position);
value = characters.reduce((value, character) => {
return this.insertCharacterAtPosition(value, character, value.length);
}, value.slice(0, position));
if (isFixedLength) {
value += valueAfter.slice(value.length - position);
} else if (this.isValueFilled(value)) {
value += mask.slice(value.length).join("");
} else {
const editableCharactersAfter = valueAfter
.split("")
.filter((character, i) => {
return this.isPositionEditable(position + i);
});
value = editableCharactersAfter.reduce((value, character) => {
const nextEditablePosition = this.getRightEditablePosition(
value.length
);
if (nextEditablePosition === null) {
return value;
}
if (!this.isPositionEditable(value.length)) {
value += mask.slice(value.length, nextEditablePosition).join("");
}
return this.insertCharacterAtPosition(value, character, value.length);
}, value);
}
return value;
};
isAutoFilled = (
{ value, selection },
{ value: previousValue, selection: previousSelection }
) => {
const { maskPlaceholder } = this.maskOptions;
if (
// Autocomplete will set the previous selection to the length of the autocompleted value
previousSelection.end < previousValue.length &&
selection.end === value.length
) {
return true;
}
if (
selection.length === 0 &&
previousSelection.length === 0 &&
selection.start < previousSelection.start &&
selection.start === value.length
) {
// When both previous and current state have no selection length, the cursor index is less than it was before
// and the cursor is at the end of the new value
// Check each character to see if there are any changes which is only possible if the value was autocompleted.
return value.split("").some((char, index) => {
return char !== previousValue[index];
});
}
if (
!maskPlaceholder &&
previousSelection.length === 0 &&
previousValue.length < value.length
) {
// If there is no mask placeholder, the selection is 0 and the new value is longer than the previous value
// (characters have been added)
return value.split("").some((char, index) => {
// Check each character before the selection to see if they have changed
if (index < previousSelection.start) {
// Any character before the previous selection that changes will be changed because of autofill
return char !== previousValue[index];
}
return false;
});
}
return false;
};
processChange = (currentState, previousState) => {
const { mask, prefix, lastEditablePosition } = this.maskOptions;
const { value, selection } = currentState;
let previousValue = previousState.value;
let previousSelection = previousState.selection;
let newValue = value;
let enteredString = "";
let formattedEnteredStringLength = 0;
let removedLength = 0;
let cursorPosition = Math.min(previousSelection.start, selection.start);
if (this.isAutoFilled(currentState, previousState)) {
// If the value is autocompleted treat it as if the input started empty.
previousValue = prefix;
previousSelection = {
start: 0,
end: 0,
length: 0
};
}
if (selection.end > previousSelection.start) {
enteredString = newValue.slice(previousSelection.start, selection.end);
formattedEnteredStringLength = this.getStringFillingLengthAtPosition(
enteredString,
cursorPosition
);
if (!formattedEnteredStringLength) {
removedLength = 0;
} else {
removedLength = previousSelection.length;
}
} else if (newValue.length < previousValue.length) {
removedLength = previousValue.length - newValue.length;
}
if (
!(
newValue.length === previousValue.length &&
selection.end === previousSelection.start
)
) {
newValue = previousValue;
}
if (removedLength) {
if (removedLength === 1 && !previousSelection.length) {
const deleteFromRight = previousSelection.start === selection.start;
cursorPosition = deleteFromRight
? this.getRightEditablePosition(selection.start)
: this.getLeftEditablePosition(selection.start);
}
newValue = this.clearRange(newValue, cursorPosition, removedLength);
}
newValue = this.insertStringAtPosition(
newValue,
enteredString,
cursorPosition
);
cursorPosition += formattedEnteredStringLength;
if (cursorPosition >= mask.length) {
cursorPosition = mask.length;
} else if (
cursorPosition < prefix.length &&
!formattedEnteredStringLength
) {
cursorPosition = prefix.length;
} else if (
cursorPosition >= prefix.length &&
cursorPosition < lastEditablePosition &&
formattedEnteredStringLength
) {
cursorPosition = this.getRightEditablePosition(cursorPosition);
}
newValue = this.formatValue(newValue);
return {
value: newValue,
enteredString,
selection: { start: cursorPosition, end: cursorPosition }
};
};
}