@cat-of-summer/st-script 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,183 +0,0 @@
1
- class st_mask {
2
-
3
- #default_filler = '_';
4
- #placeholder = true;
5
- #mask_array = [];
6
- #inputs = [];
7
-
8
- #apply_mask(mask, input_string) {
9
- let result = "";
10
- let static_mask_index = 0;
11
- let dynamic_mask_index = 0;
12
-
13
- for (let char of input_string) {
14
- while (dynamic_mask_index + static_mask_index < mask.length) {
15
- let [value, is_regexp, filler] = mask[dynamic_mask_index + static_mask_index];
16
-
17
- if (!is_regexp) result += value, static_mask_index++;
18
- else {
19
- if (new RegExp(`^${value}$`).test(char)) result += char, dynamic_mask_index++;
20
- break;
21
- }
22
- }
23
- }
24
-
25
- if (this.#default_filler && this.#placeholder)
26
- while (dynamic_mask_index + static_mask_index < mask.length)
27
- result += mask[dynamic_mask_index + static_mask_index++][1]
28
- ? mask[dynamic_mask_index + static_mask_index - 1][2]
29
- : mask[dynamic_mask_index + static_mask_index - 1][0];
30
- else if (!dynamic_mask_index) result = '';
31
- else if (dynamic_mask_index + static_mask_index < mask.length && !mask[dynamic_mask_index + static_mask_index][1])
32
- result += mask[dynamic_mask_index + static_mask_index++][0];
33
-
34
- return [result, dynamic_mask_index, mask.filter(mask_char => mask_char[1]).length == dynamic_mask_index];
35
- }
36
-
37
- #return_best_mask(raw_value) {
38
- return this.#mask_array
39
- .map(mask => this.#apply_mask(mask, raw_value))
40
- .reduce((best, value, index) => (!best || value[1] > best[0][1]
41
- ? [value, index]
42
- : best)
43
- , null);
44
- }
45
-
46
- #prepare_mask(mask) {
47
- return mask.replace(/{(\d+)\*{([^}]*?)}(.*?)}/g, (_, g1, g2, g3) => (`{{${g2}}${g3}}`).repeat(g1))
48
- .split(/({{.*?}.*?})/)
49
- .filter(Boolean)
50
- .map(value => {
51
- let [, inner_value] = value.match(/^{{(.*?)}.*?}$/) || [];
52
- let [, filler] = value.match(/^{{.*?}=(.*?)}$/) || [];
53
- return [inner_value || value, !!inner_value, filler || this.#default_filler];
54
- })
55
- }
56
-
57
- #refresh_mask(input_object = null) {
58
- if (this.#is_mask_array_empty()) {
59
- console.warn('Попытка обновить маску после удаления правил', this);
60
- return;
61
- }
62
-
63
- (input_object ? [input_object] : this.#inputs).forEach(obj => {
64
- obj.best_mask = this.#return_best_mask(obj.value);
65
- obj.input.value = obj.best_mask[0][0];
66
- Object.assign(obj.input.dataset, {
67
- mask_id: obj.best_mask[1],
68
- progress: obj.best_mask[0][1],
69
- is_full: obj.best_mask[0][2]
70
- });
71
- });
72
- }
73
-
74
- #is_mask_array_empty() {
75
- return !this.#mask_array.some(item => !item != null);
76
- }
77
-
78
- add_mask(mask) {
79
- this.#mask_array.push(this.#prepare_mask(mask));
80
-
81
- this.#refresh_mask();
82
-
83
- return this.#mask_array.length - 1;
84
- }
85
-
86
- remove_mask(index) {
87
- delete this.#mask_array[index];
88
-
89
- this.#refresh_mask();
90
- }
91
-
92
- set_masks(masks) {
93
- this.#mask_array = masks.map((mask) => {
94
- return this.#prepare_mask(mask);
95
- });
96
-
97
- this.#refresh_mask();
98
- }
99
-
100
- constructor(param) {
101
- let $this = this;
102
-
103
- $this.#default_filler = param.default_filler ?? $this.#default_filler;
104
- $this.#placeholder = param.placeholder ?? $this.#placeholder;
105
-
106
- let elements = param.inputs
107
- ? (param.inputs instanceof Element ? [param.inputs] : Array.from(param.inputs))
108
- : Array.from(document.querySelectorAll(param.selector));
109
-
110
- elements.forEach(input => {
111
-
112
- let input_object = {
113
- input: input,
114
- value: input.value.split().filter(Boolean)
115
- };
116
-
117
- input.addEventListener("input", (event) => {
118
- event.preventDefault();
119
-
120
- if (this.#is_mask_array_empty()) {
121
- console.error('Нет масок для этого поля.', this);
122
- return;
123
- }
124
-
125
- if (event.data === null) {
126
- let new_raw_value = [];
127
-
128
- for (let char of input_object.value) {
129
- new_raw_value.push(char);
130
-
131
- if (this.#apply_mask(this.#mask_array[input_object.best_mask[1]], new_raw_value)[1] == input_object.best_mask[0][1]) {
132
- new_raw_value.pop();
133
- break;
134
- }
135
- }
136
-
137
- input_object.value = new_raw_value;
138
- } else
139
- input_object.value.push(...event.data);
140
-
141
- $this.#refresh_mask(input_object);
142
-
143
- if (typeof param.on_input === 'function')
144
- param.on_input(input, {
145
- mask_id: input_object.best_mask[1],
146
- progress: input_object.best_mask[0][1],
147
- is_full: input_object.best_mask[0][2]
148
- });
149
- });
150
-
151
- $this.#inputs.push(input_object);
152
-
153
- input.closest('form')?.addEventListener('reset', () => {
154
- input_object.value = [];
155
- $this.#refresh_mask(input_object);
156
- });
157
- });
158
-
159
- if (param.masks)
160
- $this.set_masks(param.masks);
161
- }
162
-
163
- }
164
-
165
- class n_st_mask {
166
- constructor (params) {
167
- params = {
168
- masks: ["{{\\d}} {2*{\\w}=А}"], //Надо сделать бесконечность + повторения блоков, типо для чисел \d\d\d \d\d\d \d\d\d ...
169
- filler: '_',
170
- placeholder: true,
171
- reverse_input_order: false,
172
-
173
- on_input: () => {},
174
- on_fill: () => {},
175
-
176
- ...params
177
- };
178
-
179
-
180
- }
181
- }
182
-
183
- export default st_mask;