tuijs-util 1.3.8 → 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.8",
3
+ "version": "1.4.1",
4
4
  "module": "src/esm/index.js",
5
5
  "main": "src/cjs/index.cjs",
6
6
  "exports": {
package/src/cjs/index.cjs CHANGED
@@ -393,9 +393,6 @@ async function reqGet(url) {
393
393
  const res = await fetch(url, {
394
394
  method: 'GET'
395
395
  });
396
- if (!res.ok) {
397
- throw new Error(res);
398
- }
399
396
  return res;
400
397
  } catch (er) {
401
398
  throw new Error(er.message);
@@ -414,9 +411,6 @@ async function reqGetJson(url) {
414
411
  const res = await fetch(url, {
415
412
  method: 'GET'
416
413
  });
417
- if (!res.ok) {
418
- throw new Error(res);
419
- }
420
414
  const data = await res.json();
421
415
  return data;
422
416
  } catch (er) {
@@ -436,9 +430,6 @@ async function reqGetText(url) {
436
430
  const res = await fetch(url, {
437
431
  method: 'GET'
438
432
  });
439
- if (!res.ok) {
440
- throw new Error(res);
441
- }
442
433
  const data = await res.text();
443
434
  return data;
444
435
  } catch (er) {
@@ -469,12 +460,9 @@ async function reqPostJson(url, dataJson) {
469
460
  },
470
461
  body: dataJson
471
462
  });
472
- if (!res.ok) {
473
- throw new Error(res);
474
- }
475
463
  return res;
476
464
  } catch (er) {
477
- throw new Error(er.message);
465
+ throw new Error(er);
478
466
  }
479
467
  }
480
468
 
@@ -494,12 +482,9 @@ async function reqPostForm(url, dataForm) {
494
482
  method: 'POST',
495
483
  body: dataForm
496
484
  });
497
- if (!res.ok) {
498
- return res;
499
- }
500
485
  return res;
501
486
  } catch (er) {
502
- throw new Error(er.message);
487
+ throw new Error(er);
503
488
  }
504
489
  }
505
490
 
@@ -577,29 +562,45 @@ function sleep(ms) {
577
562
  }
578
563
 
579
564
  /**
580
- * Adds and intersectionObserver for a list of target elements, that calls a function when
581
- * @param {Array} targets - An array of elements that will be attached to the observer.
582
- * @param {Function} callback - The callback function that will run when the observer is triggered.
583
- * @param {Object} observerOptions - An Object containing the observer options. By default only threshold is set.
584
- * @returns {Function} - Returns a cleanup function that will disconnect the observer. This is not required to use.
585
- */
586
- function scrollIntoView(targets, callback, observerOptions = {
587
- threshold: 0.5
588
- }) {
589
- const observer = new IntersectionObserver(entries => {
590
- entries.forEach(entry => {
591
- if (entry.isIntersecting) {
592
- // Only trigger when the element is intersecting
593
- 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--;
594
591
  }
595
- });
596
- }, observerOptions);
597
- targets.forEach(target => {
598
- observer.observe(target);
599
- });
600
- // Return a cleanup function to disconnect the observer
601
- return () => {
602
- 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
603
604
  };
604
605
  }
605
606
 
@@ -50,9 +50,6 @@ export function urlAddHttps(url) {
50
50
  export async function reqGet(url) {
51
51
  try {
52
52
  const res = await fetch(url, { method: 'GET' });
53
- if (!res.ok) {
54
- throw new Error(res);
55
- }
56
53
  return res;
57
54
  } catch (er) {
58
55
  throw new Error(er.message);
@@ -69,9 +66,6 @@ export async function reqGet(url) {
69
66
  export async function reqGetJson(url) {
70
67
  try {
71
68
  const res = await fetch(url, { method: 'GET' });
72
- if (!res.ok) {
73
- throw new Error(res);
74
- }
75
69
  const data = await res.json();
76
70
  return data;
77
71
  } catch (er) {
@@ -89,9 +83,6 @@ export async function reqGetJson(url) {
89
83
  export async function reqGetText(url) {
90
84
  try {
91
85
  const res = await fetch(url, { method: 'GET' });
92
- if (!res.ok) {
93
- throw new Error(res);
94
- }
95
86
  const data = await res.text();
96
87
  return data;
97
88
  } catch (er) {
@@ -122,12 +113,9 @@ export async function reqPostJson(url, dataJson) {
122
113
  },
123
114
  body: dataJson
124
115
  });
125
- if (!res.ok) {
126
- throw new Error(res);
127
- }
128
116
  return res;
129
117
  } catch (er) {
130
- throw new Error(er.message);
118
+ throw new Error(er);
131
119
  }
132
120
  }
133
121
 
@@ -147,11 +135,8 @@ export async function reqPostForm(url, dataForm) {
147
135
  method: 'POST',
148
136
  body: dataForm
149
137
  });
150
- if (!res.ok) {
151
- return res;
152
- }
153
138
  return res;
154
139
  } catch (er) {
155
- throw new Error(er.message);
140
+ throw new Error(er);
156
141
  }
157
142
  }
@@ -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
  }