tuijs-util 1.1.4 → 1.2.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/README.md CHANGED
@@ -7,15 +7,17 @@ A simple JavaScript utility library
7
7
  npm i tuijs-util
8
8
 
9
9
  ## Import
10
- All utilities are exported both as a module and individually. Simply import the module or the specific utility you want.
10
+ All utilities are provide both in ESM and CJS formats. Simply specific utility you want.
11
11
 
12
- ## Examples
12
+ ## ESM
13
13
  ```javascript
14
- import { utilFunctionName } from 'ttbjs';
14
+ import { utilFunctionName, utilFunctionName2 } from 'ttbjs';
15
15
  utilFunctionName();
16
+ utilFunctionName2();
16
17
  ```
17
-
18
+ ## CJS
18
19
  ```javascript
19
- import { utilModule } from 'ttbjs';
20
- utilModule.utilFunctionName();
20
+ const { utilFunctionName, utilFunctionName2 } = require('ttbjs');
21
+ utilFunctionName();
22
+ utilFunctionName2();
21
23
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tuijs-util",
3
- "version": "1.1.4",
3
+ "version": "1.2.0",
4
4
  "module": "src/esm/index.js",
5
5
  "main": "src/cjs/index.cjs",
6
6
  "exports": {
@@ -10,7 +10,7 @@
10
10
  }
11
11
  },
12
12
  "description": "A simple JavaScript utility library",
13
- "author": "TechTB",
13
+ "author": "TechTB LLC",
14
14
  "license": "MIT",
15
15
  "repository": "github:TechTB-OpenSource/tuijs-util",
16
16
  "type": "module",
package/src/cjs/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  // Checks for a valid FQDN (uses regex)
4
- function checkFqdn$1(str) {
4
+ function checkFqdn(str) {
5
5
  if (typeof str !== "string" || str.length === 0 || str.length > 253) {
6
6
  return false;
7
7
  }
@@ -10,7 +10,7 @@ function checkFqdn$1(str) {
10
10
  }
11
11
 
12
12
  // Checks for a valid URL (uses regex)
13
- function checkUrl$1(str) {
13
+ function checkUrl(str) {
14
14
  try {
15
15
  if (str === null || typeof str !== "string") {
16
16
  throw new Error(`Invalid input.`);
@@ -34,7 +34,7 @@ function checkUrl$1(str) {
34
34
  }
35
35
 
36
36
  // Checks for special characters (Returns true if it matches)
37
- function checkSpecialChar$1(str) {
37
+ function checkSpecialChar(str) {
38
38
  try {
39
39
  if (str === null || typeof str !== "string") {
40
40
  throw new Error(`Invalid input.`);
@@ -51,7 +51,7 @@ function checkSpecialChar$1(str) {
51
51
  }
52
52
 
53
53
  // Checks for a valid Email (uses regex)
54
- function checkEmail$1(str) {
54
+ function checkEmail(str) {
55
55
  try {
56
56
  if (str === null || typeof str !== "string") {
57
57
  throw new Error(`Invalid input.`);
@@ -68,7 +68,7 @@ function checkEmail$1(str) {
68
68
  }
69
69
 
70
70
  // Checks for a space in a string
71
- function checkSpaces$1(str) {
71
+ function checkSpaces(str) {
72
72
  try {
73
73
  if (str === null || typeof str !== "string") {
74
74
  throw new Error(`Invalid input.`);
@@ -81,7 +81,7 @@ function checkSpaces$1(str) {
81
81
  }
82
82
 
83
83
  // Check for a list of text???
84
- function checkIsList$1(input) {
84
+ function checkIsList(input) {
85
85
  try {
86
86
  // Check if the variable is a string
87
87
  if (typeof input !== 'string') {
@@ -101,7 +101,7 @@ function checkIsList$1(input) {
101
101
  throw new Error(er);
102
102
  }
103
103
  }
104
- function checkIsArray$1(input) {
104
+ function checkIsArray(input) {
105
105
  try {
106
106
  return input.constructor === Array;
107
107
  } catch (er) {
@@ -114,14 +114,14 @@ function checkIsArray$1(input) {
114
114
  * @param {*} input - Any variable
115
115
  * @returns {boolean}
116
116
  */
117
- function checkIsObject$1(input) {
117
+ function checkIsObject(input) {
118
118
  try {
119
119
  return input.constructor === Object;
120
120
  } catch (er) {
121
121
  throw new Error(er);
122
122
  }
123
123
  }
124
- function checkIsJson$1(input) {
124
+ function checkIsJson(input) {
125
125
  try {
126
126
  JSON.parse(input);
127
127
  return true;
@@ -131,7 +131,7 @@ function checkIsJson$1(input) {
131
131
  }
132
132
 
133
133
  // Checks for number values in a string (Returns true if it matches)
134
- function checkNum$1(str) {
134
+ function checkNum(str) {
135
135
  try {
136
136
  if (str === null || typeof str !== "string") {
137
137
  throw new Error(`Invalid input.`);
@@ -148,7 +148,7 @@ function checkNum$1(str) {
148
148
  }
149
149
 
150
150
  // Checks for lowercase characters (Returns true if it matches)
151
- function checkLowercase$1(str) {
151
+ function checkLowercase(str) {
152
152
  try {
153
153
  if (str === null || typeof str !== "string") {
154
154
  throw new Error(`Invalid input.`);
@@ -165,7 +165,7 @@ function checkLowercase$1(str) {
165
165
  }
166
166
 
167
167
  // Checks for uppercase characters (Returns true if it matches)
168
- function checkUppercase$1(str) {
168
+ function checkUppercase(str) {
169
169
  try {
170
170
  if (str === null || typeof str !== "string") {
171
171
  throw new Error(`Invalid input.`);
@@ -181,26 +181,10 @@ function checkUppercase$1(str) {
181
181
  }
182
182
  }
183
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
184
  /**
201
185
  * Takes an HTML template literal, parses it with the DOM parser, then extracts the element with querySelectorAll.
202
186
  */
203
- function elmCleaner$1(templateLit) {
187
+ function elmCleaner(templateLit) {
204
188
  try {
205
189
  let parser = new DOMParser();
206
190
  let elmBody = parser.parseFromString(templateLit, 'text/html');
@@ -215,7 +199,7 @@ function elmCleaner$1(templateLit) {
215
199
  /**
216
200
  * Takes an HTML table row template literal, parses it with the DOM parser, then extracts the element with querySelectorAll.
217
201
  */
218
- function elmCleanerTr$1(templateLit) {
202
+ function elmCleanerTr(templateLit) {
219
203
  try {
220
204
  let elmTemp = document.createElement('table');
221
205
  elmTemp.innerHTML = templateLit;
@@ -231,7 +215,7 @@ function elmCleanerTr$1(templateLit) {
231
215
  /**
232
216
  * Takes an HTML template literal, parses it with the DOM parser, then extracts the NodeList. The list is then returned as an array.
233
217
  */
234
- function elmCleanerArray$1(templateLit) {
218
+ function elmCleanerArray(templateLit) {
235
219
  try {
236
220
  let parser = new DOMParser();
237
221
  let elmBody = parser.parseFromString(templateLit, 'text/html');
@@ -246,7 +230,7 @@ function elmCleanerArray$1(templateLit) {
246
230
  /**
247
231
  * Parses template literal with 'template' tag
248
232
  */
249
- function parseTemplate$1(templateLit) {
233
+ function parseTemplate(templateLit) {
250
234
  try {
251
235
  let parser = new DOMParser();
252
236
  let doc = parser.parseFromString(templateLit, 'text/html');
@@ -261,18 +245,10 @@ function parseTemplate$1(templateLit) {
261
245
  }
262
246
  }
263
247
 
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
248
  // Adds 'http://' if valid URL
273
- function urlAddHttp$1(url) {
249
+ function urlAddHttp(url) {
274
250
  try {
275
- if (url === null || !checkUrl$1(url)) {
251
+ if (url === null || !checkUrl(url)) {
276
252
  throw `Invalid input.`;
277
253
  }
278
254
  ;
@@ -286,9 +262,9 @@ function urlAddHttp$1(url) {
286
262
  }
287
263
 
288
264
  // Adds 'https://' if valid URL
289
- function urlAddHttps$1(url) {
265
+ function urlAddHttps(url) {
290
266
  try {
291
- if (url === null || !checkUrl$1(url)) {
267
+ if (url === null || !checkUrl(url)) {
292
268
  throw `Invalid input.`;
293
269
  }
294
270
  ;
@@ -302,7 +278,7 @@ function urlAddHttps$1(url) {
302
278
  }
303
279
 
304
280
  // Simple read JSON file utility
305
- async function reqFileJson$1(file) {
281
+ async function reqFileJson(file) {
306
282
  try {
307
283
  const res = await fetch(file);
308
284
  if (!res.ok) {
@@ -316,7 +292,7 @@ async function reqFileJson$1(file) {
316
292
  }
317
293
 
318
294
  // Simple GET request - This is expecting JSON data from the target URL
319
- async function reqGet$1(url) {
295
+ async function reqGet(url) {
320
296
  try {
321
297
  const res = await fetch(url, {
322
298
  method: 'GET'
@@ -332,7 +308,7 @@ async function reqGet$1(url) {
332
308
 
333
309
  // Simple POST request. export function is expecting JSON data. The JSON.parse will throw an error if data is not valid JSON.
334
310
  // NEEDS REVIEW
335
- async function reqPostJson$1(url, dataJson) {
311
+ async function reqPostJson(url, dataJson) {
336
312
  try {
337
313
  if (dataJson === null || dataJson === undefined) {
338
314
  throw `No JSON data provided`;
@@ -355,7 +331,7 @@ async function reqPostJson$1(url, dataJson) {
355
331
  }
356
332
  // Simple POST request. export function is expecting FormData.
357
333
  // NEEDS REVIEW
358
- async function reqPostForm$1(url, dataForm) {
334
+ async function reqPostForm(url, dataForm) {
359
335
  try {
360
336
  if (!(dataForm instanceof FormData)) {
361
337
  throw `The data provided was not form data`;
@@ -373,25 +349,10 @@ async function reqPostForm$1(url, dataForm) {
373
349
  }
374
350
  }
375
351
 
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
- });
352
+ 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"];
392
353
 
393
354
  // Adds zero in front of numbers less than 10
394
- function addLeadZero$1(num) {
355
+ function addLeadZero(num) {
395
356
  try {
396
357
  if (num === null || typeof num !== 'number' || num > 9) {
397
358
  throw `Invalid input.`;
@@ -407,26 +368,39 @@ function addLeadZero$1(num) {
407
368
  /**
408
369
  * Generates a unique ID
409
370
  */
410
- function generateUID$1() {
371
+ function generateUID() {
411
372
  return 'uid-' + Date.now().toString(36) + '-' + Math.random().toString(36).substr(2);
412
373
  }
413
374
 
414
- var misc = /*#__PURE__*/Object.freeze({
415
- __proto__: null,
416
- addLeadZero: addLeadZero$1,
417
- generateUID: generateUID$1
418
- });
375
+ /**
376
+ * Preloads images. Requires an array of image URL strings.
377
+ */
378
+ function preloadImages(imgUrls) {
379
+ imgUrls.forEach(url => {
380
+ const img = new Image();
381
+ img.src = url;
382
+ });
383
+ }
384
+
385
+ /**
386
+ * Sleeps for a given number of milliseconds
387
+ * @param {number} ms - Provide number of milliseconds
388
+ * @returns {Promise} - Returns Promise once timeout finishes
389
+ */
390
+ function sleep(ms) {
391
+ return new Promise(resolve => setTimeout(resolve, ms));
392
+ }
419
393
 
420
394
  /**
421
395
  * List of RegEx variables for different patterns.
422
396
  */
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 = /^[\!\@\#\$\%\^\&\*\(\)\_\+\-\=\[\]\{\}\|\;\'\:\"\,\.\<\>\?\/]+$/;
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 = /^[\!\@\#\$\%\^\&\*\(\)\_\+\-\=\[\]\{\}\|\;\'\:\"\,\.\<\>\?\/]+$/;
430
404
 
431
405
  /**
432
406
  * Removes characters from a string based on a provided regex pattern.
@@ -435,7 +409,7 @@ const special$1 = /^[\!\@\#\$\%\^\&\*\(\)\_\+\-\=\[\]\{\}\|\;\'\:\"\,\.\<\>\?\/]
435
409
  * @return {string} The processed string with specified characters removed.
436
410
  * @throws {Error} Throws an error if the first parameter is not a string or if the second parameter is not a RegExp.
437
411
  */
438
- function removeChar$1(string, regex) {
412
+ function removeChar(string, regex) {
439
413
  if (typeof string !== 'string') {
440
414
  throw new Error(`First parameter must be a string.`);
441
415
  }
@@ -445,68 +419,8 @@ function removeChar$1(string, regex) {
445
419
  return string.replace(regex, '');
446
420
  }
447
421
 
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
422
  exports.addLeadZero = addLeadZero;
508
423
  exports.binary = binary;
509
- exports.check = check;
510
424
  exports.checkEmail = checkEmail;
511
425
  exports.checkFqdn = checkFqdn;
512
426
  exports.checkIsArray = checkIsArray;
@@ -519,28 +433,24 @@ exports.checkSpaces = checkSpaces;
519
433
  exports.checkSpecialChar = checkSpecialChar;
520
434
  exports.checkUppercase = checkUppercase;
521
435
  exports.checkUrl = checkUrl;
522
- exports.dom = dom;
523
436
  exports.elmCleaner = elmCleaner;
524
437
  exports.elmCleanerArray = elmCleanerArray;
525
438
  exports.elmCleanerTr = elmCleanerTr;
526
439
  exports.generateUID = generateUID;
527
440
  exports.hexadecimal = hexadecimal;
528
441
  exports.htmlTags = htmlTags;
529
- exports.http = http;
530
442
  exports.letters = letters;
531
443
  exports.lettersLower = lettersLower;
532
444
  exports.lettersUpper = lettersUpper;
533
- exports.lists = lists;
534
- exports.misc = misc;
535
445
  exports.numbers = numbers;
536
446
  exports.parseTemplate = parseTemplate;
537
447
  exports.preloadImages = preloadImages;
538
- exports.regex = regex;
539
448
  exports.removeChar = removeChar;
540
449
  exports.reqFileJson = reqFileJson;
541
450
  exports.reqGet = reqGet;
542
451
  exports.reqPostForm = reqPostForm;
543
452
  exports.reqPostJson = reqPostJson;
453
+ exports.sleep = sleep;
544
454
  exports.special = special;
545
455
  exports.urlAddHttp = urlAddHttp;
546
456
  exports.urlAddHttps = urlAddHttps;
package/src/esm/index.js CHANGED
@@ -1,12 +1,4 @@
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 {
1
+ export {
10
2
  checkEmail,
11
3
  checkFqdn,
12
4
  checkIsArray,
@@ -19,40 +11,31 @@ export const {
19
11
  checkSpecialChar,
20
12
  checkUppercase,
21
13
  checkUrl
22
- } = check;
23
-
24
- export { dom };
25
- export const {
14
+ } from './lib/util.check.js';
15
+ export {
26
16
  elmCleaner,
27
17
  elmCleanerArray,
28
18
  elmCleanerTr,
29
19
  parseTemplate
30
- } = dom;
31
-
32
- export { http };
33
- export const {
20
+ } from './lib/util.dom.js';
21
+ export {
34
22
  reqFileJson,
35
23
  reqGet,
36
24
  reqPostForm,
37
25
  reqPostJson,
38
26
  urlAddHttp,
39
27
  urlAddHttps
40
- } = http;
41
-
42
- export { lists };
43
- export const {
28
+ } from './lib/util.http.js';
29
+ export {
44
30
  htmlTags
45
- } = lists;
46
-
47
- export { misc };
48
- export const {
31
+ } from './lib/util.lists.js';
32
+ export {
49
33
  addLeadZero,
50
34
  generateUID,
51
- preloadImages
52
- } = misc;
53
-
54
- export { regex };
55
- export const {
35
+ preloadImages,
36
+ sleep
37
+ } from './lib/util.misc.js';
38
+ export {
56
39
  binary,
57
40
  hexadecimal,
58
41
  letters,
@@ -61,4 +44,5 @@ export const {
61
44
  numbers,
62
45
  removeChar,
63
46
  special
64
- } = regex;
47
+ } from './lib/util.regex.js';
48
+
@@ -27,3 +27,12 @@ export function preloadImages(imgUrls) {
27
27
  img.src = url;
28
28
  });
29
29
  }
30
+
31
+ /**
32
+ * Sleeps for a given number of milliseconds
33
+ * @param {number} ms - Provide number of milliseconds
34
+ * @returns {Promise} - Returns Promise once timeout finishes
35
+ */
36
+ export function sleep(ms) {
37
+ return new Promise(resolve => setTimeout(resolve, ms));
38
+ }