tuijs-util 1.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 TechTB
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # TUIJS-UTIL
2
+
3
+ ## Description
4
+ A simple JavaScript utility library
5
+
6
+ ## Install
7
+ npm i tuijs-util
8
+
9
+ ## Import
10
+ All utilities are exported both as a module and individually. Simply import the module or the specific utility you want.
11
+
12
+ ## Examples
13
+ ```javascript
14
+ import { utilFunctionName } from 'ttbjs';
15
+ utilFunctionName();
16
+ ```
17
+
18
+ ```javascript
19
+ import { utilModule } from 'ttbjs';
20
+ utilModule.utilFunctionName();
21
+ ```
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "tuijs-util",
3
+ "version": "1.1.4",
4
+ "module": "src/esm/index.js",
5
+ "main": "src/cjs/index.cjs",
6
+ "exports": {
7
+ ".": {
8
+ "import": "./src/esm/index.js",
9
+ "require": "./src/cjs/index.cjs"
10
+ }
11
+ },
12
+ "description": "A simple JavaScript utility library",
13
+ "author": "TechTB",
14
+ "license": "MIT",
15
+ "repository": "github:TechTB-OpenSource/tuijs-util",
16
+ "type": "module",
17
+ "scripts": {
18
+ "roll": "rollup -c rollup.config.js"
19
+ },
20
+ "devDependencies": {
21
+ "@rollup/plugin-babel": "^6.0.4",
22
+ "@rollup/plugin-node-resolve": "^15.2.3",
23
+ "rollup": "^4.16.4"
24
+ }
25
+ }
@@ -0,0 +1,546 @@
1
+ 'use strict';
2
+
3
+ // Checks for a valid FQDN (uses regex)
4
+ function checkFqdn$1(str) {
5
+ if (typeof str !== "string" || str.length === 0 || str.length > 253) {
6
+ return false;
7
+ }
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
+ }
11
+
12
+ // Checks for a valid URL (uses regex)
13
+ function checkUrl$1(str) {
14
+ try {
15
+ if (str === null || typeof str !== "string") {
16
+ throw new Error(`Invalid input.`);
17
+ }
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
31
+ } catch (er) {
32
+ throw new Error(er);
33
+ }
34
+ }
35
+
36
+ // Checks for special characters (Returns true if it matches)
37
+ function checkSpecialChar$1(str) {
38
+ try {
39
+ if (str === null || typeof str !== "string") {
40
+ throw new Error(`Invalid input.`);
41
+ }
42
+ ;
43
+ const regSp = /[\!\@\#\$\%\^\&\*\(\)\_\+\=\[\]\{\}\?]/;
44
+ if (regSp.test(str) == true) {
45
+ return true;
46
+ }
47
+ return false;
48
+ } catch (er) {
49
+ throw new Error(er);
50
+ }
51
+ }
52
+
53
+ // Checks for a valid Email (uses regex)
54
+ function checkEmail$1(str) {
55
+ try {
56
+ if (str === null || typeof str !== "string") {
57
+ throw new Error(`Invalid input.`);
58
+ }
59
+ ;
60
+ var validRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
61
+ if (str.match(validRegex)) {
62
+ return true;
63
+ }
64
+ return false;
65
+ } catch (er) {
66
+ throw new Error(er);
67
+ }
68
+ }
69
+
70
+ // Checks for a space in a string
71
+ function checkSpaces$1(str) {
72
+ try {
73
+ if (str === null || typeof str !== "string") {
74
+ throw new Error(`Invalid input.`);
75
+ }
76
+ ;
77
+ return str.indexOf(' ') >= 0;
78
+ } catch (er) {
79
+ throw new Error(er);
80
+ }
81
+ }
82
+
83
+ // Check for a list of text???
84
+ function checkIsList$1(input) {
85
+ try {
86
+ // Check if the variable is a string
87
+ if (typeof input !== 'string') {
88
+ return false;
89
+ }
90
+ // Split the string by comma
91
+ const items = input.split(',');
92
+ // Check if all items are non-empty strings
93
+ for (let item of items) {
94
+ item = item.trim(); // Trim any leading or trailing whitespace
95
+ if (item === '') {
96
+ return false; // Empty item found
97
+ }
98
+ }
99
+ return true;
100
+ } catch (er) {
101
+ throw new Error(er);
102
+ }
103
+ }
104
+ function checkIsArray$1(input) {
105
+ try {
106
+ return input.constructor === Array;
107
+ } catch (er) {
108
+ throw new Error(er);
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Checks if an input value is an object.
114
+ * @param {*} input - Any variable
115
+ * @returns {boolean}
116
+ */
117
+ function checkIsObject$1(input) {
118
+ try {
119
+ return input.constructor === Object;
120
+ } catch (er) {
121
+ throw new Error(er);
122
+ }
123
+ }
124
+ function checkIsJson$1(input) {
125
+ try {
126
+ JSON.parse(input);
127
+ return true;
128
+ } catch (er) {
129
+ return false;
130
+ }
131
+ }
132
+
133
+ // Checks for number values in a string (Returns true if it matches)
134
+ function checkNum$1(str) {
135
+ try {
136
+ if (str === null || typeof str !== "string") {
137
+ throw new Error(`Invalid input.`);
138
+ }
139
+ ;
140
+ const regNm = /[0-9]/;
141
+ if (regNm.test(str) === true) {
142
+ return true;
143
+ }
144
+ return false;
145
+ } catch (er) {
146
+ throw new Error(er);
147
+ }
148
+ }
149
+
150
+ // Checks for lowercase characters (Returns true if it matches)
151
+ function checkLowercase$1(str) {
152
+ try {
153
+ if (str === null || typeof str !== "string") {
154
+ throw new Error(`Invalid input.`);
155
+ }
156
+ ;
157
+ const regLo = /[a-z]/;
158
+ if (regLo.test(str) == true) {
159
+ return true;
160
+ }
161
+ return false;
162
+ } catch (er) {
163
+ throw new Error(er);
164
+ }
165
+ }
166
+
167
+ // Checks for uppercase characters (Returns true if it matches)
168
+ function checkUppercase$1(str) {
169
+ try {
170
+ if (str === null || typeof str !== "string") {
171
+ throw new Error(`Invalid input.`);
172
+ }
173
+ ;
174
+ const regUp = /[A-Z]/;
175
+ if (regUp.test(str) == true) {
176
+ return true;
177
+ }
178
+ return false;
179
+ } catch (er) {
180
+ throw new Error(er);
181
+ }
182
+ }
183
+
184
+ var check = /*#__PURE__*/Object.freeze({
185
+ __proto__: null,
186
+ checkEmail: checkEmail$1,
187
+ checkFqdn: checkFqdn$1,
188
+ checkIsArray: checkIsArray$1,
189
+ checkIsJson: checkIsJson$1,
190
+ checkIsList: checkIsList$1,
191
+ checkIsObject: checkIsObject$1,
192
+ checkLowercase: checkLowercase$1,
193
+ checkNum: checkNum$1,
194
+ checkSpaces: checkSpaces$1,
195
+ checkSpecialChar: checkSpecialChar$1,
196
+ checkUppercase: checkUppercase$1,
197
+ checkUrl: checkUrl$1
198
+ });
199
+
200
+ /**
201
+ * Takes an HTML template literal, parses it with the DOM parser, then extracts the element with querySelectorAll.
202
+ */
203
+ function elmCleaner$1(templateLit) {
204
+ try {
205
+ let parser = new DOMParser();
206
+ let elmBody = parser.parseFromString(templateLit, 'text/html');
207
+ let elms = elmBody.body.querySelectorAll("*");
208
+ return elms[0];
209
+ } catch (er) {
210
+ console.error(er);
211
+ throw new Error(er);
212
+ }
213
+ }
214
+
215
+ /**
216
+ * Takes an HTML table row template literal, parses it with the DOM parser, then extracts the element with querySelectorAll.
217
+ */
218
+ function elmCleanerTr$1(templateLit) {
219
+ try {
220
+ let elmTemp = document.createElement('table');
221
+ elmTemp.innerHTML = templateLit;
222
+ let elms = elmTemp.querySelector("tr");
223
+ elmTemp.remove();
224
+ return elms;
225
+ } catch (er) {
226
+ console.error(er);
227
+ throw new Error(er);
228
+ }
229
+ }
230
+
231
+ /**
232
+ * Takes an HTML template literal, parses it with the DOM parser, then extracts the NodeList. The list is then returned as an array.
233
+ */
234
+ function elmCleanerArray$1(templateLit) {
235
+ try {
236
+ let parser = new DOMParser();
237
+ let elmBody = parser.parseFromString(templateLit, 'text/html');
238
+ let elms = elmBody.body.querySelectorAll("*");
239
+ return Array.from(elms);
240
+ } catch (er) {
241
+ console.error(er);
242
+ throw new Error(er);
243
+ }
244
+ }
245
+
246
+ /**
247
+ * Parses template literal with 'template' tag
248
+ */
249
+ function parseTemplate$1(templateLit) {
250
+ try {
251
+ let parser = new DOMParser();
252
+ let doc = parser.parseFromString(templateLit, 'text/html');
253
+ let template = doc.querySelector('template');
254
+ if (!template) {
255
+ throw new Error('No template tag found in the provided string.');
256
+ }
257
+ return template.content;
258
+ } catch (er) {
259
+ console.error(er);
260
+ throw new Error(er);
261
+ }
262
+ }
263
+
264
+ var dom = /*#__PURE__*/Object.freeze({
265
+ __proto__: null,
266
+ elmCleaner: elmCleaner$1,
267
+ elmCleanerArray: elmCleanerArray$1,
268
+ elmCleanerTr: elmCleanerTr$1,
269
+ parseTemplate: parseTemplate$1
270
+ });
271
+
272
+ // Adds 'http://' if valid URL
273
+ function urlAddHttp$1(url) {
274
+ try {
275
+ if (url === null || !checkUrl$1(url)) {
276
+ throw `Invalid input.`;
277
+ }
278
+ ;
279
+ if (url.startsWith("http://") == false && url.startsWith("https://") == false) {
280
+ url = "http://" + url;
281
+ }
282
+ return url;
283
+ } catch (er) {
284
+ throw new Error(er);
285
+ }
286
+ }
287
+
288
+ // Adds 'https://' if valid URL
289
+ function urlAddHttps$1(url) {
290
+ try {
291
+ if (url === null || !checkUrl$1(url)) {
292
+ throw `Invalid input.`;
293
+ }
294
+ ;
295
+ if (url.startsWith("http://") == false && url.startsWith("https://") == false) {
296
+ url = "https://" + url;
297
+ }
298
+ return url;
299
+ } catch (er) {
300
+ throw new Error(er);
301
+ }
302
+ }
303
+
304
+ // Simple read JSON file utility
305
+ async function reqFileJson$1(file) {
306
+ try {
307
+ const res = await fetch(file);
308
+ if (!res.ok) {
309
+ return res;
310
+ }
311
+ const data = await res.json();
312
+ return data;
313
+ } catch (er) {
314
+ throw new Error(er);
315
+ }
316
+ }
317
+
318
+ // Simple GET request - This is expecting JSON data from the target URL
319
+ async function reqGet$1(url) {
320
+ try {
321
+ const res = await fetch(url, {
322
+ method: 'GET'
323
+ });
324
+ if (!res.ok) {
325
+ throw res;
326
+ }
327
+ return res;
328
+ } catch (er) {
329
+ throw er;
330
+ }
331
+ }
332
+
333
+ // Simple POST request. export function is expecting JSON data. The JSON.parse will throw an error if data is not valid JSON.
334
+ // NEEDS REVIEW
335
+ async function reqPostJson$1(url, dataJson) {
336
+ try {
337
+ if (dataJson === null || dataJson === undefined) {
338
+ throw `No JSON data provided`;
339
+ }
340
+ JSON.parse(dataJson);
341
+ const res = await fetch(url, {
342
+ method: 'POST',
343
+ headers: {
344
+ 'Content-Type': 'application/json'
345
+ },
346
+ body: dataJson
347
+ });
348
+ if (!res.ok) {
349
+ return res;
350
+ }
351
+ return res;
352
+ } catch (er) {
353
+ throw new Error(er);
354
+ }
355
+ }
356
+ // Simple POST request. export function is expecting FormData.
357
+ // NEEDS REVIEW
358
+ async function reqPostForm$1(url, dataForm) {
359
+ try {
360
+ if (!(dataForm instanceof FormData)) {
361
+ throw `The data provided was not form data`;
362
+ }
363
+ const res = await fetch(url, {
364
+ method: 'POST',
365
+ body: dataForm
366
+ });
367
+ if (!res.ok) {
368
+ return res;
369
+ }
370
+ return res;
371
+ } catch (er) {
372
+ throw new Error(er);
373
+ }
374
+ }
375
+
376
+ var http = /*#__PURE__*/Object.freeze({
377
+ __proto__: null,
378
+ reqFileJson: reqFileJson$1,
379
+ reqGet: reqGet$1,
380
+ reqPostForm: reqPostForm$1,
381
+ reqPostJson: reqPostJson$1,
382
+ urlAddHttp: urlAddHttp$1,
383
+ urlAddHttps: urlAddHttps$1
384
+ });
385
+
386
+ const htmlTags$1 = ["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"];
387
+
388
+ var lists = /*#__PURE__*/Object.freeze({
389
+ __proto__: null,
390
+ htmlTags: htmlTags$1
391
+ });
392
+
393
+ // Adds zero in front of numbers less than 10
394
+ function addLeadZero$1(num) {
395
+ try {
396
+ if (num === null || typeof num !== 'number' || num > 9) {
397
+ throw `Invalid input.`;
398
+ }
399
+ ;
400
+ num = "0" + num;
401
+ } catch (er) {
402
+ console.error(er);
403
+ throw new Error(er);
404
+ }
405
+ }
406
+
407
+ /**
408
+ * Generates a unique ID
409
+ */
410
+ function generateUID$1() {
411
+ return 'uid-' + Date.now().toString(36) + '-' + Math.random().toString(36).substr(2);
412
+ }
413
+
414
+ var misc = /*#__PURE__*/Object.freeze({
415
+ __proto__: null,
416
+ addLeadZero: addLeadZero$1,
417
+ generateUID: generateUID$1
418
+ });
419
+
420
+ /**
421
+ * List of RegEx variables for different patterns.
422
+ */
423
+ const letters$1 = /^[a-zA-Z]+$/;
424
+ const lettersLower$1 = /^[a-z]+$/;
425
+ const lettersUpper$1 = /^[A-Z]+$/;
426
+ const numbers$1 = /^\d+$/;
427
+ const binary$1 = /[^01]/g;
428
+ const hexadecimal$1 = /[^0-9A-Fa-f]/g;
429
+ const special$1 = /^[\!\@\#\$\%\^\&\*\(\)\_\+\-\=\[\]\{\}\|\;\'\:\"\,\.\<\>\?\/]+$/;
430
+
431
+ /**
432
+ * Removes characters from a string based on a provided regex pattern.
433
+ * @param {string} string The string to process.
434
+ * @param {RegExp} regex The regex pattern of characters to remove from the string.
435
+ * @return {string} The processed string with specified characters removed.
436
+ * @throws {Error} Throws an error if the first parameter is not a string or if the second parameter is not a RegExp.
437
+ */
438
+ function removeChar$1(string, regex) {
439
+ if (typeof string !== 'string') {
440
+ throw new Error(`First parameter must be a string.`);
441
+ }
442
+ if (!(regex instanceof RegExp)) {
443
+ throw new Error(`Second parameter must be a RegExp.`);
444
+ }
445
+ return string.replace(regex, '');
446
+ }
447
+
448
+ var regex = /*#__PURE__*/Object.freeze({
449
+ __proto__: null,
450
+ binary: binary$1,
451
+ hexadecimal: hexadecimal$1,
452
+ letters: letters$1,
453
+ lettersLower: lettersLower$1,
454
+ lettersUpper: lettersUpper$1,
455
+ numbers: numbers$1,
456
+ removeChar: removeChar$1,
457
+ special: special$1
458
+ });
459
+
460
+ const {
461
+ checkEmail,
462
+ checkFqdn,
463
+ checkIsArray,
464
+ checkIsJson,
465
+ checkIsList,
466
+ checkIsObject,
467
+ checkLowercase,
468
+ checkNum,
469
+ checkSpaces,
470
+ checkSpecialChar,
471
+ checkUppercase,
472
+ checkUrl
473
+ } = check;
474
+ const {
475
+ elmCleaner,
476
+ elmCleanerArray,
477
+ elmCleanerTr,
478
+ parseTemplate
479
+ } = dom;
480
+ const {
481
+ reqFileJson,
482
+ reqGet,
483
+ reqPostForm,
484
+ reqPostJson,
485
+ urlAddHttp,
486
+ urlAddHttps
487
+ } = http;
488
+ const {
489
+ htmlTags
490
+ } = lists;
491
+ const {
492
+ addLeadZero,
493
+ generateUID,
494
+ preloadImages
495
+ } = misc;
496
+ const {
497
+ binary,
498
+ hexadecimal,
499
+ letters,
500
+ lettersLower,
501
+ lettersUpper,
502
+ numbers,
503
+ removeChar,
504
+ special
505
+ } = regex;
506
+
507
+ exports.addLeadZero = addLeadZero;
508
+ exports.binary = binary;
509
+ exports.check = check;
510
+ exports.checkEmail = checkEmail;
511
+ exports.checkFqdn = checkFqdn;
512
+ exports.checkIsArray = checkIsArray;
513
+ exports.checkIsJson = checkIsJson;
514
+ exports.checkIsList = checkIsList;
515
+ exports.checkIsObject = checkIsObject;
516
+ exports.checkLowercase = checkLowercase;
517
+ exports.checkNum = checkNum;
518
+ exports.checkSpaces = checkSpaces;
519
+ exports.checkSpecialChar = checkSpecialChar;
520
+ exports.checkUppercase = checkUppercase;
521
+ exports.checkUrl = checkUrl;
522
+ exports.dom = dom;
523
+ exports.elmCleaner = elmCleaner;
524
+ exports.elmCleanerArray = elmCleanerArray;
525
+ exports.elmCleanerTr = elmCleanerTr;
526
+ exports.generateUID = generateUID;
527
+ exports.hexadecimal = hexadecimal;
528
+ exports.htmlTags = htmlTags;
529
+ exports.http = http;
530
+ exports.letters = letters;
531
+ exports.lettersLower = lettersLower;
532
+ exports.lettersUpper = lettersUpper;
533
+ exports.lists = lists;
534
+ exports.misc = misc;
535
+ exports.numbers = numbers;
536
+ exports.parseTemplate = parseTemplate;
537
+ exports.preloadImages = preloadImages;
538
+ exports.regex = regex;
539
+ exports.removeChar = removeChar;
540
+ exports.reqFileJson = reqFileJson;
541
+ exports.reqGet = reqGet;
542
+ exports.reqPostForm = reqPostForm;
543
+ exports.reqPostJson = reqPostJson;
544
+ exports.special = special;
545
+ exports.urlAddHttp = urlAddHttp;
546
+ exports.urlAddHttps = urlAddHttps;
@@ -0,0 +1,64 @@
1
+ import * as check from './lib/util.check.js';
2
+ import * as dom from './lib/util.dom.js';
3
+ import * as http from './lib/util.http.js';
4
+ import * as lists from './lib/util.lists.js';
5
+ import * as misc from './lib/util.misc.js';
6
+ import * as regex from './lib/util.regex.js';
7
+
8
+ export { check };
9
+ export const {
10
+ checkEmail,
11
+ checkFqdn,
12
+ checkIsArray,
13
+ checkIsJson,
14
+ checkIsList,
15
+ checkIsObject,
16
+ checkLowercase,
17
+ checkNum,
18
+ checkSpaces,
19
+ checkSpecialChar,
20
+ checkUppercase,
21
+ checkUrl
22
+ } = check;
23
+
24
+ export { dom };
25
+ export const {
26
+ elmCleaner,
27
+ elmCleanerArray,
28
+ elmCleanerTr,
29
+ parseTemplate
30
+ } = dom;
31
+
32
+ export { http };
33
+ export const {
34
+ reqFileJson,
35
+ reqGet,
36
+ reqPostForm,
37
+ reqPostJson,
38
+ urlAddHttp,
39
+ urlAddHttps
40
+ } = http;
41
+
42
+ export { lists };
43
+ export const {
44
+ htmlTags
45
+ } = lists;
46
+
47
+ export { misc };
48
+ export const {
49
+ addLeadZero,
50
+ generateUID,
51
+ preloadImages
52
+ } = misc;
53
+
54
+ export { regex };
55
+ export const {
56
+ binary,
57
+ hexadecimal,
58
+ letters,
59
+ lettersLower,
60
+ lettersUpper,
61
+ numbers,
62
+ removeChar,
63
+ special
64
+ } = regex;
@@ -0,0 +1,171 @@
1
+ // Checks for a valid FQDN (uses regex)
2
+ export function checkFqdn(str) {
3
+ if (typeof str !== "string" || str.length === 0 || str.length > 253) {
4
+ return false;
5
+ }
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
+ }
9
+
10
+ // Checks for a valid URL (uses regex)
11
+ export function checkUrl(str) {
12
+ try {
13
+ if (str === null || typeof str !== "string") {
14
+ throw new Error(`Invalid input.`);
15
+ };
16
+ var pattern = new RegExp('^(https?:\\/\\/)?' + // protocol
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
23
+ } catch (er) {
24
+ throw new Error(er);
25
+ }
26
+ }
27
+
28
+ // Checks for special characters (Returns true if it matches)
29
+ export function checkSpecialChar(str) {
30
+ try {
31
+ if (str === null || typeof str !== "string") {
32
+ throw new Error(`Invalid input.`);
33
+ };
34
+ const regSp = /[\!\@\#\$\%\^\&\*\(\)\_\+\=\[\]\{\}\?]/;
35
+ if (regSp.test(str) == true) {
36
+ return true;
37
+ }
38
+ return false;
39
+ } catch (er) {
40
+ throw new Error(er);
41
+ }
42
+ }
43
+
44
+ // Checks for a valid Email (uses regex)
45
+ export function checkEmail(str) {
46
+ try {
47
+ if (str === null || typeof str !== "string") {
48
+ throw new Error(`Invalid input.`);
49
+ };
50
+ var validRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
51
+ if (str.match(validRegex)) {
52
+ return true;
53
+ }
54
+ return false;
55
+ } catch (er) {
56
+ throw new Error(er);
57
+ }
58
+ }
59
+
60
+ // Checks for a space in a string
61
+ export function checkSpaces(str) {
62
+ try {
63
+ if (str === null || typeof str !== "string") {
64
+ throw new Error(`Invalid input.`);
65
+ };
66
+ return str.indexOf(' ') >= 0;
67
+ } catch (er) {
68
+ throw new Error(er);
69
+ }
70
+ }
71
+
72
+ // Check for a list of text???
73
+ export function checkIsList(input) {
74
+ try {
75
+ // Check if the variable is a string
76
+ if (typeof input !== 'string') {
77
+ return false;
78
+ }
79
+ // Split the string by comma
80
+ const items = input.split(',');
81
+ // Check if all items are non-empty strings
82
+ for (let item of items) {
83
+ item = item.trim(); // Trim any leading or trailing whitespace
84
+ if (item === '') {
85
+ return false; // Empty item found
86
+ }
87
+ }
88
+ return true;
89
+ } catch (er) {
90
+ throw new Error(er);
91
+ }
92
+ }
93
+
94
+ export function checkIsArray(input) {
95
+ try {
96
+ return input.constructor === Array;
97
+ } catch (er) {
98
+ throw new Error(er);
99
+ }
100
+ }
101
+
102
+ /**
103
+ * Checks if an input value is an object.
104
+ * @param {*} input - Any variable
105
+ * @returns {boolean}
106
+ */
107
+ export function checkIsObject(input) {
108
+ try {
109
+ return input.constructor === Object;
110
+ } catch (er) {
111
+ throw new Error(er);
112
+ }
113
+ }
114
+
115
+ export function checkIsJson(input) {
116
+ try {
117
+ JSON.parse(input);
118
+ return true;
119
+ } catch (er) {
120
+ return false;
121
+ }
122
+ }
123
+
124
+
125
+ // Checks for number values in a string (Returns true if it matches)
126
+ export function checkNum(str) {
127
+ try {
128
+ if (str === null || typeof str !== "string") {
129
+ throw new Error(`Invalid input.`);
130
+ };
131
+ const regNm = /[0-9]/;
132
+ if (regNm.test(str) === true) {
133
+ return true;
134
+ }
135
+ return false;
136
+ } catch (er) {
137
+ throw new Error(er);
138
+ }
139
+ }
140
+
141
+ // Checks for lowercase characters (Returns true if it matches)
142
+ export function checkLowercase(str) {
143
+ try {
144
+ if (str === null || typeof str !== "string") {
145
+ throw new Error(`Invalid input.`);
146
+ };
147
+ const regLo = /[a-z]/;
148
+ if (regLo.test(str) == true) {
149
+ return true;
150
+ }
151
+ return false;
152
+ } catch (er) {
153
+ throw new Error(er);
154
+ }
155
+ }
156
+
157
+ // Checks for uppercase characters (Returns true if it matches)
158
+ export function checkUppercase(str) {
159
+ try {
160
+ if (str === null || typeof str !== "string") {
161
+ throw new Error(`Invalid input.`);
162
+ };
163
+ const regUp = /[A-Z]/;
164
+ if (regUp.test(str) == true) {
165
+ return true;
166
+ }
167
+ return false;
168
+ } catch (er) {
169
+ throw new Error(er);
170
+ }
171
+ }
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Takes an HTML template literal, parses it with the DOM parser, then extracts the element with querySelectorAll.
3
+ */
4
+ export function elmCleaner(templateLit) {
5
+ try {
6
+ let parser = new DOMParser();
7
+ let elmBody = parser.parseFromString(templateLit, 'text/html');
8
+ let elms = elmBody.body.querySelectorAll("*");
9
+ return elms[0];
10
+ } catch (er) {
11
+ console.error(er);
12
+ throw new Error(er);
13
+ }
14
+ }
15
+
16
+ /**
17
+ * Takes an HTML table row template literal, parses it with the DOM parser, then extracts the element with querySelectorAll.
18
+ */
19
+ export function elmCleanerTr(templateLit) {
20
+ try {
21
+ let elmTemp = document.createElement('table');
22
+ elmTemp.innerHTML = templateLit;
23
+ let elms = elmTemp.querySelector("tr");
24
+ elmTemp.remove();
25
+ return elms;
26
+ } catch (er) {
27
+ console.error(er);
28
+ throw new Error(er);
29
+ }
30
+ }
31
+
32
+ /**
33
+ * Takes an HTML template literal, parses it with the DOM parser, then extracts the NodeList. The list is then returned as an array.
34
+ */
35
+ export function elmCleanerArray(templateLit) {
36
+ try {
37
+ let parser = new DOMParser();
38
+ let elmBody = parser.parseFromString(templateLit, 'text/html');
39
+ let elms = elmBody.body.querySelectorAll("*");
40
+ return Array.from(elms);
41
+ } catch (er) {
42
+ console.error(er);
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);
62
+ }
63
+ }
@@ -0,0 +1,101 @@
1
+ import { checkUrl } from './util.check.js';
2
+
3
+ // Adds 'http://' if valid URL
4
+ export function urlAddHttp(url) {
5
+ try {
6
+ if (url === null || !checkUrl(url)) {
7
+ throw `Invalid input.`;
8
+ };
9
+ if (url.startsWith("http://") == false && url.startsWith("https://") == false) {
10
+ url = "http://" + url
11
+ }
12
+ return url;
13
+ } catch (er) {
14
+ throw new Error(er);
15
+ }
16
+ }
17
+
18
+ // Adds 'https://' if valid URL
19
+ export function urlAddHttps(url) {
20
+ try {
21
+ if (url === null || !checkUrl(url)) {
22
+ throw `Invalid input.`;
23
+ };
24
+ if (url.startsWith("http://") == false && url.startsWith("https://") == false) {
25
+ url = "https://" + url
26
+ }
27
+ return url;
28
+ } catch (er) {
29
+ throw new Error(er);
30
+ }
31
+ }
32
+
33
+ // Simple read JSON file utility
34
+ export async function reqFileJson(file) {
35
+ try {
36
+ const res = await fetch(file);
37
+ if (!res.ok) {
38
+ return res;
39
+ }
40
+ const data = await res.json();
41
+ return data;
42
+ } catch (er) {
43
+ throw new Error(er);
44
+ }
45
+ }
46
+
47
+ // Simple GET request - This is expecting JSON data from the target URL
48
+ export async function reqGet(url) {
49
+ try {
50
+ const res = await fetch(url, { method: 'GET' });
51
+ if (!res.ok) {
52
+ throw res;
53
+ }
54
+ return res;
55
+ } catch (er) {
56
+ throw er;
57
+ }
58
+ }
59
+
60
+ // Simple POST request. export function is expecting JSON data. The JSON.parse will throw an error if data is not valid JSON.
61
+ // NEEDS REVIEW
62
+ export async function reqPostJson(url, dataJson) {
63
+ try {
64
+ if (dataJson === null || dataJson === undefined) {
65
+ throw `No JSON data provided`;
66
+ }
67
+ JSON.parse(dataJson);
68
+ const res = await fetch(url, {
69
+ method: 'POST',
70
+ headers: {
71
+ 'Content-Type': 'application/json',
72
+ },
73
+ body: dataJson
74
+ });
75
+ if (!res.ok) {
76
+ return res;
77
+ }
78
+ return res;
79
+ } catch (er) {
80
+ throw new Error(er);
81
+ }
82
+ }
83
+ // Simple POST request. export function is expecting FormData.
84
+ // NEEDS REVIEW
85
+ export async function reqPostForm(url, dataForm) {
86
+ try {
87
+ if (!(dataForm instanceof FormData)) {
88
+ throw `The data provided was not form data`;
89
+ }
90
+ const res = await fetch(url, {
91
+ method: 'POST',
92
+ body: dataForm
93
+ });
94
+ if (!res.ok) {
95
+ return res;
96
+ }
97
+ return res;
98
+ } catch (er) {
99
+ throw new Error(er);
100
+ }
101
+ }
@@ -0,0 +1,83 @@
1
+ export const htmlTags = [
2
+ "html",
3
+ "body",
4
+ "div",
5
+ "span",
6
+ "applet",
7
+ "object",
8
+ "iframe",
9
+ "h1",
10
+ "h2",
11
+ "h3",
12
+ "h4",
13
+ "h5",
14
+ "h6",
15
+ "p",
16
+ "blockquote",
17
+ "pre",
18
+ "a",
19
+ "abbr",
20
+ "acronym",
21
+ "address",
22
+ "big",
23
+ "cite",
24
+ "code",
25
+ "del",
26
+ "dfn",
27
+ "em",
28
+ "img",
29
+ "ins",
30
+ "kbd",
31
+ "q",
32
+ "s",
33
+ "samp",
34
+ "small",
35
+ "strike",
36
+ "strong",
37
+ "sub",
38
+ "sup",
39
+ "tt",
40
+ "var",
41
+ "b",
42
+ "u",
43
+ "i",
44
+ "center",
45
+ "dl",
46
+ "dt",
47
+ "dd",
48
+ "ol",
49
+ "ul",
50
+ "li",
51
+ "fieldset",
52
+ "form",
53
+ "label",
54
+ "legend",
55
+ "table",
56
+ "caption",
57
+ "tbody",
58
+ "tfoot",
59
+ "thead",
60
+ "tr",
61
+ "th",
62
+ "td",
63
+ "article",
64
+ "aside",
65
+ "canvas",
66
+ "details",
67
+ "embed",
68
+ "figure",
69
+ "figcaption",
70
+ "footer",
71
+ "header",
72
+ "hgroup",
73
+ "menu",
74
+ "nav",
75
+ "output",
76
+ "ruby",
77
+ "section",
78
+ "summary",
79
+ "time",
80
+ "mark",
81
+ "audio",
82
+ "video"
83
+ ];
@@ -0,0 +1,29 @@
1
+ // Adds zero in front of numbers less than 10
2
+ export function addLeadZero(num) {
3
+ try {
4
+ if (num === null || typeof num !== 'number' || num > 9) {
5
+ throw `Invalid input.`;
6
+ };
7
+ num = "0" + num;
8
+ } catch (er) {
9
+ console.error(er);
10
+ throw new Error(er);
11
+ }
12
+ }
13
+
14
+ /**
15
+ * Generates a unique ID
16
+ */
17
+ export function generateUID() {
18
+ return 'uid-' + Date.now().toString(36) + '-' + Math.random().toString(36).substr(2);
19
+ }
20
+
21
+ /**
22
+ * Preloads images. Requires an array of image URL strings.
23
+ */
24
+ export function preloadImages(imgUrls) {
25
+ imgUrls.forEach(url => {
26
+ const img = new Image();
27
+ img.src = url;
28
+ });
29
+ }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * List of RegEx variables for different patterns.
3
+ */
4
+ export const letters = /^[a-zA-Z]+$/;
5
+ export const lettersLower = /^[a-z]+$/;
6
+ export const lettersUpper = /^[A-Z]+$/;
7
+ export const numbers = /^\d+$/;
8
+ export const binary = /[^01]/g;
9
+ export const hexadecimal = /[^0-9A-Fa-f]/g;
10
+ export const special = /^[\!\@\#\$\%\^\&\*\(\)\_\+\-\=\[\]\{\}\|\;\'\:\"\,\.\<\>\?\/]+$/;
11
+
12
+ /**
13
+ * Removes characters from a string based on a provided regex pattern.
14
+ * @param {string} string The string to process.
15
+ * @param {RegExp} regex The regex pattern of characters to remove from the string.
16
+ * @return {string} The processed string with specified characters removed.
17
+ * @throws {Error} Throws an error if the first parameter is not a string or if the second parameter is not a RegExp.
18
+ */
19
+ export function removeChar(string, regex) {
20
+ if (typeof string !== 'string') {
21
+ throw new Error(`First parameter must be a string.`);
22
+ }
23
+ if (!(regex instanceof RegExp)) {
24
+ throw new Error(`Second parameter must be a RegExp.`);
25
+ }
26
+ return string.replace(regex, '');
27
+ }