tuijs-util 1.2.2 → 1.3.0
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.
- package/package.json +1 -1
- package/src/cjs/index.cjs +298 -173
- package/src/esm/index.js +11 -8
- package/src/esm/lib/util.check.js +111 -82
- package/src/esm/lib/util.dom.js +43 -26
- package/src/esm/lib/util.http.js +57 -24
- package/src/esm/lib/util.misc.js +33 -8
- package/src/esm/lib/util.regex.js +26 -14
package/src/cjs/index.cjs
CHANGED
|
@@ -1,86 +1,143 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
/**
|
|
4
|
+
* List of RegEx variables for different patterns.
|
|
5
|
+
*/
|
|
6
|
+
const regExLetters = /^[a-zA-Z]+$/;
|
|
7
|
+
const regExLettersLower = /^[a-z]+$/;
|
|
8
|
+
const regExLettersUpper = /^[A-Z]+$/;
|
|
9
|
+
const regExNumbers = /^\d+$/;
|
|
10
|
+
const regExBinary = /^[01]+$/;
|
|
11
|
+
const regExHexadecimal = /^[0-9A-Fa-f]+$/;
|
|
12
|
+
const regExSpecial = /^[\!\@\#\$\%\^\&\*\(\)\_\+\-\=\[\]\{\}\|\;\:\'\"\,\.\<\>\/\?\`\\\~]+$/;
|
|
13
|
+
const regExFqdn = /^(?=.{1,253}$)(([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+([a-zA-Z]{2,}|[a-zA-Z0-9-]{2,}))$/;
|
|
14
|
+
const regExUrl = new RegExp('^(https?:\\/\\/)?' +
|
|
15
|
+
// protocol
|
|
16
|
+
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' +
|
|
17
|
+
// domain name
|
|
18
|
+
'((\\d{1,3}\\.){3}\\d{1,3}))' +
|
|
19
|
+
// OR ip (v4) address
|
|
20
|
+
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' +
|
|
21
|
+
// port and path
|
|
22
|
+
'(\\?[;&a-z\\d%_.~+=-]*)?' +
|
|
23
|
+
// query string
|
|
24
|
+
'(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator
|
|
25
|
+
const regExEmail = /^[\w-\.]+@([\w-]+\.)+[a-zA-Z]{2,}$/;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Removes characters from a string based on a provided regex pattern.
|
|
29
|
+
* @param {string} string The string to process.
|
|
30
|
+
* @param {RegExp} regex The regex pattern of characters to remove from the string.
|
|
31
|
+
* @return {string} The processed string with specified characters removed.
|
|
32
|
+
* @throws {Error} Throws an error if the first parameter is not a string or if the second parameter is not a RegExp.
|
|
33
|
+
*/
|
|
34
|
+
function removeChar(string, regEx) {
|
|
35
|
+
try {
|
|
36
|
+
if (typeof string !== 'string') {
|
|
37
|
+
throw new Error(`First parameter must be a string.`);
|
|
38
|
+
}
|
|
39
|
+
if (!(regEx instanceof RegExp)) {
|
|
40
|
+
throw new Error(`Second parameter must be a RegExp.`);
|
|
41
|
+
}
|
|
42
|
+
return string.replace(regEx, '');
|
|
43
|
+
} catch (er) {
|
|
44
|
+
throw new Error(er.message);
|
|
7
45
|
}
|
|
8
|
-
var pattern = /^(?=.{1,253}$)(([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+([a-zA-Z]{2,}|[a-zA-Z0-9-]{2,}))$/;
|
|
9
|
-
return pattern.test(str);
|
|
10
46
|
}
|
|
11
47
|
|
|
12
|
-
|
|
13
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Checks for a valid FQDN (Uses RegEx).
|
|
50
|
+
* @param {string} string
|
|
51
|
+
* @returns {boolean} - Returns true if test is successful and false if the string is not validated or the RegEx test fails.
|
|
52
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
53
|
+
*/
|
|
54
|
+
function checkFqdn(string) {
|
|
14
55
|
try {
|
|
15
|
-
if (
|
|
16
|
-
|
|
56
|
+
if (typeof string !== 'string' || string.length === 0) {
|
|
57
|
+
return false;
|
|
17
58
|
}
|
|
18
|
-
;
|
|
19
|
-
var pattern = new RegExp('^(https?:\\/\\/)?' +
|
|
20
|
-
// protocol
|
|
21
|
-
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' +
|
|
22
|
-
// domain name
|
|
23
|
-
'((\\d{1,3}\\.){3}\\d{1,3}))' +
|
|
24
|
-
// OR ip (v4) address
|
|
25
|
-
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' +
|
|
26
|
-
// port and path
|
|
27
|
-
'(\\?[;&a-z\\d%_.~+=-]*)?' +
|
|
28
|
-
// query string
|
|
29
|
-
'(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator
|
|
30
|
-
return !!pattern.test(str); // Returns false if the url is invalid
|
|
59
|
+
return regExFqdn.test(string);
|
|
31
60
|
} catch (er) {
|
|
32
|
-
throw new Error(er);
|
|
61
|
+
throw new Error(er.message);
|
|
33
62
|
}
|
|
34
63
|
}
|
|
35
64
|
|
|
36
|
-
|
|
37
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Checks for a valid URL (Uses RegEx).
|
|
67
|
+
* @param {string} string
|
|
68
|
+
* @returns {boolean} - Returns true if test is successful and false if the string is not validated or the RegEx test fails.
|
|
69
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
70
|
+
*/
|
|
71
|
+
function checkUrl(string) {
|
|
38
72
|
try {
|
|
39
|
-
if (
|
|
40
|
-
|
|
73
|
+
if (typeof string !== 'string' || string.length === 0) {
|
|
74
|
+
return false;
|
|
41
75
|
}
|
|
42
76
|
;
|
|
43
|
-
|
|
44
|
-
if (regSp.test(str) == true) {
|
|
45
|
-
return true;
|
|
46
|
-
}
|
|
47
|
-
return false;
|
|
77
|
+
return regExUrl.test(string); // Returns false if the url is invalid
|
|
48
78
|
} catch (er) {
|
|
49
|
-
throw new Error(er);
|
|
79
|
+
throw new Error(er.message);
|
|
50
80
|
}
|
|
51
81
|
}
|
|
52
82
|
|
|
53
|
-
|
|
54
|
-
|
|
83
|
+
/**
|
|
84
|
+
* Checks for special characters (Uses RegEx).
|
|
85
|
+
* @param {string} string
|
|
86
|
+
* @returns {boolean} - Returns true if a special character is found and false if the string is not validated or the RegEx test fails.
|
|
87
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
88
|
+
*/
|
|
89
|
+
function checkSpecialChar(string) {
|
|
55
90
|
try {
|
|
56
|
-
if (
|
|
57
|
-
|
|
91
|
+
if (typeof string !== 'string' || string.length === 0) {
|
|
92
|
+
return false;
|
|
58
93
|
}
|
|
59
94
|
;
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
95
|
+
return regExSpecial.test(string);
|
|
96
|
+
} catch (er) {
|
|
97
|
+
throw new Error(er.message);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Checks for a valid email address. (Uses RegEx).
|
|
103
|
+
* @param {string} string
|
|
104
|
+
* @returns {boolean} - Returns true if email pattern test is successful and false if the string is not validated or the RegEx test fails.
|
|
105
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
106
|
+
*/
|
|
107
|
+
function checkEmail(string) {
|
|
108
|
+
try {
|
|
109
|
+
if (typeof string !== 'string' || string.length === 0) {
|
|
110
|
+
return false;
|
|
63
111
|
}
|
|
64
|
-
|
|
112
|
+
;
|
|
113
|
+
return regExEmail.test(string);
|
|
65
114
|
} catch (er) {
|
|
66
|
-
throw new Error(er);
|
|
115
|
+
throw new Error(er.message);
|
|
67
116
|
}
|
|
68
117
|
}
|
|
69
118
|
|
|
70
|
-
|
|
71
|
-
|
|
119
|
+
/**
|
|
120
|
+
* Checks for a space in a string
|
|
121
|
+
* @param {string} string
|
|
122
|
+
* @returns {boolean} - Returns true if space is found and false if the string is not validated or if a space is not found.
|
|
123
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
124
|
+
*/
|
|
125
|
+
function checkSpaces(string) {
|
|
72
126
|
try {
|
|
73
|
-
if (
|
|
74
|
-
|
|
127
|
+
if (typeof string !== 'string' || string.length === 0) {
|
|
128
|
+
return false;
|
|
75
129
|
}
|
|
76
130
|
;
|
|
77
131
|
return str.indexOf(' ') >= 0;
|
|
78
132
|
} catch (er) {
|
|
79
|
-
throw new Error(er);
|
|
133
|
+
throw new Error(er.message);
|
|
80
134
|
}
|
|
81
135
|
}
|
|
82
136
|
|
|
83
137
|
// Check for a list of text???
|
|
138
|
+
/**
|
|
139
|
+
* IN WORK
|
|
140
|
+
*/
|
|
84
141
|
function checkIsList(input) {
|
|
85
142
|
try {
|
|
86
143
|
// Check if the variable is a string
|
|
@@ -98,29 +155,43 @@ function checkIsList(input) {
|
|
|
98
155
|
}
|
|
99
156
|
return true;
|
|
100
157
|
} catch (er) {
|
|
101
|
-
throw new Error(er);
|
|
158
|
+
throw new Error(er.message);
|
|
102
159
|
}
|
|
103
160
|
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Checks an input to determine if it is an Array.
|
|
164
|
+
* @param {*} input
|
|
165
|
+
* @returns {boolean} - Returns true if the input is an Array and false if not.
|
|
166
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
167
|
+
*/
|
|
104
168
|
function checkIsArray(input) {
|
|
105
169
|
try {
|
|
106
170
|
return input.constructor === Array;
|
|
107
171
|
} catch (er) {
|
|
108
|
-
throw new Error(er);
|
|
172
|
+
throw new Error(er.message);
|
|
109
173
|
}
|
|
110
174
|
}
|
|
111
175
|
|
|
112
176
|
/**
|
|
113
|
-
* Checks
|
|
114
|
-
* @param {*} input
|
|
115
|
-
* @returns {boolean}
|
|
177
|
+
* Checks an input to determine if it is an Object.
|
|
178
|
+
* @param {*} input
|
|
179
|
+
* @returns {boolean} - Returns true if the input is an Object and false if not.
|
|
180
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
116
181
|
*/
|
|
117
182
|
function checkIsObject(input) {
|
|
118
183
|
try {
|
|
119
184
|
return input.constructor === Object;
|
|
120
185
|
} catch (er) {
|
|
121
|
-
throw new Error(er);
|
|
186
|
+
throw new Error(er.message);
|
|
122
187
|
}
|
|
123
188
|
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Checks an input to determine if it is valid JSON.
|
|
192
|
+
* @param {*} input
|
|
193
|
+
* @returns {boolean} - Returns true if the input is valid JSON and false if not.
|
|
194
|
+
*/
|
|
124
195
|
function checkIsJson(input) {
|
|
125
196
|
try {
|
|
126
197
|
JSON.parse(input);
|
|
@@ -130,59 +201,88 @@ function checkIsJson(input) {
|
|
|
130
201
|
}
|
|
131
202
|
}
|
|
132
203
|
|
|
133
|
-
|
|
134
|
-
|
|
204
|
+
/**
|
|
205
|
+
* Checks for number values in a string (Uses RegEx)
|
|
206
|
+
* @param {string} string
|
|
207
|
+
* @returns {boolean} - Returns true if a number is found and false if not.
|
|
208
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
209
|
+
*/
|
|
210
|
+
function checkNum(string) {
|
|
135
211
|
try {
|
|
136
|
-
if (
|
|
137
|
-
|
|
212
|
+
if (typeof string !== 'string' || string.length === 0) {
|
|
213
|
+
return false;
|
|
138
214
|
}
|
|
139
215
|
;
|
|
140
|
-
|
|
141
|
-
if (regNm.test(str) === true) {
|
|
142
|
-
return true;
|
|
143
|
-
}
|
|
144
|
-
return false;
|
|
216
|
+
return regExNumbers.test(string);
|
|
145
217
|
} catch (er) {
|
|
146
|
-
throw new Error(er);
|
|
218
|
+
throw new Error(er.message);
|
|
147
219
|
}
|
|
148
220
|
}
|
|
149
221
|
|
|
150
|
-
|
|
151
|
-
|
|
222
|
+
/**
|
|
223
|
+
* Checks for lowercase characters (Uses RegEx)
|
|
224
|
+
* @param {string} string
|
|
225
|
+
* @returns {boolean} - Returns true if a lowercase character is found and false if not.
|
|
226
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
227
|
+
*/
|
|
228
|
+
function checkLowercase(string) {
|
|
152
229
|
try {
|
|
153
|
-
if (
|
|
154
|
-
|
|
230
|
+
if (typeof string !== 'string' || string.length === 0) {
|
|
231
|
+
return false;
|
|
155
232
|
}
|
|
156
233
|
;
|
|
157
|
-
|
|
158
|
-
if (regLo.test(str) == true) {
|
|
159
|
-
return true;
|
|
160
|
-
}
|
|
161
|
-
return false;
|
|
234
|
+
return regExLettersLower.test(string);
|
|
162
235
|
} catch (er) {
|
|
163
|
-
throw new Error(er);
|
|
236
|
+
throw new Error(er.message);
|
|
164
237
|
}
|
|
165
238
|
}
|
|
166
239
|
|
|
167
|
-
|
|
168
|
-
|
|
240
|
+
/**
|
|
241
|
+
* Checks for uppercase characters (Uses RegEx)
|
|
242
|
+
* @param {string} string
|
|
243
|
+
* @returns {boolean} - Returns true if a uppercase character is found and false if not.
|
|
244
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
245
|
+
*/
|
|
246
|
+
function checkUppercase(string) {
|
|
169
247
|
try {
|
|
170
|
-
if (
|
|
171
|
-
|
|
248
|
+
if (typeof string !== 'string' || string.length === 0) {
|
|
249
|
+
return false;
|
|
172
250
|
}
|
|
173
251
|
;
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
252
|
+
return regExLettersUpper.test(string);
|
|
253
|
+
} catch (er) {
|
|
254
|
+
throw new Error(er.message);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Takes an HTML template literal, parses it, then extracts it.
|
|
260
|
+
* All elements in the template MUST be contained within a single set of template tags.
|
|
261
|
+
* THIS IS THE RECOMMENDED PARSER TO USE.
|
|
262
|
+
* @param {string} templateLit - An HTML string containing a <template> tag.
|
|
263
|
+
* @returns {DocumentFragment} - Returns a DocumentFragment which has been parsed and queried.
|
|
264
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
265
|
+
*/
|
|
266
|
+
function parseTemplate(templateLit) {
|
|
267
|
+
try {
|
|
268
|
+
let parser = new DOMParser();
|
|
269
|
+
let doc = parser.parseFromString(templateLit, 'text/html');
|
|
270
|
+
let template = doc.querySelector('template');
|
|
271
|
+
if (!template) {
|
|
272
|
+
throw new Error('No template tag found in the provided string.');
|
|
177
273
|
}
|
|
178
|
-
return
|
|
274
|
+
return template.content;
|
|
179
275
|
} catch (er) {
|
|
180
|
-
throw new Error(er);
|
|
276
|
+
throw new Error(er.message);
|
|
181
277
|
}
|
|
182
278
|
}
|
|
183
279
|
|
|
184
280
|
/**
|
|
185
|
-
* Takes an HTML template literal, parses it
|
|
281
|
+
* Takes an HTML template literal, parses it, then extracts the element.
|
|
282
|
+
* All elements in the template MUST be contained within a single parent element.
|
|
283
|
+
* @param {string} templateLit
|
|
284
|
+
* @returns {Element} - Returns a element Object which has been parsed and queried.
|
|
285
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
186
286
|
*/
|
|
187
287
|
function elmCleaner(templateLit) {
|
|
188
288
|
try {
|
|
@@ -191,13 +291,18 @@ function elmCleaner(templateLit) {
|
|
|
191
291
|
let elms = elmBody.body.querySelectorAll("*");
|
|
192
292
|
return elms[0];
|
|
193
293
|
} catch (er) {
|
|
194
|
-
|
|
195
|
-
throw new Error(er);
|
|
294
|
+
throw new Error(er.message);
|
|
196
295
|
}
|
|
197
296
|
}
|
|
198
297
|
|
|
199
298
|
/**
|
|
200
|
-
* Takes an HTML table row template literal, parses it
|
|
299
|
+
* Takes an HTML table row template literal, parses it, then extracts the element.
|
|
300
|
+
* @param {string} templateLit
|
|
301
|
+
* @returns {Element} - Returns a element Object which has been parsed and queried.
|
|
302
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
303
|
+
*/
|
|
304
|
+
/**
|
|
305
|
+
* IN WORK
|
|
201
306
|
*/
|
|
202
307
|
function elmCleanerTr(templateLit) {
|
|
203
308
|
try {
|
|
@@ -207,13 +312,18 @@ function elmCleanerTr(templateLit) {
|
|
|
207
312
|
elmTemp.remove();
|
|
208
313
|
return elms;
|
|
209
314
|
} catch (er) {
|
|
210
|
-
|
|
211
|
-
throw new Error(er);
|
|
315
|
+
throw new Error(er.message);
|
|
212
316
|
}
|
|
213
317
|
}
|
|
214
318
|
|
|
215
319
|
/**
|
|
216
320
|
* Takes an HTML template literal, parses it with the DOM parser, then extracts the NodeList. The list is then returned as an array.
|
|
321
|
+
* @param {string} templateLit
|
|
322
|
+
* @returns {Element} - Returns a element Object which has been parsed and queried.
|
|
323
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
324
|
+
*/
|
|
325
|
+
/**
|
|
326
|
+
* IN WORK
|
|
217
327
|
*/
|
|
218
328
|
function elmCleanerArray(templateLit) {
|
|
219
329
|
try {
|
|
@@ -222,98 +332,109 @@ function elmCleanerArray(templateLit) {
|
|
|
222
332
|
let elms = elmBody.body.querySelectorAll("*");
|
|
223
333
|
return Array.from(elms);
|
|
224
334
|
} catch (er) {
|
|
225
|
-
|
|
226
|
-
throw new Error(er);
|
|
335
|
+
throw new Error(er.message);
|
|
227
336
|
}
|
|
228
337
|
}
|
|
229
338
|
|
|
230
339
|
/**
|
|
231
|
-
*
|
|
340
|
+
* Adds 'http://' if valid URL and 'http://' or 'https://' is missing.
|
|
341
|
+
* @param {string} url
|
|
342
|
+
* @returns {string} - Returns an updated url string if necessary or returns the same string if url already starts with 'http://' or 'https://'.
|
|
343
|
+
* @throws {Error} - Throws Error if an error is detected.
|
|
232
344
|
*/
|
|
233
|
-
function parseTemplate(templateLit) {
|
|
234
|
-
try {
|
|
235
|
-
let parser = new DOMParser();
|
|
236
|
-
let doc = parser.parseFromString(templateLit, 'text/html');
|
|
237
|
-
let template = doc.querySelector('template');
|
|
238
|
-
if (!template) {
|
|
239
|
-
throw new Error('No template tag found in the provided string.');
|
|
240
|
-
}
|
|
241
|
-
return template.content;
|
|
242
|
-
} catch (er) {
|
|
243
|
-
console.error(er);
|
|
244
|
-
throw new Error(er);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// Adds 'http://' if valid URL
|
|
249
345
|
function urlAddHttp(url) {
|
|
250
346
|
try {
|
|
251
347
|
if (url === null || !checkUrl(url)) {
|
|
252
|
-
throw `Invalid input
|
|
348
|
+
throw new Error(`Invalid input.`);
|
|
253
349
|
}
|
|
254
350
|
;
|
|
255
|
-
if (url.startsWith("http://")
|
|
351
|
+
if (url.startsWith("http://") === false && url.startsWith("https://") === false) {
|
|
256
352
|
url = "http://" + url;
|
|
257
353
|
}
|
|
258
354
|
return url;
|
|
259
355
|
} catch (er) {
|
|
260
|
-
throw new Error(er);
|
|
356
|
+
throw new Error(er.message);
|
|
261
357
|
}
|
|
262
358
|
}
|
|
263
359
|
|
|
264
|
-
|
|
360
|
+
/**
|
|
361
|
+
* Adds 'https://' if valid URL and 'http://' or 'https://' is missing.
|
|
362
|
+
* @param {string} url
|
|
363
|
+
* @returns {string} - Returns an updated url string if necessary or returns the same string if url already starts with 'http://' or 'https://'.
|
|
364
|
+
* @throws {Error} - Throws Error if an error is detected.
|
|
365
|
+
*/
|
|
265
366
|
function urlAddHttps(url) {
|
|
266
367
|
try {
|
|
267
368
|
if (url === null || !checkUrl(url)) {
|
|
268
|
-
throw `Invalid input
|
|
369
|
+
throw new Error(`Invalid input.`);
|
|
269
370
|
}
|
|
270
371
|
;
|
|
271
|
-
if (url.startsWith("http://")
|
|
372
|
+
if (url.startsWith("http://") === false && url.startsWith("https://") === false) {
|
|
272
373
|
url = "https://" + url;
|
|
273
374
|
}
|
|
274
375
|
return url;
|
|
275
376
|
} catch (er) {
|
|
276
|
-
throw new Error(er);
|
|
377
|
+
throw new Error(er.message);
|
|
277
378
|
}
|
|
278
379
|
}
|
|
279
380
|
|
|
280
|
-
|
|
281
|
-
|
|
381
|
+
/**
|
|
382
|
+
* Collects and parses data from a JSON file
|
|
383
|
+
* @async
|
|
384
|
+
* @param {string} filePath
|
|
385
|
+
* @returns {Promise<Object>} - Returns data if response is ok, otherwise it throws an Error.
|
|
386
|
+
* @throws {Error} - Throws Error if an error is detected.
|
|
387
|
+
*/
|
|
388
|
+
async function reqFileJson(filePath) {
|
|
282
389
|
try {
|
|
283
|
-
const res = await fetch(
|
|
390
|
+
const res = await fetch(filePath);
|
|
284
391
|
if (!res.ok) {
|
|
285
|
-
|
|
392
|
+
throw new Error(res);
|
|
286
393
|
}
|
|
287
394
|
const data = await res.json();
|
|
288
395
|
return data;
|
|
289
396
|
} catch (er) {
|
|
290
|
-
throw new Error(er);
|
|
397
|
+
throw new Error(er.message);
|
|
291
398
|
}
|
|
292
399
|
}
|
|
293
400
|
|
|
294
|
-
|
|
401
|
+
/**
|
|
402
|
+
* Sends GET request to specified URL
|
|
403
|
+
* @async
|
|
404
|
+
* @param {string} url
|
|
405
|
+
* @returns {Promise<Object>} - Returns response if response is ok, otherwise it throws an Error.
|
|
406
|
+
* @throws {Error} - Throws Error if an error is detected.
|
|
407
|
+
*/
|
|
295
408
|
async function reqGet(url) {
|
|
296
409
|
try {
|
|
297
410
|
const res = await fetch(url, {
|
|
298
411
|
method: 'GET'
|
|
299
412
|
});
|
|
300
413
|
if (!res.ok) {
|
|
301
|
-
throw res;
|
|
414
|
+
throw new Error(res);
|
|
302
415
|
}
|
|
303
416
|
return res;
|
|
304
417
|
} catch (er) {
|
|
305
|
-
throw er;
|
|
418
|
+
throw new Error(er.message);
|
|
306
419
|
}
|
|
307
420
|
}
|
|
308
421
|
|
|
309
|
-
|
|
310
|
-
|
|
422
|
+
/**
|
|
423
|
+
* Sends POST request to specified URL, which contains JSON data in the body.
|
|
424
|
+
* @async
|
|
425
|
+
* @param {string} url
|
|
426
|
+
* @param {JSON} dataJson
|
|
427
|
+
* @returns {Promise<Object>} - Returns response if response is ok, otherwise it throws an Error.
|
|
428
|
+
* @throws {Error} - Throws Error if an error is detected.
|
|
429
|
+
*/
|
|
311
430
|
async function reqPostJson(url, dataJson) {
|
|
312
431
|
try {
|
|
313
432
|
if (dataJson === null || dataJson === undefined) {
|
|
314
|
-
throw `No
|
|
433
|
+
throw new Error(`No data provided.`);
|
|
434
|
+
}
|
|
435
|
+
if (!checkIsJson(dataJson)) {
|
|
436
|
+
throw new Error(`Provided data is not JSON.`);
|
|
315
437
|
}
|
|
316
|
-
JSON.parse(dataJson);
|
|
317
438
|
const res = await fetch(url, {
|
|
318
439
|
method: 'POST',
|
|
319
440
|
headers: {
|
|
@@ -322,15 +443,18 @@ async function reqPostJson(url, dataJson) {
|
|
|
322
443
|
body: dataJson
|
|
323
444
|
});
|
|
324
445
|
if (!res.ok) {
|
|
325
|
-
|
|
446
|
+
throw new Error(res);
|
|
326
447
|
}
|
|
327
448
|
return res;
|
|
328
449
|
} catch (er) {
|
|
329
|
-
throw new Error(er);
|
|
450
|
+
throw new Error(er.message);
|
|
330
451
|
}
|
|
331
452
|
}
|
|
453
|
+
|
|
332
454
|
// Simple POST request. export function is expecting FormData.
|
|
333
|
-
|
|
455
|
+
/**
|
|
456
|
+
* IN WORK
|
|
457
|
+
*/
|
|
334
458
|
async function reqPostForm(url, dataForm) {
|
|
335
459
|
try {
|
|
336
460
|
if (!(dataForm instanceof FormData)) {
|
|
@@ -351,34 +475,60 @@ async function reqPostForm(url, dataForm) {
|
|
|
351
475
|
|
|
352
476
|
const htmlTags = ["html", "body", "div", "span", "applet", "object", "iframe", "h1", "h2", "h3", "h4", "h5", "h6", "p", "blockquote", "pre", "a", "abbr", "acronym", "address", "big", "cite", "code", "del", "dfn", "em", "img", "ins", "kbd", "q", "s", "samp", "small", "strike", "strong", "sub", "sup", "tt", "var", "b", "u", "i", "center", "dl", "dt", "dd", "ol", "ul", "li", "fieldset", "form", "label", "legend", "table", "caption", "tbody", "tfoot", "thead", "tr", "th", "td", "article", "aside", "canvas", "details", "embed", "figure", "figcaption", "footer", "header", "hgroup", "menu", "nav", "output", "ruby", "section", "summary", "time", "mark", "audio", "video"];
|
|
353
477
|
|
|
354
|
-
|
|
478
|
+
/**
|
|
479
|
+
* Adds zero in front of numbers less than 10 and returns as a string.
|
|
480
|
+
* @param {number} num
|
|
481
|
+
* @returns {string}
|
|
482
|
+
* @throws {Error} - Throws Error if an error is detected.
|
|
483
|
+
*/
|
|
355
484
|
function addLeadZero(num) {
|
|
356
485
|
try {
|
|
357
486
|
if (num === null || typeof num !== 'number' || num > 9) {
|
|
358
|
-
throw `Invalid input
|
|
487
|
+
throw new Error(`Invalid input.`);
|
|
359
488
|
}
|
|
360
489
|
;
|
|
361
|
-
|
|
490
|
+
return "0" + num;
|
|
491
|
+
;
|
|
362
492
|
} catch (er) {
|
|
363
|
-
|
|
364
|
-
throw new Error(er);
|
|
493
|
+
throw new Error(er.message);
|
|
365
494
|
}
|
|
366
495
|
}
|
|
367
496
|
|
|
368
497
|
/**
|
|
369
498
|
* Generates a unique ID
|
|
499
|
+
* @param {number} [length=16] - The number of characters for the random part of the ID.
|
|
500
|
+
* @returns {string}
|
|
501
|
+
* @throws {Error} - Throws Error if input is invalid or another error is detected.
|
|
370
502
|
*/
|
|
371
|
-
function generateUID() {
|
|
372
|
-
|
|
503
|
+
function generateUID(length = 16) {
|
|
504
|
+
try {
|
|
505
|
+
if (typeof length !== 'number' || length <= 0) {
|
|
506
|
+
throw new Error(`Invalid input. The 'length' parameter must be a positive number.`);
|
|
507
|
+
}
|
|
508
|
+
const timestampPart = Date.now().toString(36);
|
|
509
|
+
const randomPart = Math.random().toString(36).slice(2, 2 + length);
|
|
510
|
+
return 'uid-' + timestampPart + '-' + randomPart;
|
|
511
|
+
} catch (er) {
|
|
512
|
+
throw new Error(er.message);
|
|
513
|
+
}
|
|
373
514
|
}
|
|
374
515
|
|
|
375
516
|
/**
|
|
376
|
-
*
|
|
517
|
+
* @typedef {string[]} ImgUrls - An array of URL strings
|
|
518
|
+
*/
|
|
519
|
+
/**
|
|
520
|
+
* Preloads images in cache.
|
|
521
|
+
* @param {ImgUrls} imgUrls - An array of URL strings
|
|
522
|
+
* @returns {void}
|
|
523
|
+
* @throws {Error} - Throws an Error if loading an image fails.
|
|
377
524
|
*/
|
|
378
525
|
function preloadImages(imgUrls) {
|
|
379
526
|
imgUrls.forEach(url => {
|
|
380
527
|
const img = new Image();
|
|
381
528
|
img.src = url;
|
|
529
|
+
img.onerror = () => {
|
|
530
|
+
throw new Error(`Failed to load image: ${url}`);
|
|
531
|
+
};
|
|
382
532
|
});
|
|
383
533
|
}
|
|
384
534
|
|
|
@@ -391,36 +541,7 @@ function sleep(ms) {
|
|
|
391
541
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
392
542
|
}
|
|
393
543
|
|
|
394
|
-
/**
|
|
395
|
-
* List of RegEx variables for different patterns.
|
|
396
|
-
*/
|
|
397
|
-
const letters = /^[a-zA-Z]+$/;
|
|
398
|
-
const lettersLower = /^[a-z]+$/;
|
|
399
|
-
const lettersUpper = /^[A-Z]+$/;
|
|
400
|
-
const numbers = /^\d+$/;
|
|
401
|
-
const binary = /[^01]/g;
|
|
402
|
-
const hexadecimal = /[^0-9A-Fa-f]/g;
|
|
403
|
-
const special = /^[\!\@\#\$\%\^\&\*\(\)\_\+\-\=\[\]\{\}\|\;\'\:\"\,\.\<\>\?\/]+$/;
|
|
404
|
-
|
|
405
|
-
/**
|
|
406
|
-
* Removes characters from a string based on a provided regex pattern.
|
|
407
|
-
* @param {string} string The string to process.
|
|
408
|
-
* @param {RegExp} regex The regex pattern of characters to remove from the string.
|
|
409
|
-
* @return {string} The processed string with specified characters removed.
|
|
410
|
-
* @throws {Error} Throws an error if the first parameter is not a string or if the second parameter is not a RegExp.
|
|
411
|
-
*/
|
|
412
|
-
function removeChar(string, regex) {
|
|
413
|
-
if (typeof string !== 'string') {
|
|
414
|
-
throw new Error(`First parameter must be a string.`);
|
|
415
|
-
}
|
|
416
|
-
if (!(regex instanceof RegExp)) {
|
|
417
|
-
throw new Error(`Second parameter must be a RegExp.`);
|
|
418
|
-
}
|
|
419
|
-
return string.replace(regex, '');
|
|
420
|
-
}
|
|
421
|
-
|
|
422
544
|
exports.addLeadZero = addLeadZero;
|
|
423
|
-
exports.binary = binary;
|
|
424
545
|
exports.checkEmail = checkEmail;
|
|
425
546
|
exports.checkFqdn = checkFqdn;
|
|
426
547
|
exports.checkIsArray = checkIsArray;
|
|
@@ -437,20 +558,24 @@ exports.elmCleaner = elmCleaner;
|
|
|
437
558
|
exports.elmCleanerArray = elmCleanerArray;
|
|
438
559
|
exports.elmCleanerTr = elmCleanerTr;
|
|
439
560
|
exports.generateUID = generateUID;
|
|
440
|
-
exports.hexadecimal = hexadecimal;
|
|
441
561
|
exports.htmlTags = htmlTags;
|
|
442
|
-
exports.letters = letters;
|
|
443
|
-
exports.lettersLower = lettersLower;
|
|
444
|
-
exports.lettersUpper = lettersUpper;
|
|
445
|
-
exports.numbers = numbers;
|
|
446
562
|
exports.parseTemplate = parseTemplate;
|
|
447
563
|
exports.preloadImages = preloadImages;
|
|
564
|
+
exports.regExBinary = regExBinary;
|
|
565
|
+
exports.regExEmail = regExEmail;
|
|
566
|
+
exports.regExFqdn = regExFqdn;
|
|
567
|
+
exports.regExHexadecimal = regExHexadecimal;
|
|
568
|
+
exports.regExLetters = regExLetters;
|
|
569
|
+
exports.regExLettersLower = regExLettersLower;
|
|
570
|
+
exports.regExLettersUpper = regExLettersUpper;
|
|
571
|
+
exports.regExNumbers = regExNumbers;
|
|
572
|
+
exports.regExSpecial = regExSpecial;
|
|
573
|
+
exports.regExUrl = regExUrl;
|
|
448
574
|
exports.removeChar = removeChar;
|
|
449
575
|
exports.reqFileJson = reqFileJson;
|
|
450
576
|
exports.reqGet = reqGet;
|
|
451
577
|
exports.reqPostForm = reqPostForm;
|
|
452
578
|
exports.reqPostJson = reqPostJson;
|
|
453
579
|
exports.sleep = sleep;
|
|
454
|
-
exports.special = special;
|
|
455
580
|
exports.urlAddHttp = urlAddHttp;
|
|
456
581
|
exports.urlAddHttps = urlAddHttps;
|