tuijs-util 1.3.7 → 1.4.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tuijs-util",
3
- "version": "1.3.7",
3
+ "version": "1.4.1",
4
4
  "module": "src/esm/index.js",
5
5
  "main": "src/cjs/index.cjs",
6
6
  "exports": {
@@ -20,6 +20,6 @@
20
20
  "devDependencies": {
21
21
  "@rollup/plugin-babel": "^6.0.4",
22
22
  "@rollup/plugin-node-resolve": "^15.3.0",
23
- "rollup": "^4.22.4"
23
+ "rollup": "^4.24.0"
24
24
  }
25
25
  }
package/src/cjs/index.cjs CHANGED
@@ -339,12 +339,6 @@ function elmCleanerArray(templateLit) {
339
339
  }
340
340
  }
341
341
 
342
- /**
343
- * @typedef {Array} ImageUrls - A list of image urls
344
- * @property {string} imageUrl - The url of the desired image
345
- * @property {Array<string>} imageUrlStrings - An Array of image url strings
346
- */
347
-
348
342
  /**
349
343
  * Adds 'http://' if valid URL and 'http://' or 'https://' is missing.
350
344
  * @param {string} url
@@ -388,18 +382,35 @@ function urlAddHttps(url) {
388
382
  }
389
383
 
390
384
  /**
391
- * Collects and parses data from a JSON file
385
+ * Sends GET request to specified URL
392
386
  * @async
393
- * @param {string} filePath
387
+ * @param {string} url
388
+ * @returns {Promise<Object>} - Returns response if response is ok, otherwise it throws an Error.
389
+ * @throws {Error} - Throws Error if an error is detected.
390
+ */
391
+ async function reqGet(url) {
392
+ try {
393
+ const res = await fetch(url, {
394
+ method: 'GET'
395
+ });
396
+ return res;
397
+ } catch (er) {
398
+ throw new Error(er.message);
399
+ }
400
+ }
401
+
402
+ /**
403
+ * Collects JSON data from a given url
404
+ * @async
405
+ * @param {string} url
394
406
  * @returns {Promise<Object>} - Returns data if response is ok, otherwise it throws an Error.
395
407
  * @throws {Error} - Throws Error if an error is detected.
396
408
  */
397
- async function reqFileJson(filePath) {
409
+ async function reqGetJson(url) {
398
410
  try {
399
- const res = await fetch(filePath);
400
- if (!res.ok) {
401
- throw new Error(res);
402
- }
411
+ const res = await fetch(url, {
412
+ method: 'GET'
413
+ });
403
414
  const data = await res.json();
404
415
  return data;
405
416
  } catch (er) {
@@ -408,21 +419,19 @@ async function reqFileJson(filePath) {
408
419
  }
409
420
 
410
421
  /**
411
- * Sends GET request to specified URL
422
+ * Collects text data from a given url
412
423
  * @async
413
424
  * @param {string} url
414
- * @returns {Promise<Object>} - Returns response if response is ok, otherwise it throws an Error.
425
+ * @returns {Promise<Object>} - Returns data if response is ok, otherwise it throws an Error.
415
426
  * @throws {Error} - Throws Error if an error is detected.
416
427
  */
417
- async function reqGet(url) {
428
+ async function reqGetText(url) {
418
429
  try {
419
430
  const res = await fetch(url, {
420
431
  method: 'GET'
421
432
  });
422
- if (!res.ok) {
423
- throw new Error(res);
424
- }
425
- return res;
433
+ const data = await res.text();
434
+ return data;
426
435
  } catch (er) {
427
436
  throw new Error(er.message);
428
437
  }
@@ -451,31 +460,28 @@ async function reqPostJson(url, dataJson) {
451
460
  },
452
461
  body: dataJson
453
462
  });
454
- if (!res.ok) {
455
- throw new Error(res);
456
- }
457
463
  return res;
458
464
  } catch (er) {
459
- throw new Error(er.message);
465
+ throw new Error(er);
460
466
  }
461
467
  }
462
468
 
463
- // Simple POST request. export function is expecting FormData.
464
469
  /**
465
- * IN WORK
470
+ * Sends POST request to specified URL, which contains FormData in the body.
471
+ * @param {string} url
472
+ * @param {FormData} dataForm
473
+ * @returns {Promise<Object>} - Returns response if response is ok, otherwise it throws an Error.
474
+ * @throws {Error} - Throws Error if an error is detected.
466
475
  */
467
476
  async function reqPostForm(url, dataForm) {
468
477
  try {
469
478
  if (!(dataForm instanceof FormData)) {
470
- throw `The data provided was not form data`;
479
+ throw new Error(`The data provided was not form data`);
471
480
  }
472
481
  const res = await fetch(url, {
473
482
  method: 'POST',
474
483
  body: dataForm
475
484
  });
476
- if (!res.ok) {
477
- return res;
478
- }
479
485
  return res;
480
486
  } catch (er) {
481
487
  throw new Error(er);
@@ -556,29 +562,45 @@ function sleep(ms) {
556
562
  }
557
563
 
558
564
  /**
559
- * Adds and intersectionObserver for a list of target elements, that calls a function when
560
- * @param {Array} targets - An array of elements that will be attached to the observer.
561
- * @param {Function} callback - The callback function that will run when the observer is triggered.
562
- * @param {Object} observerOptions - An Object containing the observer options. By default only threshold is set.
563
- * @returns {Function} - Returns a cleanup function that will disconnect the observer. This is not required to use.
564
- */
565
- function scrollIntoView(targets, callback, observerOptions = {
566
- threshold: 0.5
567
- }) {
568
- const observer = new IntersectionObserver(entries => {
569
- entries.forEach(entry => {
570
- if (entry.isIntersecting) {
571
- // Only trigger when the element is intersecting
572
- callback(entry.target);
565
+ *
566
+ */
567
+ function scrollIntoView() {
568
+ let allObservers = [];
569
+ function addObserver(targets, callback, observerOptions = {
570
+ threshold: 0.5
571
+ }) {
572
+ const observer = new IntersectionObserver(entries => {
573
+ entries.forEach(entry => {
574
+ if (entry.isIntersecting) {
575
+ // Only trigger when the element is intersecting
576
+ callback(entry.target);
577
+ }
578
+ });
579
+ }, observerOptions);
580
+ for (let i = 0; i < targets.length; i++) {
581
+ observer.observe(targets[i]);
582
+ }
583
+ allObservers.push(observer);
584
+ }
585
+ function removeNamedObserver(name) {
586
+ for (let i = 0; i < allObservers.length; i++) {
587
+ if (allObservers[i].name === name) {
588
+ allObservers[i].disconnect();
589
+ allObservers.splice(i, 1);
590
+ i--;
573
591
  }
574
- });
575
- }, observerOptions);
576
- targets.forEach(target => {
577
- observer.observe(target);
578
- });
579
- // Return a cleanup function to disconnect the observer
580
- return () => {
581
- observer.disconnect();
592
+ }
593
+ }
594
+ function removeAllObservers() {
595
+ for (let i = 0; i < allObservers.length; i++) {
596
+ allObservers[i].disconnect();
597
+ }
598
+ allObservers = [];
599
+ }
600
+ return {
601
+ addObserver,
602
+ removeNamedObserver,
603
+ removeAllObservers
582
604
  };
583
605
  }
584
606
 
@@ -614,8 +636,9 @@ exports.regExNumbers = regExNumbers;
614
636
  exports.regExSpecial = regExSpecial;
615
637
  exports.regExUrl = regExUrl;
616
638
  exports.removeChar = removeChar;
617
- exports.reqFileJson = reqFileJson;
618
639
  exports.reqGet = reqGet;
640
+ exports.reqGetJson = reqGetJson;
641
+ exports.reqGetText = reqGetText;
619
642
  exports.reqPostForm = reqPostForm;
620
643
  exports.reqPostJson = reqPostJson;
621
644
  exports.scrollIntoView = scrollIntoView;
package/src/esm/index.js CHANGED
@@ -20,8 +20,9 @@ export {
20
20
  parseTemplate,
21
21
  } from './lib/util.dom.js';
22
22
  export {
23
- reqFileJson,
24
23
  reqGet,
24
+ reqGetJson,
25
+ reqGetText,
25
26
  reqPostForm,
26
27
  reqPostJson,
27
28
  urlAddHttp,
@@ -78,9 +78,3 @@ 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
- */
@@ -41,18 +41,31 @@ export function urlAddHttps(url) {
41
41
  }
42
42
 
43
43
  /**
44
- * Collects and parses data from a JSON file
44
+ * Sends GET request to specified URL
45
45
  * @async
46
- * @param {string} filePath
46
+ * @param {string} url
47
+ * @returns {Promise<Object>} - Returns response if response is ok, otherwise it throws an Error.
48
+ * @throws {Error} - Throws Error if an error is detected.
49
+ */
50
+ export async function reqGet(url) {
51
+ try {
52
+ const res = await fetch(url, { method: 'GET' });
53
+ return res;
54
+ } catch (er) {
55
+ throw new Error(er.message);
56
+ }
57
+ }
58
+
59
+ /**
60
+ * Collects JSON data from a given url
61
+ * @async
62
+ * @param {string} url
47
63
  * @returns {Promise<Object>} - Returns data if response is ok, otherwise it throws an Error.
48
64
  * @throws {Error} - Throws Error if an error is detected.
49
65
  */
50
- export async function reqFileJson(filePath) {
66
+ export async function reqGetJson(url) {
51
67
  try {
52
- const res = await fetch(filePath);
53
- if (!res.ok) {
54
- throw new Error(res);
55
- }
68
+ const res = await fetch(url, { method: 'GET' });
56
69
  const data = await res.json();
57
70
  return data;
58
71
  } catch (er) {
@@ -61,19 +74,17 @@ export async function reqFileJson(filePath) {
61
74
  }
62
75
 
63
76
  /**
64
- * Sends GET request to specified URL
77
+ * Collects text data from a given url
65
78
  * @async
66
79
  * @param {string} url
67
- * @returns {Promise<Object>} - Returns response if response is ok, otherwise it throws an Error.
80
+ * @returns {Promise<Object>} - Returns data if response is ok, otherwise it throws an Error.
68
81
  * @throws {Error} - Throws Error if an error is detected.
69
82
  */
70
- export async function reqGet(url) {
83
+ export async function reqGetText(url) {
71
84
  try {
72
85
  const res = await fetch(url, { method: 'GET' });
73
- if (!res.ok) {
74
- throw new Error(res);
75
- }
76
- return res;
86
+ const data = await res.text();
87
+ return data;
77
88
  } catch (er) {
78
89
  throw new Error(er.message);
79
90
  }
@@ -102,31 +113,28 @@ export async function reqPostJson(url, dataJson) {
102
113
  },
103
114
  body: dataJson
104
115
  });
105
- if (!res.ok) {
106
- throw new Error(res);
107
- }
108
116
  return res;
109
117
  } catch (er) {
110
- throw new Error(er.message);
118
+ throw new Error(er);
111
119
  }
112
120
  }
113
121
 
114
- // Simple POST request. export function is expecting FormData.
115
122
  /**
116
- * IN WORK
123
+ * Sends POST request to specified URL, which contains FormData in the body.
124
+ * @param {string} url
125
+ * @param {FormData} dataForm
126
+ * @returns {Promise<Object>} - Returns response if response is ok, otherwise it throws an Error.
127
+ * @throws {Error} - Throws Error if an error is detected.
117
128
  */
118
129
  export async function reqPostForm(url, dataForm) {
119
130
  try {
120
131
  if (!(dataForm instanceof FormData)) {
121
- throw `The data provided was not form data`;
132
+ throw new Error(`The data provided was not form data`);
122
133
  }
123
134
  const res = await fetch(url, {
124
135
  method: 'POST',
125
136
  body: dataForm
126
137
  });
127
- if (!res.ok) {
128
- return res;
129
- }
130
138
  return res;
131
139
  } catch (er) {
132
140
  throw new Error(er);
@@ -68,25 +68,46 @@ export function sleep(ms) {
68
68
  }
69
69
 
70
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.
71
+ *
76
72
  */
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);
73
+ export function scrollIntoView() {
74
+ let allObservers = [];
75
+
76
+ function addObserver(targets, callback, observerOptions = { threshold: 0.5 }) {
77
+ const observer = new IntersectionObserver((entries) => {
78
+ entries.forEach(entry => {
79
+ if (entry.isIntersecting) { // Only trigger when the element is intersecting
80
+ callback(entry.target);
81
+ }
82
+ });
83
+ }, observerOptions);
84
+ for (let i = 0; i < targets.length; i++) {
85
+ observer.observe(targets[i])
86
+
87
+ }
88
+ allObservers.push(observer);
89
+ }
90
+
91
+ function removeNamedObserver(name) {
92
+ for (let i = 0; i < allObservers.length; i++) {
93
+ if (allObservers[i].name === name) {
94
+ allObservers[i].disconnect();
95
+ allObservers.splice(i, 1);
96
+ i--;
82
97
  }
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
- };
98
+ }
99
+ }
100
+
101
+ function removeAllObservers() {
102
+ for (let i = 0; i < allObservers.length; i++) {
103
+ allObservers[i].disconnect();
104
+ }
105
+ allObservers = [];
106
+ }
107
+
108
+ return {
109
+ addObserver,
110
+ removeNamedObserver,
111
+ removeAllObservers
112
+ }
92
113
  }