tuijs-util 2.0.3 → 2.0.5

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/src/lib/check.ts CHANGED
@@ -1,254 +1,254 @@
1
- import {
2
- regExAnyNumber,
3
- regExAnyLowercase,
4
- regExAnyUppercase,
5
- regExAnySpecial,
6
- regExFqdn,
7
- regExUrl,
8
- regExEmail,
9
- regExIpv4,
10
- regExIpv6,
11
- regExMacNoSeparator,
12
- regExMacColonPairs,
13
- regExMacColonQuads,
14
- regExMacHyphenPairs,
15
- regExMacHyphenQuads,
16
- regExMacDotPairs,
17
- regExMacDotQuads
18
- } from "./regex.js";
19
-
20
- /**
21
- * Checks for a valid FQDN (Uses RegEx).
22
- */
23
- export function checkFqdn(string: string): boolean {
24
- if (typeof string !== 'string' || string.length === 0) {
25
- return false;
26
- }
27
- return regExFqdn.test(string);
28
- }
29
-
30
-
31
- /**
32
- * Checks for a valid URL (Uses RegEx).
33
- */
34
- export function checkUrl(string: string): boolean {
35
- if (typeof string !== 'string' || string.length === 0) {
36
- return false;
37
- };
38
- return regExUrl.test(string);
39
- }
40
-
41
- /**
42
- * Checks for a valid Mac Address (Uses RegEx).
43
- * Returns true if test is successful and false if the string is not validated or the RegEx test fails.
44
- */
45
- export function checkMac(string: string): boolean {
46
- if (typeof string !== 'string' || string.length === 0) {
47
- return false;
48
- }
49
- if (
50
- regExMacNoSeparator.test(string) ||
51
- regExMacColonPairs.test(string) ||
52
- regExMacColonQuads.test(string) ||
53
- regExMacHyphenPairs.test(string) ||
54
- regExMacHyphenQuads.test(string) ||
55
- regExMacDotPairs.test(string) ||
56
- regExMacDotQuads.test(string)
57
- ) {
58
- return true;
59
- }
60
- return false;
61
- }
62
-
63
- /**
64
- * Checks for a valid IP (Uses checkIpv4 and checkIpv6).
65
- * Returns true if test is successful and false if the string is not validated or the RegEx test fails.
66
- */
67
- export function checkIp(string: string): boolean {
68
- return checkIpv4(string) || checkIpv6(string);
69
- }
70
-
71
- /**
72
- * Checks for a valid IPv4 (Uses RegEx).
73
- * Returns true if test is successful and false if the string is not validated or the RegEx test fails.
74
- */
75
- export function checkIpv4(string: string): boolean {
76
- if (typeof string !== 'string' || string.length === 0) {
77
- return false;
78
- }
79
- return regExIpv4.test(string);
80
- }
81
-
82
- /**
83
- * Checks for a valid IPv6 (Uses RegEx).
84
- * Returns true if test is successful and false if the string is not validated or the RegEx test fails.
85
- */
86
- export function checkIpv6(string: string): boolean {
87
- if (typeof string !== 'string' || string.length === 0) {
88
- return false;
89
- }
90
- return regExIpv6.test(string);
91
- }
92
-
93
- /**
94
- * Checks for special characters (Uses RegEx).
95
- * Returns true if any special character is found and false if the string is not validated or the RegEx test fails.
96
- */
97
- export function checkAnySpecialChar(string: string): boolean {
98
- if (typeof string !== 'string' || string.length === 0) {
99
- return false;
100
- };
101
- return regExAnySpecial.test(string);
102
- }
103
-
104
- /**
105
- * Checks for number values in a string (Uses RegEx)
106
- * Returns true if a number is found and false if not.
107
- */
108
- export function checkAnyNum(string: string): boolean {
109
- if (typeof string !== 'string' || string.length === 0) {
110
- return false;
111
- };
112
- return regExAnyNumber.test(string);
113
- }
114
-
115
- /**
116
- * Checks for lowercase characters (Uses RegEx)
117
- * Returns true if a lowercase character is found and false if not.
118
- */
119
- export function checkAnyLowercase(string: string): boolean {
120
- if (typeof string !== 'string' || string.length === 0) {
121
- return false;
122
- };
123
- return regExAnyLowercase.test(string);
124
- }
125
-
126
- /**
127
- * Checks for uppercase characters (Uses RegEx)
128
- * Returns true if a uppercase character is found and false if not.
129
- */
130
- export function checkAnyUppercase(string: string): boolean {
131
- if (typeof string !== 'string' || string.length === 0) {
132
- return false;
133
- };
134
- return regExAnyUppercase.test(string);
135
- }
136
-
137
- /**
138
- * Checks for a space in a string
139
- * Returns true if space is found and false if the string is not validated or if a space is not found.
140
- */
141
- export function checkAnySpaces(string: string): boolean {
142
- if (typeof string !== 'string' || string.length === 0) {
143
- return false;
144
- };
145
- return string.indexOf(' ') >= 0;
146
- }
147
-
148
- /**
149
- * Checks for a valid email address. (Uses RegEx).
150
- * Returns true if email pattern test is successful and false if the string is not validated or the RegEx test fails.
151
- */
152
- export function checkEmail(string: string): boolean {
153
- if (typeof string !== 'string' || string.length === 0) {
154
- return false;
155
- };
156
- return regExEmail.test(string);
157
- }
158
-
159
- /**
160
- * Checks if a string is a newline-separated list.
161
- * Returns true if the input is a string containing at least one newline character and false if not.
162
- */
163
- export function checkIsList(input: any): boolean {
164
- if (typeof input !== 'string' || input.length === 0) {
165
- return false;
166
- }
167
- if (input.includes('{') || input.includes('[') || input.includes(']') || input.includes('}')) {
168
- return false;
169
- }
170
- return input.includes('\n') || input.includes('\r') || input.includes(',');
171
- }
172
-
173
- /**
174
- * Checks an input to determine if it is an Array.
175
- * Returns true if the input is an Array and false if not.
176
- * NOTE - This probably doesn't need to exist but it is here for consistency with the other check functions.
177
- */
178
- export function checkIsArray(input: any): boolean {
179
- return Array.isArray(input);
180
- }
181
-
182
- /**
183
- * Checks an input to determine if it is an Object.
184
- * Returns true if the input is an Object and false if not.
185
- */
186
- export function checkIsObject(input: any): boolean {
187
- return input !== null && typeof input === 'object' && !Array.isArray(input);
188
- }
189
-
190
- /**
191
- * Checks an input to determine if it is valid JSON.
192
- * Returns true if the input is valid JSON and false if not.
193
- * Try catch is needed to prevent errors from being thrown while checker is working as expected.
194
- */
195
- export function checkIsJson(input: any): boolean {
196
- if (typeof input !== 'string') {
197
- return false;
198
- }
199
- try {
200
- JSON.parse(input);
201
- return true;
202
- } catch {
203
- return false;
204
- }
205
- }
206
-
207
- /**
208
- * Checks an input to determine if it is a DOM object.
209
- * Returns true if the input is a DOM object and false if not.
210
- */
211
- export function checkIsDomObject(input: any): boolean {
212
- return (
213
- input instanceof Element ||
214
- input instanceof Document ||
215
- input instanceof Window ||
216
- input instanceof DocumentFragment ||
217
- input instanceof ShadowRoot
218
- );
219
- }
220
-
221
- /**
222
- * Checks an input to determine if it is a Function.
223
- * Returns true if the input is a Function and false if not.
224
- * NOTE - This probably doesn't need to exist but it is here for consistency with the other check functions.
225
- */
226
- export function checkIsFunction(input: any): boolean {
227
- return typeof input === 'function';
228
- }
229
-
230
- /**
231
- * Checks an input to determine if it is a valid subnet mask.
232
- * Returns true if the input is a valid subnet mask and false if not.
233
- */
234
- export function checkSubnetMask(mask: string): boolean {
235
- const maskArray = mask.split('.').map(octet => parseInt(octet, 10));
236
- if (maskArray.length !== 4) {
237
- return false;
238
- }
239
- if (maskArray.some(num => Number.isNaN(num))) {
240
- return false;
241
- }
242
- const binaryMask = maskArray
243
- .map(num => num.toString(2).padStart(8, '0'))
244
- .join('');
245
- if (/01.*10/.test(binaryMask)) {
246
- return false;
247
- }
248
- for (let i = 0; i < maskArray.length; i++) {
249
- if (maskArray[i] > 255 || maskArray[i] < 0) {
250
- return false;
251
- }
252
- }
253
- return true;
254
- }
1
+ import {
2
+ regExAnyNumber,
3
+ regExAnyLowercase,
4
+ regExAnyUppercase,
5
+ regExAnySpecial,
6
+ regExFqdn,
7
+ regExUrl,
8
+ regExEmail,
9
+ regExIpv4,
10
+ regExIpv6,
11
+ regExMacNoSeparator,
12
+ regExMacColonPairs,
13
+ regExMacColonQuads,
14
+ regExMacHyphenPairs,
15
+ regExMacHyphenQuads,
16
+ regExMacDotPairs,
17
+ regExMacDotQuads
18
+ } from "./regex.js";
19
+
20
+ /**
21
+ * Checks for a valid FQDN (Uses RegEx).
22
+ */
23
+ export function checkFqdn(string: string): boolean {
24
+ if (typeof string !== 'string' || string.length === 0) {
25
+ return false;
26
+ }
27
+ return regExFqdn.test(string);
28
+ }
29
+
30
+
31
+ /**
32
+ * Checks for a valid URL (Uses RegEx).
33
+ */
34
+ export function checkUrl(string: string): boolean {
35
+ if (typeof string !== 'string' || string.length === 0) {
36
+ return false;
37
+ };
38
+ return regExUrl.test(string);
39
+ }
40
+
41
+ /**
42
+ * Checks for a valid Mac Address (Uses RegEx).
43
+ * Returns true if test is successful and false if the string is not validated or the RegEx test fails.
44
+ */
45
+ export function checkMac(string: string): boolean {
46
+ if (typeof string !== 'string' || string.length === 0) {
47
+ return false;
48
+ }
49
+ if (
50
+ regExMacNoSeparator.test(string) ||
51
+ regExMacColonPairs.test(string) ||
52
+ regExMacColonQuads.test(string) ||
53
+ regExMacHyphenPairs.test(string) ||
54
+ regExMacHyphenQuads.test(string) ||
55
+ regExMacDotPairs.test(string) ||
56
+ regExMacDotQuads.test(string)
57
+ ) {
58
+ return true;
59
+ }
60
+ return false;
61
+ }
62
+
63
+ /**
64
+ * Checks for a valid IP (Uses checkIpv4 and checkIpv6).
65
+ * Returns true if test is successful and false if the string is not validated or the RegEx test fails.
66
+ */
67
+ export function checkIp(string: string): boolean {
68
+ return checkIpv4(string) || checkIpv6(string);
69
+ }
70
+
71
+ /**
72
+ * Checks for a valid IPv4 (Uses RegEx).
73
+ * Returns true if test is successful and false if the string is not validated or the RegEx test fails.
74
+ */
75
+ export function checkIpv4(string: string): boolean {
76
+ if (typeof string !== 'string' || string.length === 0) {
77
+ return false;
78
+ }
79
+ return regExIpv4.test(string);
80
+ }
81
+
82
+ /**
83
+ * Checks for a valid IPv6 (Uses RegEx).
84
+ * Returns true if test is successful and false if the string is not validated or the RegEx test fails.
85
+ */
86
+ export function checkIpv6(string: string): boolean {
87
+ if (typeof string !== 'string' || string.length === 0) {
88
+ return false;
89
+ }
90
+ return regExIpv6.test(string);
91
+ }
92
+
93
+ /**
94
+ * Checks for special characters (Uses RegEx).
95
+ * Returns true if any special character is found and false if the string is not validated or the RegEx test fails.
96
+ */
97
+ export function checkAnySpecialChar(string: string): boolean {
98
+ if (typeof string !== 'string' || string.length === 0) {
99
+ return false;
100
+ };
101
+ return regExAnySpecial.test(string);
102
+ }
103
+
104
+ /**
105
+ * Checks for number values in a string (Uses RegEx)
106
+ * Returns true if a number is found and false if not.
107
+ */
108
+ export function checkAnyNum(string: string): boolean {
109
+ if (typeof string !== 'string' || string.length === 0) {
110
+ return false;
111
+ };
112
+ return regExAnyNumber.test(string);
113
+ }
114
+
115
+ /**
116
+ * Checks for lowercase characters (Uses RegEx)
117
+ * Returns true if a lowercase character is found and false if not.
118
+ */
119
+ export function checkAnyLowercase(string: string): boolean {
120
+ if (typeof string !== 'string' || string.length === 0) {
121
+ return false;
122
+ };
123
+ return regExAnyLowercase.test(string);
124
+ }
125
+
126
+ /**
127
+ * Checks for uppercase characters (Uses RegEx)
128
+ * Returns true if a uppercase character is found and false if not.
129
+ */
130
+ export function checkAnyUppercase(string: string): boolean {
131
+ if (typeof string !== 'string' || string.length === 0) {
132
+ return false;
133
+ };
134
+ return regExAnyUppercase.test(string);
135
+ }
136
+
137
+ /**
138
+ * Checks for a space in a string
139
+ * Returns true if space is found and false if the string is not validated or if a space is not found.
140
+ */
141
+ export function checkAnySpaces(string: string): boolean {
142
+ if (typeof string !== 'string' || string.length === 0) {
143
+ return false;
144
+ };
145
+ return string.indexOf(' ') >= 0;
146
+ }
147
+
148
+ /**
149
+ * Checks for a valid email address. (Uses RegEx).
150
+ * Returns true if email pattern test is successful and false if the string is not validated or the RegEx test fails.
151
+ */
152
+ export function checkEmail(string: string): boolean {
153
+ if (typeof string !== 'string' || string.length === 0) {
154
+ return false;
155
+ };
156
+ return regExEmail.test(string);
157
+ }
158
+
159
+ /**
160
+ * Checks if a string is a newline-separated list.
161
+ * Returns true if the input is a string containing at least one newline character and false if not.
162
+ */
163
+ export function checkIsList(input: any): boolean {
164
+ if (typeof input !== 'string' || input.length === 0) {
165
+ return false;
166
+ }
167
+ if (input.includes('{') || input.includes('[') || input.includes(']') || input.includes('}')) {
168
+ return false;
169
+ }
170
+ return input.includes('\n') || input.includes('\r') || input.includes(',');
171
+ }
172
+
173
+ /**
174
+ * Checks an input to determine if it is an Array.
175
+ * Returns true if the input is an Array and false if not.
176
+ * NOTE - This probably doesn't need to exist but it is here for consistency with the other check functions.
177
+ */
178
+ export function checkIsArray(input: any): boolean {
179
+ return Array.isArray(input);
180
+ }
181
+
182
+ /**
183
+ * Checks an input to determine if it is an Object.
184
+ * Returns true if the input is an Object and false if not.
185
+ */
186
+ export function checkIsObject(input: any): boolean {
187
+ return input !== null && typeof input === 'object' && !Array.isArray(input);
188
+ }
189
+
190
+ /**
191
+ * Checks an input to determine if it is valid JSON.
192
+ * Returns true if the input is valid JSON and false if not.
193
+ * Try catch is needed to prevent errors from being thrown while checker is working as expected.
194
+ */
195
+ export function checkIsJson(input: any): boolean {
196
+ if (typeof input !== 'string') {
197
+ return false;
198
+ }
199
+ try {
200
+ JSON.parse(input);
201
+ return true;
202
+ } catch {
203
+ return false;
204
+ }
205
+ }
206
+
207
+ /**
208
+ * Checks an input to determine if it is a DOM object.
209
+ * Returns true if the input is a DOM object and false if not.
210
+ */
211
+ export function checkIsDomObject(input: any): boolean {
212
+ return (
213
+ input instanceof Element ||
214
+ input instanceof Document ||
215
+ input instanceof Window ||
216
+ input instanceof DocumentFragment ||
217
+ input instanceof ShadowRoot
218
+ );
219
+ }
220
+
221
+ /**
222
+ * Checks an input to determine if it is a Function.
223
+ * Returns true if the input is a Function and false if not.
224
+ * NOTE - This probably doesn't need to exist but it is here for consistency with the other check functions.
225
+ */
226
+ export function checkIsFunction(input: any): boolean {
227
+ return typeof input === 'function';
228
+ }
229
+
230
+ /**
231
+ * Checks an input to determine if it is a valid subnet mask.
232
+ * Returns true if the input is a valid subnet mask and false if not.
233
+ */
234
+ export function checkSubnetMask(mask: string): boolean {
235
+ const maskArray = mask.split('.').map(octet => parseInt(octet, 10));
236
+ if (maskArray.length !== 4) {
237
+ return false;
238
+ }
239
+ if (maskArray.some(num => Number.isNaN(num))) {
240
+ return false;
241
+ }
242
+ const binaryMask = maskArray
243
+ .map(num => num.toString(2).padStart(8, '0'))
244
+ .join('');
245
+ if (/01.*10/.test(binaryMask)) {
246
+ return false;
247
+ }
248
+ for (let i = 0; i < maskArray.length; i++) {
249
+ if (maskArray[i] > 255 || maskArray[i] < 0) {
250
+ return false;
251
+ }
252
+ }
253
+ return true;
254
+ }
package/src/lib/dom.ts CHANGED
@@ -1,54 +1,54 @@
1
- /**
2
- * Takes an HTML template literal, parses it, then extracts it.
3
- */
4
- export function parseTemplate(templateLiteral: string): DocumentFragment {
5
- const parser: DOMParser = new DOMParser();
6
- const parsedDocument: Document = parser.parseFromString(templateLiteral, 'text/html');
7
- const element: HTMLTemplateElement | null = parsedDocument.querySelector('template');
8
- if (!element) {
9
- const temporaryDiv: HTMLDivElement = document.createElement('div');
10
- temporaryDiv.replaceChildren(...Array.from(parsedDocument.body.childNodes));
11
- const newFragment: DocumentFragment = document.createDocumentFragment();
12
- while (temporaryDiv.firstChild) {
13
- newFragment.appendChild(temporaryDiv.firstChild);
14
- }
15
- return newFragment;
16
- }
17
- const elementFragment: DocumentFragment = element.content;
18
- return elementFragment;
19
- }
20
-
21
- /**
22
- * Takes an HTML template literal, parses it, then extracts the element.
23
- * All elements in the template MUST be contained within a single parent element.
24
- */
25
- export function parseElement(templateLiteral: string): Element | undefined {
26
- const parser: DOMParser = new DOMParser();
27
- const parsedDocument: Document = parser.parseFromString(templateLiteral, 'text/html');
28
- const element: NodeListOf<Element> = parsedDocument.body.querySelectorAll("*");
29
- return element[0];
30
- }
31
-
32
- /**
33
- * Takes an HTML table row template literal, parses it, then extracts the element.
34
- */
35
- export function parseTableRow(templateLiteral: string): Element | null {
36
- const parser: DOMParser = new DOMParser();
37
- const parsedDocument: Document = parser.parseFromString(templateLiteral, 'text/html');
38
- const element: NodeListOf<Element> = parsedDocument.body.querySelectorAll("*");
39
- const temporaryTable = document.createElement('table');
40
- temporaryTable.replaceChildren(...Array.from(element));
41
- const tableRow: HTMLTableRowElement | null = temporaryTable.querySelector("tr");
42
- temporaryTable.remove();
43
- return tableRow;
44
- }
45
-
46
- /**
47
- * Takes an HTML template literal, parses it with the DOM parser, then extracts the NodeList. The list is then returned as an array.
48
- */
49
- export function listNodes(templateLiteral: string): Element[] {
50
- const parser: DOMParser = new DOMParser();
51
- const parsedDocument: Document = parser.parseFromString(templateLiteral, 'text/html');
52
- const elements: NodeListOf<Element> = parsedDocument.body.querySelectorAll("*");
53
- return Array.from(elements);
54
- }
1
+ /**
2
+ * Takes an HTML template literal, parses it, then extracts it.
3
+ */
4
+ export function parseTemplate(templateLiteral: string): DocumentFragment {
5
+ const parser: DOMParser = new DOMParser();
6
+ const parsedDocument: Document = parser.parseFromString(templateLiteral, 'text/html');
7
+ const element: HTMLTemplateElement | null = parsedDocument.querySelector('template');
8
+ if (!element) {
9
+ const temporaryDiv: HTMLDivElement = document.createElement('div');
10
+ temporaryDiv.replaceChildren(...Array.from(parsedDocument.body.childNodes));
11
+ const newFragment: DocumentFragment = document.createDocumentFragment();
12
+ while (temporaryDiv.firstChild) {
13
+ newFragment.appendChild(temporaryDiv.firstChild);
14
+ }
15
+ return newFragment;
16
+ }
17
+ const elementFragment: DocumentFragment = element.content;
18
+ return elementFragment;
19
+ }
20
+
21
+ /**
22
+ * Takes an HTML template literal, parses it, then extracts the element.
23
+ * All elements in the template MUST be contained within a single parent element.
24
+ */
25
+ export function parseElement(templateLiteral: string): Element | undefined {
26
+ const parser: DOMParser = new DOMParser();
27
+ const parsedDocument: Document = parser.parseFromString(templateLiteral, 'text/html');
28
+ const element: NodeListOf<Element> = parsedDocument.body.querySelectorAll("*");
29
+ return element[0];
30
+ }
31
+
32
+ /**
33
+ * Takes an HTML table row template literal, parses it, then extracts the element.
34
+ */
35
+ export function parseTableRow(templateLiteral: string): Element | null {
36
+ const parser: DOMParser = new DOMParser();
37
+ const parsedDocument: Document = parser.parseFromString(templateLiteral, 'text/html');
38
+ const element: NodeListOf<Element> = parsedDocument.body.querySelectorAll("*");
39
+ const temporaryTable = document.createElement('table');
40
+ temporaryTable.replaceChildren(...Array.from(element));
41
+ const tableRow: HTMLTableRowElement | null = temporaryTable.querySelector("tr");
42
+ temporaryTable.remove();
43
+ return tableRow;
44
+ }
45
+
46
+ /**
47
+ * Takes an HTML template literal, parses it with the DOM parser, then extracts the NodeList. The list is then returned as an array.
48
+ */
49
+ export function listNodes(templateLiteral: string): Element[] {
50
+ const parser: DOMParser = new DOMParser();
51
+ const parsedDocument: Document = parser.parseFromString(templateLiteral, 'text/html');
52
+ const elements: NodeListOf<Element> = parsedDocument.body.querySelectorAll("*");
53
+ return Array.from(elements);
54
+ }