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/esm/index.js
CHANGED
|
@@ -36,13 +36,16 @@ export {
|
|
|
36
36
|
sleep
|
|
37
37
|
} from './lib/util.misc.js';
|
|
38
38
|
export {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
39
|
+
regExLetters,
|
|
40
|
+
regExLettersLower,
|
|
41
|
+
regExLettersUpper,
|
|
42
|
+
regExNumbers,
|
|
43
|
+
regExBinary,
|
|
44
|
+
regExHexadecimal,
|
|
45
|
+
regExSpecial,
|
|
46
|
+
regExFqdn,
|
|
47
|
+
regExUrl,
|
|
48
|
+
regExEmail,
|
|
49
|
+
removeChar
|
|
47
50
|
} from './lib/util.regex.js';
|
|
48
51
|
|
|
@@ -1,80 +1,95 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { regExNumbers, regExLettersLower, regExLettersUpper, regExSpecial, regExFqdn, regExUrl, regExEmail } from "./util.regex";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks for a valid FQDN (Uses RegEx).
|
|
5
|
+
* @param {string} string
|
|
6
|
+
* @returns {boolean} - Returns true if test is successful and false if the string is not validated or the RegEx test fails.
|
|
7
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
8
|
+
*/
|
|
9
|
+
export function checkFqdn(string) {
|
|
10
|
+
try {
|
|
11
|
+
if (typeof string !== 'string' || string.length === 0) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
return regExFqdn.test(string);
|
|
15
|
+
} catch (er) {
|
|
16
|
+
throw new Error(er.message);
|
|
5
17
|
}
|
|
6
|
-
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,}))$/;
|
|
7
|
-
return pattern.test(str);
|
|
8
18
|
}
|
|
9
19
|
|
|
10
|
-
|
|
11
|
-
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Checks for a valid URL (Uses RegEx).
|
|
23
|
+
* @param {string} string
|
|
24
|
+
* @returns {boolean} - Returns true if test is successful and false if the string is not validated or the RegEx test fails.
|
|
25
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
26
|
+
*/
|
|
27
|
+
export function checkUrl(string) {
|
|
12
28
|
try {
|
|
13
|
-
if (
|
|
14
|
-
|
|
29
|
+
if (typeof string !== 'string' || string.length === 0) {
|
|
30
|
+
return false;
|
|
15
31
|
};
|
|
16
|
-
|
|
17
|
-
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
|
|
18
|
-
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
|
|
19
|
-
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
|
|
20
|
-
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
|
|
21
|
-
'(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator
|
|
22
|
-
return !!pattern.test(str); // Returns false if the url is invalid
|
|
32
|
+
return regExUrl.test(string); // Returns false if the url is invalid
|
|
23
33
|
} catch (er) {
|
|
24
|
-
throw new Error(er);
|
|
34
|
+
throw new Error(er.message);
|
|
25
35
|
}
|
|
26
36
|
}
|
|
27
37
|
|
|
28
|
-
|
|
29
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Checks for special characters (Uses RegEx).
|
|
40
|
+
* @param {string} string
|
|
41
|
+
* @returns {boolean} - Returns true if a special character is found and false if the string is not validated or the RegEx test fails.
|
|
42
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
43
|
+
*/
|
|
44
|
+
export function checkSpecialChar(string) {
|
|
30
45
|
try {
|
|
31
|
-
if (
|
|
32
|
-
|
|
46
|
+
if (typeof string !== 'string' || string.length === 0) {
|
|
47
|
+
return false;
|
|
33
48
|
};
|
|
34
|
-
|
|
35
|
-
if (regSp.test(str) == true) {
|
|
36
|
-
return true;
|
|
37
|
-
}
|
|
38
|
-
return false;
|
|
49
|
+
return regExSpecial.test(string);
|
|
39
50
|
} catch (er) {
|
|
40
|
-
throw new Error(er);
|
|
51
|
+
throw new Error(er.message);
|
|
41
52
|
}
|
|
42
53
|
}
|
|
43
54
|
|
|
44
55
|
/**
|
|
45
|
-
*
|
|
46
|
-
* @param {string}
|
|
47
|
-
* @returns {boolean} -
|
|
48
|
-
* @throws {Error} - Throws
|
|
56
|
+
* Checks for a valid email address. (Uses RegEx).
|
|
57
|
+
* @param {string} string
|
|
58
|
+
* @returns {boolean} - Returns true if email pattern test is successful and false if the string is not validated or the RegEx test fails.
|
|
59
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
49
60
|
*/
|
|
50
|
-
export function checkEmail(
|
|
61
|
+
export function checkEmail(string) {
|
|
51
62
|
try {
|
|
52
|
-
if (
|
|
53
|
-
|
|
63
|
+
if (typeof string !== 'string' || string.length === 0) {
|
|
64
|
+
return false;
|
|
54
65
|
};
|
|
55
|
-
|
|
56
|
-
if (str.match(validRegex)) {
|
|
57
|
-
return true;
|
|
58
|
-
}
|
|
59
|
-
return false;
|
|
66
|
+
return regExEmail.test(string);
|
|
60
67
|
} catch (er) {
|
|
61
68
|
throw new Error(er.message);
|
|
62
69
|
}
|
|
63
70
|
}
|
|
64
71
|
|
|
65
|
-
|
|
66
|
-
|
|
72
|
+
/**
|
|
73
|
+
* Checks for a space in a string
|
|
74
|
+
* @param {string} string
|
|
75
|
+
* @returns {boolean} - Returns true if space is found and false if the string is not validated or if a space is not found.
|
|
76
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
77
|
+
*/
|
|
78
|
+
export function checkSpaces(string) {
|
|
67
79
|
try {
|
|
68
|
-
if (
|
|
69
|
-
|
|
80
|
+
if (typeof string !== 'string' || string.length === 0) {
|
|
81
|
+
return false;
|
|
70
82
|
};
|
|
71
83
|
return str.indexOf(' ') >= 0;
|
|
72
84
|
} catch (er) {
|
|
73
|
-
throw new Error(er);
|
|
85
|
+
throw new Error(er.message);
|
|
74
86
|
}
|
|
75
87
|
}
|
|
76
88
|
|
|
77
89
|
// Check for a list of text???
|
|
90
|
+
/**
|
|
91
|
+
* IN WORK
|
|
92
|
+
*/
|
|
78
93
|
export function checkIsList(input) {
|
|
79
94
|
try {
|
|
80
95
|
// Check if the variable is a string
|
|
@@ -92,31 +107,43 @@ export function checkIsList(input) {
|
|
|
92
107
|
}
|
|
93
108
|
return true;
|
|
94
109
|
} catch (er) {
|
|
95
|
-
throw new Error(er);
|
|
110
|
+
throw new Error(er.message);
|
|
96
111
|
}
|
|
97
112
|
}
|
|
98
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Checks an input to determine if it is an Array.
|
|
116
|
+
* @param {*} input
|
|
117
|
+
* @returns {boolean} - Returns true if the input is an Array and false if not.
|
|
118
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
119
|
+
*/
|
|
99
120
|
export function checkIsArray(input) {
|
|
100
121
|
try {
|
|
101
122
|
return input.constructor === Array;
|
|
102
123
|
} catch (er) {
|
|
103
|
-
throw new Error(er);
|
|
124
|
+
throw new Error(er.message);
|
|
104
125
|
}
|
|
105
126
|
}
|
|
106
127
|
|
|
107
128
|
/**
|
|
108
|
-
* Checks
|
|
109
|
-
* @param {*} input
|
|
110
|
-
* @returns {boolean}
|
|
129
|
+
* Checks an input to determine if it is an Object.
|
|
130
|
+
* @param {*} input
|
|
131
|
+
* @returns {boolean} - Returns true if the input is an Object and false if not.
|
|
132
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
111
133
|
*/
|
|
112
134
|
export function checkIsObject(input) {
|
|
113
135
|
try {
|
|
114
136
|
return input.constructor === Object;
|
|
115
137
|
} catch (er) {
|
|
116
|
-
throw new Error(er);
|
|
138
|
+
throw new Error(er.message);
|
|
117
139
|
}
|
|
118
140
|
}
|
|
119
141
|
|
|
142
|
+
/**
|
|
143
|
+
* Checks an input to determine if it is valid JSON.
|
|
144
|
+
* @param {*} input
|
|
145
|
+
* @returns {boolean} - Returns true if the input is valid JSON and false if not.
|
|
146
|
+
*/
|
|
120
147
|
export function checkIsJson(input) {
|
|
121
148
|
try {
|
|
122
149
|
JSON.parse(input);
|
|
@@ -126,51 +153,53 @@ export function checkIsJson(input) {
|
|
|
126
153
|
}
|
|
127
154
|
}
|
|
128
155
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
156
|
+
/**
|
|
157
|
+
* Checks for number values in a string (Uses RegEx)
|
|
158
|
+
* @param {string} string
|
|
159
|
+
* @returns {boolean} - Returns true if a number is found and false if not.
|
|
160
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
161
|
+
*/
|
|
162
|
+
export function checkNum(string) {
|
|
132
163
|
try {
|
|
133
|
-
if (
|
|
134
|
-
|
|
164
|
+
if (typeof string !== 'string' || string.length === 0) {
|
|
165
|
+
return false;
|
|
135
166
|
};
|
|
136
|
-
|
|
137
|
-
if (regNm.test(str) === true) {
|
|
138
|
-
return true;
|
|
139
|
-
}
|
|
140
|
-
return false;
|
|
167
|
+
return regExNumbers.test(string);
|
|
141
168
|
} catch (er) {
|
|
142
|
-
throw new Error(er);
|
|
169
|
+
throw new Error(er.message);
|
|
143
170
|
}
|
|
144
171
|
}
|
|
145
172
|
|
|
146
|
-
|
|
147
|
-
|
|
173
|
+
/**
|
|
174
|
+
* Checks for lowercase characters (Uses RegEx)
|
|
175
|
+
* @param {string} string
|
|
176
|
+
* @returns {boolean} - Returns true if a lowercase character is found and false if not.
|
|
177
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
178
|
+
*/
|
|
179
|
+
export function checkLowercase(string) {
|
|
148
180
|
try {
|
|
149
|
-
if (
|
|
150
|
-
|
|
181
|
+
if (typeof string !== 'string' || string.length === 0) {
|
|
182
|
+
return false;
|
|
151
183
|
};
|
|
152
|
-
|
|
153
|
-
if (regLo.test(str) == true) {
|
|
154
|
-
return true;
|
|
155
|
-
}
|
|
156
|
-
return false;
|
|
184
|
+
return regExLettersLower.test(string);
|
|
157
185
|
} catch (er) {
|
|
158
|
-
throw new Error(er);
|
|
186
|
+
throw new Error(er.message);
|
|
159
187
|
}
|
|
160
188
|
}
|
|
161
189
|
|
|
162
|
-
|
|
163
|
-
|
|
190
|
+
/**
|
|
191
|
+
* Checks for uppercase characters (Uses RegEx)
|
|
192
|
+
* @param {string} string
|
|
193
|
+
* @returns {boolean} - Returns true if a uppercase character is found and false if not.
|
|
194
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
195
|
+
*/
|
|
196
|
+
export function checkUppercase(string) {
|
|
164
197
|
try {
|
|
165
|
-
if (
|
|
166
|
-
|
|
198
|
+
if (typeof string !== 'string' || string.length === 0) {
|
|
199
|
+
return false;
|
|
167
200
|
};
|
|
168
|
-
|
|
169
|
-
if (regUp.test(str) == true) {
|
|
170
|
-
return true;
|
|
171
|
-
}
|
|
172
|
-
return false;
|
|
201
|
+
return regExLettersUpper.test(string);
|
|
173
202
|
} catch (er) {
|
|
174
|
-
throw new Error(er);
|
|
203
|
+
throw new Error(er.message);
|
|
175
204
|
}
|
|
176
205
|
}
|
package/src/esm/lib/util.dom.js
CHANGED
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Takes an HTML template literal, parses it
|
|
2
|
+
* Takes an HTML template literal, parses it, then extracts it.
|
|
3
|
+
* All elements in the template MUST be contained within a single set of template tags.
|
|
4
|
+
* THIS IS THE RECOMMENDED PARSER TO USE.
|
|
5
|
+
* @param {string} templateLit - An HTML string containing a <template> tag.
|
|
6
|
+
* @returns {DocumentFragment} - Returns a DocumentFragment which has been parsed and queried.
|
|
7
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
8
|
+
*/
|
|
9
|
+
export function parseTemplate(templateLit) {
|
|
10
|
+
try {
|
|
11
|
+
let parser = new DOMParser();
|
|
12
|
+
let doc = parser.parseFromString(templateLit, 'text/html');
|
|
13
|
+
let template = doc.querySelector('template');
|
|
14
|
+
if (!template) {
|
|
15
|
+
throw new Error('No template tag found in the provided string.');
|
|
16
|
+
}
|
|
17
|
+
return template.content;
|
|
18
|
+
} catch (er) {
|
|
19
|
+
throw new Error(er.message);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Takes an HTML template literal, parses it, then extracts the element.
|
|
25
|
+
* All elements in the template MUST be contained within a single parent element.
|
|
26
|
+
* @param {string} templateLit
|
|
27
|
+
* @returns {Element} - Returns a element Object which has been parsed and queried.
|
|
28
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
3
29
|
*/
|
|
4
30
|
export function elmCleaner(templateLit) {
|
|
5
31
|
try {
|
|
@@ -8,13 +34,18 @@ export function elmCleaner(templateLit) {
|
|
|
8
34
|
let elms = elmBody.body.querySelectorAll("*");
|
|
9
35
|
return elms[0];
|
|
10
36
|
} catch (er) {
|
|
11
|
-
|
|
12
|
-
throw new Error(er);
|
|
37
|
+
throw new Error(er.message);
|
|
13
38
|
}
|
|
14
39
|
}
|
|
15
40
|
|
|
16
41
|
/**
|
|
17
|
-
* Takes an HTML table row template literal, parses it
|
|
42
|
+
* Takes an HTML table row template literal, parses it, then extracts the element.
|
|
43
|
+
* @param {string} templateLit
|
|
44
|
+
* @returns {Element} - Returns a element Object which has been parsed and queried.
|
|
45
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
46
|
+
*/
|
|
47
|
+
/**
|
|
48
|
+
* IN WORK
|
|
18
49
|
*/
|
|
19
50
|
export function elmCleanerTr(templateLit) {
|
|
20
51
|
try {
|
|
@@ -24,13 +55,18 @@ export function elmCleanerTr(templateLit) {
|
|
|
24
55
|
elmTemp.remove();
|
|
25
56
|
return elms;
|
|
26
57
|
} catch (er) {
|
|
27
|
-
|
|
28
|
-
throw new Error(er);
|
|
58
|
+
throw new Error(er.message);
|
|
29
59
|
}
|
|
30
60
|
}
|
|
31
61
|
|
|
32
62
|
/**
|
|
33
63
|
* Takes an HTML template literal, parses it with the DOM parser, then extracts the NodeList. The list is then returned as an array.
|
|
64
|
+
* @param {string} templateLit
|
|
65
|
+
* @returns {Element} - Returns a element Object which has been parsed and queried.
|
|
66
|
+
* @throws {Error} - Throws error message if error occurs.
|
|
67
|
+
*/
|
|
68
|
+
/**
|
|
69
|
+
* IN WORK
|
|
34
70
|
*/
|
|
35
71
|
export function elmCleanerArray(templateLit) {
|
|
36
72
|
try {
|
|
@@ -39,25 +75,6 @@ export function elmCleanerArray(templateLit) {
|
|
|
39
75
|
let elms = elmBody.body.querySelectorAll("*");
|
|
40
76
|
return Array.from(elms);
|
|
41
77
|
} catch (er) {
|
|
42
|
-
|
|
43
|
-
throw new Error(er);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Parses template literal with 'template' tag
|
|
49
|
-
*/
|
|
50
|
-
export function parseTemplate(templateLit) {
|
|
51
|
-
try {
|
|
52
|
-
let parser = new DOMParser();
|
|
53
|
-
let doc = parser.parseFromString(templateLit, 'text/html');
|
|
54
|
-
let template = doc.querySelector('template');
|
|
55
|
-
if (!template) {
|
|
56
|
-
throw new Error('No template tag found in the provided string.');
|
|
57
|
-
}
|
|
58
|
-
return template.content;
|
|
59
|
-
} catch (er) {
|
|
60
|
-
console.error(er);
|
|
61
|
-
throw new Error(er);
|
|
78
|
+
throw new Error(er.message);
|
|
62
79
|
}
|
|
63
80
|
}
|
package/src/esm/lib/util.http.js
CHANGED
|
@@ -1,70 +1,100 @@
|
|
|
1
|
-
import { checkUrl } from './util.check.js';
|
|
1
|
+
import { checkUrl, checkIsJson } from './util.check.js';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Adds 'http://' if valid URL and 'http://' or 'https://' is missing.
|
|
5
|
+
* @param {string} url
|
|
6
|
+
* @returns {string} - Returns an updated url string if necessary or returns the same string if url already starts with 'http://' or 'https://'.
|
|
7
|
+
* @throws {Error} - Throws Error if an error is detected.
|
|
8
|
+
*/
|
|
4
9
|
export function urlAddHttp(url) {
|
|
5
10
|
try {
|
|
6
11
|
if (url === null || !checkUrl(url)) {
|
|
7
|
-
throw `Invalid input
|
|
12
|
+
throw new Error(`Invalid input.`);
|
|
8
13
|
};
|
|
9
|
-
if (url.startsWith("http://")
|
|
14
|
+
if (url.startsWith("http://") === false && url.startsWith("https://") === false) {
|
|
10
15
|
url = "http://" + url
|
|
11
16
|
}
|
|
12
17
|
return url;
|
|
13
18
|
} catch (er) {
|
|
14
|
-
throw new Error(er);
|
|
19
|
+
throw new Error(er.message);
|
|
15
20
|
}
|
|
16
21
|
}
|
|
17
22
|
|
|
18
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Adds 'https://' if valid URL and 'http://' or 'https://' is missing.
|
|
25
|
+
* @param {string} url
|
|
26
|
+
* @returns {string} - Returns an updated url string if necessary or returns the same string if url already starts with 'http://' or 'https://'.
|
|
27
|
+
* @throws {Error} - Throws Error if an error is detected.
|
|
28
|
+
*/
|
|
19
29
|
export function urlAddHttps(url) {
|
|
20
30
|
try {
|
|
21
31
|
if (url === null || !checkUrl(url)) {
|
|
22
|
-
throw `Invalid input
|
|
32
|
+
throw new Error(`Invalid input.`);
|
|
23
33
|
};
|
|
24
|
-
if (url.startsWith("http://")
|
|
34
|
+
if (url.startsWith("http://") === false && url.startsWith("https://") === false) {
|
|
25
35
|
url = "https://" + url
|
|
26
36
|
}
|
|
27
37
|
return url;
|
|
28
38
|
} catch (er) {
|
|
29
|
-
throw new Error(er);
|
|
39
|
+
throw new Error(er.message);
|
|
30
40
|
}
|
|
31
41
|
}
|
|
32
42
|
|
|
33
|
-
|
|
34
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Collects and parses data from a JSON file
|
|
45
|
+
* @async
|
|
46
|
+
* @param {string} filePath
|
|
47
|
+
* @returns {Promise<Object>} - Returns data if response is ok, otherwise it throws an Error.
|
|
48
|
+
* @throws {Error} - Throws Error if an error is detected.
|
|
49
|
+
*/
|
|
50
|
+
export async function reqFileJson(filePath) {
|
|
35
51
|
try {
|
|
36
|
-
const res = await fetch(
|
|
52
|
+
const res = await fetch(filePath);
|
|
37
53
|
if (!res.ok) {
|
|
38
|
-
|
|
54
|
+
throw new Error(res);
|
|
39
55
|
}
|
|
40
56
|
const data = await res.json();
|
|
41
57
|
return data;
|
|
42
58
|
} catch (er) {
|
|
43
|
-
throw new Error(er);
|
|
59
|
+
throw new Error(er.message);
|
|
44
60
|
}
|
|
45
61
|
}
|
|
46
62
|
|
|
47
|
-
|
|
63
|
+
/**
|
|
64
|
+
* Sends GET request to specified URL
|
|
65
|
+
* @async
|
|
66
|
+
* @param {string} url
|
|
67
|
+
* @returns {Promise<Object>} - Returns response if response is ok, otherwise it throws an Error.
|
|
68
|
+
* @throws {Error} - Throws Error if an error is detected.
|
|
69
|
+
*/
|
|
48
70
|
export async function reqGet(url) {
|
|
49
71
|
try {
|
|
50
72
|
const res = await fetch(url, { method: 'GET' });
|
|
51
73
|
if (!res.ok) {
|
|
52
|
-
throw res;
|
|
74
|
+
throw new Error(res);
|
|
53
75
|
}
|
|
54
76
|
return res;
|
|
55
77
|
} catch (er) {
|
|
56
|
-
throw er;
|
|
78
|
+
throw new Error(er.message);
|
|
57
79
|
}
|
|
58
80
|
}
|
|
59
81
|
|
|
60
|
-
|
|
61
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Sends POST request to specified URL, which contains JSON data in the body.
|
|
84
|
+
* @async
|
|
85
|
+
* @param {string} url
|
|
86
|
+
* @param {JSON} dataJson
|
|
87
|
+
* @returns {Promise<Object>} - Returns response if response is ok, otherwise it throws an Error.
|
|
88
|
+
* @throws {Error} - Throws Error if an error is detected.
|
|
89
|
+
*/
|
|
62
90
|
export async function reqPostJson(url, dataJson) {
|
|
63
91
|
try {
|
|
64
92
|
if (dataJson === null || dataJson === undefined) {
|
|
65
|
-
throw `No
|
|
93
|
+
throw new Error(`No data provided.`);
|
|
94
|
+
}
|
|
95
|
+
if (!checkIsJson(dataJson)) {
|
|
96
|
+
throw new Error(`Provided data is not JSON.`);
|
|
66
97
|
}
|
|
67
|
-
JSON.parse(dataJson);
|
|
68
98
|
const res = await fetch(url, {
|
|
69
99
|
method: 'POST',
|
|
70
100
|
headers: {
|
|
@@ -73,15 +103,18 @@ export async function reqPostJson(url, dataJson) {
|
|
|
73
103
|
body: dataJson
|
|
74
104
|
});
|
|
75
105
|
if (!res.ok) {
|
|
76
|
-
|
|
106
|
+
throw new Error(res);
|
|
77
107
|
}
|
|
78
108
|
return res;
|
|
79
109
|
} catch (er) {
|
|
80
|
-
throw new Error(er);
|
|
110
|
+
throw new Error(er.message);
|
|
81
111
|
}
|
|
82
112
|
}
|
|
113
|
+
|
|
83
114
|
// Simple POST request. export function is expecting FormData.
|
|
84
|
-
|
|
115
|
+
/**
|
|
116
|
+
* IN WORK
|
|
117
|
+
*/
|
|
85
118
|
export async function reqPostForm(url, dataForm) {
|
|
86
119
|
try {
|
|
87
120
|
if (!(dataForm instanceof FormData)) {
|
package/src/esm/lib/util.misc.js
CHANGED
|
@@ -1,30 +1,55 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Adds zero in front of numbers less than 10 and returns as a string.
|
|
3
|
+
* @param {number} num
|
|
4
|
+
* @returns {string}
|
|
5
|
+
* @throws {Error} - Throws Error if an error is detected.
|
|
6
|
+
*/
|
|
2
7
|
export function addLeadZero(num) {
|
|
3
8
|
try {
|
|
4
9
|
if (num === null || typeof num !== 'number' || num > 9) {
|
|
5
|
-
throw `Invalid input
|
|
10
|
+
throw new Error(`Invalid input.`);
|
|
6
11
|
};
|
|
7
|
-
|
|
12
|
+
return "0" + num;;
|
|
8
13
|
} catch (er) {
|
|
9
|
-
|
|
10
|
-
throw new Error(er);
|
|
14
|
+
throw new Error(er.message);
|
|
11
15
|
}
|
|
12
16
|
}
|
|
13
17
|
|
|
14
18
|
/**
|
|
15
19
|
* Generates a unique ID
|
|
20
|
+
* @param {number} [length=16] - The number of characters for the random part of the ID.
|
|
21
|
+
* @returns {string}
|
|
22
|
+
* @throws {Error} - Throws Error if input is invalid or another error is detected.
|
|
16
23
|
*/
|
|
17
|
-
export function generateUID() {
|
|
18
|
-
|
|
24
|
+
export function generateUID(length = 16) {
|
|
25
|
+
try {
|
|
26
|
+
if (typeof length !== 'number' || length <= 0) {
|
|
27
|
+
throw new Error(`Invalid input. The 'length' parameter must be a positive number.`);
|
|
28
|
+
}
|
|
29
|
+
const timestampPart = Date.now().toString(36);
|
|
30
|
+
const randomPart = Math.random().toString(36).slice(2, 2 + length);
|
|
31
|
+
return 'uid-' + timestampPart + '-' + randomPart;
|
|
32
|
+
} catch (er) {
|
|
33
|
+
throw new Error(er.message);
|
|
34
|
+
}
|
|
19
35
|
}
|
|
20
36
|
|
|
21
37
|
/**
|
|
22
|
-
*
|
|
38
|
+
* @typedef {string[]} ImgUrls - An array of URL strings
|
|
39
|
+
*/
|
|
40
|
+
/**
|
|
41
|
+
* Preloads images in cache.
|
|
42
|
+
* @param {ImgUrls} imgUrls - An array of URL strings
|
|
43
|
+
* @returns {void}
|
|
44
|
+
* @throws {Error} - Throws an Error if loading an image fails.
|
|
23
45
|
*/
|
|
24
46
|
export function preloadImages(imgUrls) {
|
|
25
47
|
imgUrls.forEach(url => {
|
|
26
48
|
const img = new Image();
|
|
27
49
|
img.src = url;
|
|
50
|
+
img.onerror = () => {
|
|
51
|
+
throw new Error(`Failed to load image: ${url}`);
|
|
52
|
+
};
|
|
28
53
|
});
|
|
29
54
|
}
|
|
30
55
|
|
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* List of RegEx variables for different patterns.
|
|
3
3
|
*/
|
|
4
|
-
export const
|
|
5
|
-
export const
|
|
6
|
-
export const
|
|
7
|
-
export const
|
|
8
|
-
export const
|
|
9
|
-
export const
|
|
10
|
-
export const
|
|
4
|
+
export const regExLetters = /^[a-zA-Z]+$/;
|
|
5
|
+
export const regExLettersLower = /^[a-z]+$/;
|
|
6
|
+
export const regExLettersUpper = /^[A-Z]+$/;
|
|
7
|
+
export const regExNumbers = /^\d+$/;
|
|
8
|
+
export const regExBinary = /^[01]+$/;
|
|
9
|
+
export const regExHexadecimal = /^[0-9A-Fa-f]+$/;
|
|
10
|
+
export const regExSpecial = /^[\!\@\#\$\%\^\&\*\(\)\_\+\-\=\[\]\{\}\|\;\:\'\"\,\.\<\>\/\?\`\\\~]+$/;
|
|
11
|
+
export 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,}))$/;
|
|
12
|
+
export const regExUrl = new RegExp('^(https?:\\/\\/)?' + // protocol
|
|
13
|
+
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
|
|
14
|
+
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
|
|
15
|
+
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
|
|
16
|
+
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
|
|
17
|
+
'(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator
|
|
18
|
+
export const regExEmail = /^[\w-\.]+@([\w-]+\.)+[a-zA-Z]{2,}$/;
|
|
11
19
|
|
|
12
20
|
/**
|
|
13
21
|
* Removes characters from a string based on a provided regex pattern.
|
|
@@ -16,12 +24,16 @@ export const special = /^[\!\@\#\$\%\^\&\*\(\)\_\+\-\=\[\]\{\}\|\;\'\:\"\,\.\<\>
|
|
|
16
24
|
* @return {string} The processed string with specified characters removed.
|
|
17
25
|
* @throws {Error} Throws an error if the first parameter is not a string or if the second parameter is not a RegExp.
|
|
18
26
|
*/
|
|
19
|
-
export function removeChar(string,
|
|
20
|
-
|
|
21
|
-
|
|
27
|
+
export function removeChar(string, regEx) {
|
|
28
|
+
try {
|
|
29
|
+
if (typeof string !== 'string') {
|
|
30
|
+
throw new Error(`First parameter must be a string.`);
|
|
31
|
+
}
|
|
32
|
+
if (!(regEx instanceof RegExp)) {
|
|
33
|
+
throw new Error(`Second parameter must be a RegExp.`);
|
|
34
|
+
}
|
|
35
|
+
return string.replace(regEx, '');
|
|
36
|
+
} catch (er) {
|
|
37
|
+
throw new Error(er.message);
|
|
22
38
|
}
|
|
23
|
-
if (!(regex instanceof RegExp)) {
|
|
24
|
-
throw new Error(`Second parameter must be a RegExp.`);
|
|
25
|
-
}
|
|
26
|
-
return string.replace(regex, '');
|
|
27
39
|
}
|