@vettvangur/vanilla 0.0.58 → 0.0.60

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/dist/data.esm.js CHANGED
@@ -1,30 +1,30 @@
1
- /**
2
- * vanilla :: index.mjs.
3
- *
4
- * Vanilla JS helper utilities.
5
- *
6
- * These docs are generated from inline JSDoc in the repo and are intended for contributors.
7
- *
8
- * @generated
9
- * @module vanilla
1
+ /**
2
+ * vanilla :: index.mjs.
3
+ *
4
+ * Vanilla JS helper utilities.
5
+ *
6
+ * These docs are generated from inline JSDoc in the repo and are intended for contributors.
7
+ *
8
+ * @generated
9
+ * @module vanilla
10
10
  */
11
11
 
12
12
 
13
- /**
14
- * Fetcher.
15
- *
16
- * @param {object} options - Options object.
17
- * @param {string} options.url - Value.
18
- * @param {any} [options.options=null] - Options that control behavior.
19
- * @param {boolean} [options.throwOnError=false] - Value.
20
- * @returns Result of the operation.
21
- * @throws When the operation fails.
22
- *
23
- * @example
24
- * // vanilla (src/tools/javascript/vanilla/index.mjs)
25
- * // import { fetcher } from '@vettvangur/vanilla'
26
- *
27
- * await fetcher({ url: url, options: {}, throwOnError: throwOnError })
13
+ /**
14
+ * Fetcher.
15
+ *
16
+ * @param {object} options - Options object.
17
+ * @param {string} options.url - Value.
18
+ * @param {any} [options.options=null] - Options that control behavior.
19
+ * @param {boolean} [options.throwOnError=false] - Value.
20
+ * @returns Result of the operation.
21
+ * @throws When the operation fails.
22
+ *
23
+ * @example
24
+ * // vanilla (src/tools/javascript/vanilla/index.mjs)
25
+ * // import { fetcher } from '@vettvangur/vanilla'
26
+ *
27
+ * await fetcher({ url: url, options: {}, throwOnError: throwOnError })
28
28
  */
29
29
 
30
30
  async function fetcher({
@@ -36,35 +36,27 @@ async function fetcher({
36
36
  try {
37
37
  const request = await fetch(url, options);
38
38
  if (!request.ok) {
39
- const errorDetails = await request.text();
40
- const fullErrorMessage = `${errorMessage} - Status: ${request.status}, ${request.statusText}, Details: ${errorDetails}`;
41
- console.error(fullErrorMessage);
42
39
  if (throwOnError) {
43
- throw new Error(fullErrorMessage);
40
+ const errorDetails = await request.text();
41
+ throw new Error(`${errorMessage} - Status: ${request.status}, ${request.statusText}, Details: ${errorDetails}`);
44
42
  }
45
-
46
- // Return null instead of undefined on error for predictable handling
47
43
  return null;
48
44
  }
49
45
  return await request.json();
50
46
  } catch (error) {
51
- const fullErrorMessage = `${errorMessage} - ${error}`;
52
- console.error(fullErrorMessage);
53
47
  if (throwOnError) {
54
48
  throw error;
55
49
  }
56
-
57
- // Return null instead of undefined on error for predictable handling
58
50
  return null;
59
51
  }
60
52
  }
61
53
 
62
- /**
63
- * Checks if a given string is a valid regular expression pattern.
64
- *
65
- * @ignore
66
- * @param {string} pattern - The regex pattern to validate.
67
- * @returns {boolean} Returns `true` if the pattern is a valid regex, otherwise `false`.
54
+ /**
55
+ * Checks if a given string is a valid regular expression pattern.
56
+ *
57
+ * @ignore
58
+ * @param {string} pattern - The regex pattern to validate.
59
+ * @returns {boolean} Returns `true` if the pattern is a valid regex, otherwise `false`.
68
60
  */
69
61
  function isValidRegex(pattern) {
70
62
  try {
@@ -75,25 +67,25 @@ function isValidRegex(pattern) {
75
67
  }
76
68
  }
77
69
 
78
- /**
79
- * Regexr.
80
- *
81
- * @example
82
- * console.log(regexr)
70
+ /**
71
+ * Regexr.
72
+ *
73
+ * @example
74
+ * console.log(regexr)
83
75
  */
84
76
  const regexr = {
85
- /**
86
- * Tests whether a given value matches a regex pattern.
87
- *
88
- * @param {Object} params - Parameters for testing regex.
89
- * @param {string} params.value - The string to test against the regex.
90
- * @param {string} params.pattern - The regex pattern to match.
91
- * @param {string} [params.flags='g'] - Regex flags (default: 'g').
92
- * @returns {boolean} Returns `true` if the value matches the pattern, otherwise `false`.
93
- *
94
- * @example
95
- * const isMatch = regexr.match(pattern)
96
- * console.log(isMatch) // return true or false
77
+ /**
78
+ * Tests whether a given value matches a regex pattern.
79
+ *
80
+ * @param {Object} params - Parameters for testing regex.
81
+ * @param {string} params.value - The string to test against the regex.
82
+ * @param {string} params.pattern - The regex pattern to match.
83
+ * @param {string} [params.flags='g'] - Regex flags (default: 'g').
84
+ * @returns {boolean} Returns `true` if the value matches the pattern, otherwise `false`.
85
+ *
86
+ * @example
87
+ * const isMatch = regexr.match(pattern)
88
+ * console.log(isMatch) // return true or false
97
89
  */
98
90
  test({
99
91
  value,
@@ -106,14 +98,14 @@ const regexr = {
106
98
  const regexp = new RegExp(pattern, flags);
107
99
  return regexp.test(value);
108
100
  },
109
- /**
110
- * Matches a given value against a regex pattern and returns the matches.
111
- *
112
- * @param {Object} params - Parameters for matching regex.
113
- * @param {string} params.value - The string to match against the regex.
114
- * @param {string} params.pattern - The regex pattern to match.
115
- * @param {string} [params.flags='g'] - Regex flags (default: 'g').
116
- * @returns {Array<string>|null} Returns an array of matches, or `null` if no match is found.
101
+ /**
102
+ * Matches a given value against a regex pattern and returns the matches.
103
+ *
104
+ * @param {Object} params - Parameters for matching regex.
105
+ * @param {string} params.value - The string to match against the regex.
106
+ * @param {string} params.pattern - The regex pattern to match.
107
+ * @param {string} [params.flags='g'] - Regex flags (default: 'g').
108
+ * @returns {Array<string>|null} Returns an array of matches, or `null` if no match is found.
117
109
  */
118
110
  match({
119
111
  value,
@@ -128,40 +120,35 @@ const regexr = {
128
120
  }
129
121
  };
130
122
 
131
- /**
132
- * Flatten.
133
- *
134
- * @param obj - Value.
135
- * @param prefix - Value.
136
- * @returns Result of the operation.
137
- *
138
- * @example
139
- * // vanilla (src/tools/javascript/vanilla/index.mjs)
140
- * // import { flatten } from '@vettvangur/vanilla'
141
- *
142
- * flatten(obj, prefix)
123
+ /**
124
+ * Flatten.
125
+ *
126
+ * @param obj - Value.
127
+ * @param prefix - Value.
128
+ * @returns Result of the operation.
129
+ *
130
+ * @example
131
+ * // vanilla (src/tools/javascript/vanilla/index.mjs)
132
+ * // import { flatten } from '@vettvangur/vanilla'
133
+ *
134
+ * flatten(obj, prefix)
143
135
  */
144
136
  const flatten = (obj, prefix = '') => {
145
- try {
146
- if (!obj || typeof obj !== 'object') {
147
- console.error('❌ FLATTEN ERROR: Received invalid data:', obj);
148
- return {};
149
- }
150
- return Object.keys(obj).reduce((acc, key) => {
151
- const fullKey = key === 'DEFAULT' ? prefix : prefix ? `${prefix}-${key}` : key;
152
- if (typeof obj[key] === 'object' && key !== 'DEFAULT') {
153
- Object.assign(acc, flatten(obj[key], fullKey));
154
- } else if (typeof obj[key] === 'string') {
155
- acc[fullKey] = obj[key];
156
- } else {
157
- console.warn('! Skipping Invalid Key:', key, 'Value:', obj[key]);
158
- }
159
- return acc;
160
- }, {});
161
- } catch (error) {
162
- console.error('🚨 Flatten Function Error:', error);
163
- return {}; // Prevents Tailwind from crashing
137
+ if (obj == null) {
138
+ return {};
164
139
  }
140
+ if (typeof obj !== 'object') {
141
+ throw new TypeError(`flatten: expected an object, got ${typeof obj}`);
142
+ }
143
+ return Object.keys(obj).reduce((acc, key) => {
144
+ const fullKey = key === 'DEFAULT' ? prefix : prefix ? `${prefix}-${key}` : key;
145
+ if (typeof obj[key] === 'object' && key !== 'DEFAULT') {
146
+ Object.assign(acc, flatten(obj[key], fullKey));
147
+ } else if (typeof obj[key] === 'string') {
148
+ acc[fullKey] = obj[key];
149
+ }
150
+ return acc;
151
+ }, {});
165
152
  };
166
153
 
167
154
  export { fetcher, flatten, regexr };
package/dist/dom.esm.js CHANGED
@@ -1,91 +1,94 @@
1
- /**
2
- * vanilla :: index.mjs.
3
- *
4
- * Vanilla JS helper utilities.
5
- *
6
- * These docs are generated from inline JSDoc in the repo and are intended for contributors.
7
- *
8
- * @generated
9
- * @module vanilla
1
+ /**
2
+ * vanilla :: index.mjs.
3
+ *
4
+ * Vanilla JS helper utilities.
5
+ *
6
+ * These docs are generated from inline JSDoc in the repo and are intended for contributors.
7
+ *
8
+ * @generated
9
+ * @module vanilla
10
10
  */
11
11
 
12
12
 
13
- /**
14
- * Preload timeout.
15
- *
16
- * @returns Nothing.
17
- *
18
- * @example
19
- * // vanilla (src/tools/javascript/vanilla/index.mjs)
20
- * // import { preloadTimeout } from '@vettvangur/vanilla'
21
- *
22
- * preloadTimeout()
13
+ /**
14
+ * Preload timeout.
15
+ *
16
+ * @returns Nothing.
17
+ *
18
+ * @example
19
+ * // vanilla (src/tools/javascript/vanilla/index.mjs)
20
+ * // import { preloadTimeout } from '@vettvangur/vanilla'
21
+ *
22
+ * preloadTimeout()
23
23
  */
24
24
  function preloadTimeout() {
25
+ if (typeof document === 'undefined') {
26
+ return () => {};
27
+ }
25
28
  const body = document.querySelector('.body');
26
29
  if (!body) {
27
- return;
30
+ return () => {};
28
31
  }
29
- setTimeout(() => body.classList.add('loaded'), 100);
30
- setTimeout(() => {
32
+ const t1 = setTimeout(() => body.classList.add('loaded'), 100);
33
+ const t2 = setTimeout(() => {
31
34
  body.classList.add('preload--hidden');
32
35
  body.classList.remove('preload--transitions');
33
36
  }, 400);
37
+ return () => {
38
+ clearTimeout(t1);
39
+ clearTimeout(t2);
40
+ };
34
41
  }
35
42
 
36
- /**
37
- * Click outside.
38
- *
39
- * @param callback - Value.
40
- * @returns Nothing.
41
- *
42
- * @example
43
- * // vanilla (src/tools/javascript/vanilla/index.mjs)
44
- * // import { clickOutside } from '@vettvangur/vanilla'
45
- *
46
- * clickOutside(callback)
43
+ /**
44
+ * Click outside.
45
+ *
46
+ * @param callback - Value.
47
+ * @returns Nothing.
48
+ *
49
+ * @example
50
+ * // vanilla (src/tools/javascript/vanilla/index.mjs)
51
+ * // import { clickOutside } from '@vettvangur/vanilla'
52
+ *
53
+ * clickOutside(callback)
47
54
  */
48
55
  function clickOutside(callback) {
49
- // Clicks
50
- document.addEventListener('click', e => {
56
+ if (typeof document === 'undefined') {
57
+ return () => {};
58
+ }
59
+ const onClick = e => {
51
60
  const target = e.target;
52
61
  const classTargets = document.querySelectorAll('.co-el');
53
62
 
54
- // Check if the click target is inside an element with class `.co-trigger`
55
63
  // @ts-ignore
56
64
  if (target.closest('.co-trigger')) {
57
65
  return;
58
66
  }
59
-
60
- // Execute the callback function
61
67
  callback(e);
62
-
63
- // Close open elements
64
68
  Array.prototype.forEach.call(classTargets, item => {
65
69
  if (item.classList.contains('open')) {
66
70
  item.classList.remove('open');
67
71
  }
68
72
  });
69
- }, false);
70
-
71
- // ESC key
72
- document.addEventListener('keyup', e => {
73
+ };
74
+ const onKeyUp = e => {
73
75
  const openElements = document.querySelectorAll('.open');
74
76
  const keys = e.keyCode || e.which;
75
-
76
- // If ESC key (key code 27) is pressed
77
77
  if (keys === 27) {
78
- // Execute the callback function
79
78
  callback(e);
80
-
81
- // Close open elements
82
79
  Array.prototype.forEach.call(openElements, item => {
83
80
  if (item.classList.contains('open')) {
84
81
  item.classList.remove('open');
85
82
  }
86
83
  });
87
84
  }
88
- });
85
+ };
86
+ document.addEventListener('click', onClick, false);
87
+ document.addEventListener('keyup', onKeyUp);
88
+ return () => {
89
+ document.removeEventListener('click', onClick, false);
90
+ document.removeEventListener('keyup', onKeyUp);
91
+ };
89
92
  }
90
93
 
91
94
  export { clickOutside, preloadTimeout };