tuijs-util 1.3.0 → 1.3.2
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 +3 -3
- package/src/cjs/index.cjs +51 -12
- package/src/esm/index.js +3 -2
- package/src/esm/lib/util.check.js +1 -1
- package/src/esm/lib/util.dom.js +6 -0
- package/src/esm/lib/util.misc.js +41 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tuijs-util",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.2",
|
|
4
4
|
"module": "src/esm/index.js",
|
|
5
5
|
"main": "src/cjs/index.cjs",
|
|
6
6
|
"exports": {
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@rollup/plugin-babel": "^6.0.4",
|
|
22
|
-
"@rollup/plugin-node-resolve": "^15.
|
|
23
|
-
"rollup": "^4.
|
|
22
|
+
"@rollup/plugin-node-resolve": "^15.3.0",
|
|
23
|
+
"rollup": "^4.22.4"
|
|
24
24
|
}
|
|
25
25
|
}
|
package/src/cjs/index.cjs
CHANGED
|
@@ -336,6 +336,12 @@ function elmCleanerArray(templateLit) {
|
|
|
336
336
|
}
|
|
337
337
|
}
|
|
338
338
|
|
|
339
|
+
/**
|
|
340
|
+
* @typedef {Array} ImageUrls - A list of image urls
|
|
341
|
+
* @property {string} imageUrl - The url of the desired image
|
|
342
|
+
* @property {Array<string>} imageUrlStrings - An Array of image url strings
|
|
343
|
+
*/
|
|
344
|
+
|
|
339
345
|
/**
|
|
340
346
|
* Adds 'http://' if valid URL and 'http://' or 'https://' is missing.
|
|
341
347
|
* @param {string} url
|
|
@@ -514,22 +520,27 @@ function generateUID(length = 16) {
|
|
|
514
520
|
}
|
|
515
521
|
|
|
516
522
|
/**
|
|
517
|
-
* @typedef {string[]}
|
|
523
|
+
* @typedef {string[]} ImageUrls - An array of URL strings
|
|
518
524
|
*/
|
|
519
525
|
/**
|
|
520
|
-
*
|
|
521
|
-
* @param {
|
|
522
|
-
* @returns {
|
|
523
|
-
* @throws {Error} - Throws an Error if loading an image fails.
|
|
526
|
+
* Loads all images and returns a promise when loading is complete
|
|
527
|
+
* @param {ImageUrls} imageUrls - An Array of image url strings
|
|
528
|
+
* @returns {Object}
|
|
524
529
|
*/
|
|
525
|
-
function preloadImages(
|
|
526
|
-
|
|
527
|
-
const
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
530
|
+
async function preloadImages(imageUrls) {
|
|
531
|
+
try {
|
|
532
|
+
const loadImage = url => {
|
|
533
|
+
return new Promise((resolve, reject) => {
|
|
534
|
+
const img = new Image();
|
|
535
|
+
img.onload = () => resolve(url);
|
|
536
|
+
img.onerror = () => reject(new Error(`Failed to load image at ${url}`));
|
|
537
|
+
img.src = url;
|
|
538
|
+
});
|
|
531
539
|
};
|
|
532
|
-
|
|
540
|
+
await Promise.all(imageUrls.map(loadImage));
|
|
541
|
+
} catch (er) {
|
|
542
|
+
throw new Error(er.message);
|
|
543
|
+
}
|
|
533
544
|
}
|
|
534
545
|
|
|
535
546
|
/**
|
|
@@ -541,6 +552,33 @@ function sleep(ms) {
|
|
|
541
552
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
542
553
|
}
|
|
543
554
|
|
|
555
|
+
/**
|
|
556
|
+
* Adds and intersectionObserver for a list of target elements, that calls a function when
|
|
557
|
+
* @param {Array} targets - An array of elements that will be attached to the observer.
|
|
558
|
+
* @param {Function} callback - The callback function that will run when the observer is triggered.
|
|
559
|
+
* @param {Object} observerOptions - An Object containing the observer options. By default only threshold is set.
|
|
560
|
+
* @returns {Function} - Returns a cleanup function that will disconnect the observer. This is not required to use.
|
|
561
|
+
*/
|
|
562
|
+
function scrollIntoView(targets, callback, observerOptions = {
|
|
563
|
+
threshold: 0.5
|
|
564
|
+
}) {
|
|
565
|
+
const observer = new IntersectionObserver(entries => {
|
|
566
|
+
entries.forEach(entry => {
|
|
567
|
+
if (entry.isIntersecting) {
|
|
568
|
+
// Only trigger when the element is intersecting
|
|
569
|
+
callback(entry.target);
|
|
570
|
+
}
|
|
571
|
+
});
|
|
572
|
+
}, observerOptions);
|
|
573
|
+
targets.forEach(target => {
|
|
574
|
+
observer.observe(target);
|
|
575
|
+
});
|
|
576
|
+
// Return a cleanup function to disconnect the observer
|
|
577
|
+
return () => {
|
|
578
|
+
observer.disconnect();
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
|
|
544
582
|
exports.addLeadZero = addLeadZero;
|
|
545
583
|
exports.checkEmail = checkEmail;
|
|
546
584
|
exports.checkFqdn = checkFqdn;
|
|
@@ -576,6 +614,7 @@ exports.reqFileJson = reqFileJson;
|
|
|
576
614
|
exports.reqGet = reqGet;
|
|
577
615
|
exports.reqPostForm = reqPostForm;
|
|
578
616
|
exports.reqPostJson = reqPostJson;
|
|
617
|
+
exports.scrollIntoView = scrollIntoView;
|
|
579
618
|
exports.sleep = sleep;
|
|
580
619
|
exports.urlAddHttp = urlAddHttp;
|
|
581
620
|
exports.urlAddHttps = urlAddHttps;
|
package/src/esm/index.js
CHANGED
|
@@ -16,7 +16,7 @@ export {
|
|
|
16
16
|
elmCleaner,
|
|
17
17
|
elmCleanerArray,
|
|
18
18
|
elmCleanerTr,
|
|
19
|
-
parseTemplate
|
|
19
|
+
parseTemplate,
|
|
20
20
|
} from './lib/util.dom.js';
|
|
21
21
|
export {
|
|
22
22
|
reqFileJson,
|
|
@@ -33,7 +33,8 @@ export {
|
|
|
33
33
|
addLeadZero,
|
|
34
34
|
generateUID,
|
|
35
35
|
preloadImages,
|
|
36
|
-
sleep
|
|
36
|
+
sleep,
|
|
37
|
+
scrollIntoView
|
|
37
38
|
} from './lib/util.misc.js';
|
|
38
39
|
export {
|
|
39
40
|
regExLetters,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { regExNumbers, regExLettersLower, regExLettersUpper, regExSpecial, regExFqdn, regExUrl, regExEmail } from "./util.regex";
|
|
1
|
+
import { regExNumbers, regExLettersLower, regExLettersUpper, regExSpecial, regExFqdn, regExUrl, regExEmail } from "./util.regex.js";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Checks for a valid FQDN (Uses RegEx).
|
package/src/esm/lib/util.dom.js
CHANGED
|
@@ -78,3 +78,9 @@ export function elmCleanerArray(templateLit) {
|
|
|
78
78
|
throw new Error(er.message);
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @typedef {Array} ImageUrls - A list of image urls
|
|
84
|
+
* @property {string} imageUrl - The url of the desired image
|
|
85
|
+
* @property {Array<string>} imageUrlStrings - An Array of image url strings
|
|
86
|
+
*/
|
package/src/esm/lib/util.misc.js
CHANGED
|
@@ -35,22 +35,27 @@ export function generateUID(length = 16) {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
* @typedef {string[]}
|
|
38
|
+
* @typedef {string[]} ImageUrls - An array of URL strings
|
|
39
39
|
*/
|
|
40
40
|
/**
|
|
41
|
-
*
|
|
42
|
-
* @param {
|
|
43
|
-
* @returns {
|
|
44
|
-
* @throws {Error} - Throws an Error if loading an image fails.
|
|
41
|
+
* Loads all images and returns a promise when loading is complete
|
|
42
|
+
* @param {ImageUrls} imageUrls - An Array of image url strings
|
|
43
|
+
* @returns {Object}
|
|
45
44
|
*/
|
|
46
|
-
export function preloadImages(
|
|
47
|
-
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
45
|
+
export async function preloadImages(imageUrls) {
|
|
46
|
+
try {
|
|
47
|
+
const loadImage = (url) => {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const img = new Image();
|
|
50
|
+
img.onload = () => resolve(url);
|
|
51
|
+
img.onerror = () => reject(new Error(`Failed to load image at ${url}`));
|
|
52
|
+
img.src = url;
|
|
53
|
+
});
|
|
52
54
|
};
|
|
53
|
-
|
|
55
|
+
await Promise.all(imageUrls.map(loadImage));
|
|
56
|
+
} catch (er) {
|
|
57
|
+
throw new Error(er.message);
|
|
58
|
+
}
|
|
54
59
|
}
|
|
55
60
|
|
|
56
61
|
/**
|
|
@@ -61,3 +66,27 @@ export function preloadImages(imgUrls) {
|
|
|
61
66
|
export function sleep(ms) {
|
|
62
67
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
63
68
|
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Adds and intersectionObserver for a list of target elements, that calls a function when
|
|
72
|
+
* @param {Array} targets - An array of elements that will be attached to the observer.
|
|
73
|
+
* @param {Function} callback - The callback function that will run when the observer is triggered.
|
|
74
|
+
* @param {Object} observerOptions - An Object containing the observer options. By default only threshold is set.
|
|
75
|
+
* @returns {Function} - Returns a cleanup function that will disconnect the observer. This is not required to use.
|
|
76
|
+
*/
|
|
77
|
+
export function scrollIntoView(targets, callback, observerOptions = { threshold: 0.5}) {
|
|
78
|
+
const observer = new IntersectionObserver((entries) => {
|
|
79
|
+
entries.forEach(entry => {
|
|
80
|
+
if (entry.isIntersecting) { // Only trigger when the element is intersecting
|
|
81
|
+
callback(entry.target);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}, observerOptions);
|
|
85
|
+
targets.forEach(target => {
|
|
86
|
+
observer.observe(target);
|
|
87
|
+
});
|
|
88
|
+
// Return a cleanup function to disconnect the observer
|
|
89
|
+
return () => {
|
|
90
|
+
observer.disconnect();
|
|
91
|
+
};
|
|
92
|
+
}
|