@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.
@@ -2,33 +2,33 @@ import path from 'node:path';
2
2
  import process from 'node:process';
3
3
  import dotenv from 'dotenv';
4
4
 
5
- /**
6
- * vanilla :: index.mjs.
7
- *
8
- * Vanilla JS helper utilities.
9
- *
10
- * These docs are generated from inline JSDoc in the repo and are intended for contributors.
11
- *
12
- * @generated
13
- * @module vanilla
5
+ /**
6
+ * vanilla :: index.mjs.
7
+ *
8
+ * Vanilla JS helper utilities.
9
+ *
10
+ * These docs are generated from inline JSDoc in the repo and are intended for contributors.
11
+ *
12
+ * @generated
13
+ * @module vanilla
14
14
  */
15
15
 
16
16
 
17
- /**
18
- * Fetcher.
19
- *
20
- * @param {object} options - Options object.
21
- * @param {string} options.url - Value.
22
- * @param {any} [options.options=null] - Options that control behavior.
23
- * @param {boolean} [options.throwOnError=false] - Value.
24
- * @returns Result of the operation.
25
- * @throws When the operation fails.
26
- *
27
- * @example
28
- * // vanilla (src/tools/javascript/vanilla/index.mjs)
29
- * // import { fetcher } from '@vettvangur/vanilla'
30
- *
31
- * await fetcher({ url: url, options: {}, throwOnError: throwOnError })
17
+ /**
18
+ * Fetcher.
19
+ *
20
+ * @param {object} options - Options object.
21
+ * @param {string} options.url - Value.
22
+ * @param {any} [options.options=null] - Options that control behavior.
23
+ * @param {boolean} [options.throwOnError=false] - Value.
24
+ * @returns Result of the operation.
25
+ * @throws When the operation fails.
26
+ *
27
+ * @example
28
+ * // vanilla (src/tools/javascript/vanilla/index.mjs)
29
+ * // import { fetcher } from '@vettvangur/vanilla'
30
+ *
31
+ * await fetcher({ url: url, options: {}, throwOnError: throwOnError })
32
32
  */
33
33
 
34
34
  async function fetcher({
@@ -40,34 +40,26 @@ async function fetcher({
40
40
  try {
41
41
  const request = await fetch(url, options);
42
42
  if (!request.ok) {
43
- const errorDetails = await request.text();
44
- const fullErrorMessage = `${errorMessage} - Status: ${request.status}, ${request.statusText}, Details: ${errorDetails}`;
45
- console.error(fullErrorMessage);
46
43
  if (throwOnError) {
47
- throw new Error(fullErrorMessage);
44
+ const errorDetails = await request.text();
45
+ throw new Error(`${errorMessage} - Status: ${request.status}, ${request.statusText}, Details: ${errorDetails}`);
48
46
  }
49
-
50
- // Return null instead of undefined on error for predictable handling
51
47
  return null;
52
48
  }
53
49
  return await request.json();
54
50
  } catch (error) {
55
- const fullErrorMessage = `${errorMessage} - ${error}`;
56
- console.error(fullErrorMessage);
57
51
  if (throwOnError) {
58
52
  throw error;
59
53
  }
60
-
61
- // Return null instead of undefined on error for predictable handling
62
54
  return null;
63
55
  }
64
56
  }
65
57
 
66
- /**
67
- * Umbraco.
68
- *
69
- * @example
70
- * console.log(Umbraco)
58
+ /**
59
+ * Umbraco.
60
+ *
61
+ * @example
62
+ * console.log(Umbraco)
71
63
  */
72
64
  const Umbraco = Object.freeze({
73
65
  NODE: 'node',
@@ -140,11 +132,11 @@ async function loadEnvFiles(showMessage = true) {
140
132
  * @param {string} [options.expand=''] - Fields to expand.
141
133
  * @param {string} [options.node=''] - Specific node to fetch extras for (if applicable).
142
134
  * @param {string} [options.contentType=''] - Type of content - used to fetch extraData for that specific type.
143
- * @returns {string} The constructed URL for fetching Umbraco content.
135
+ * @returns {Promise<string>} The constructed URL for fetching Umbraco content.
144
136
  * @throws Will throw an error if an unsupported fetch type is provided.
145
137
  */
146
- function getUmbracoFetchUrl(route, fetchType, options = {}) {
147
- loadEnvFiles();
138
+ async function getUmbracoFetchUrl(route, fetchType, options = {}) {
139
+ await loadEnvFiles(false);
148
140
  const params = {
149
141
  skip: options.skip || 0,
150
142
  take: options.take || 9999,
@@ -197,21 +189,16 @@ async function getUmbracoData({
197
189
  options = {},
198
190
  fetchOptions = {}
199
191
  }) {
200
- try {
201
- /** @type {string[]} */
202
- const validFetchTypes = Object.values(Umbraco);
203
- if (!validFetchTypes.includes(fetchType)) {
204
- throw new Error(`Invalid fetch type: ${fetchType}`);
205
- }
206
- const umbracofetchUrl = getUmbracoFetchUrl(route, fetchType, options);
207
- return await fetcher({
208
- url: umbracofetchUrl,
209
- options: fetchOptions
210
- });
211
- } catch (error) {
212
- console.error('[@vettvangur/vanilla :: getUmbracoData]', error);
213
- return null;
192
+ /** @type {string[]} */
193
+ const validFetchTypes = Object.values(Umbraco);
194
+ if (!validFetchTypes.includes(fetchType)) {
195
+ throw new Error(`Invalid fetch type: ${fetchType}`);
214
196
  }
197
+ const umbracofetchUrl = await getUmbracoFetchUrl(route, fetchType, options);
198
+ return fetcher({
199
+ url: umbracofetchUrl,
200
+ options: fetchOptions
201
+ });
215
202
  }
216
203
 
217
204
  export { getUmbracoData, loadEnvFiles };
@@ -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
- /**
13
- * @memberof @vettvangur/vanilla
12
+ /**
13
+ * @memberof @vettvangur/vanilla
14
14
  */
15
15
 
16
- /**
17
- * Capitalize.
18
- *
19
- * @param value - Value.
20
- * @param lowerRest - Value.
21
- * @returns Result of the operation.
22
- *
23
- * @example
24
- * // vanilla (src/tools/javascript/vanilla/index.mjs)
25
- * // import { capitalize } from '@vettvangur/vanilla'
26
- *
27
- * capitalize(value, lowerRest)
16
+ /**
17
+ * Capitalize.
18
+ *
19
+ * @param value - Value.
20
+ * @param lowerRest - Value.
21
+ * @returns Result of the operation.
22
+ *
23
+ * @example
24
+ * // vanilla (src/tools/javascript/vanilla/index.mjs)
25
+ * // import { capitalize } from '@vettvangur/vanilla'
26
+ *
27
+ * capitalize(value, lowerRest)
28
28
  */
29
29
  function capitalize(value, lowerRest = false) {
30
30
  if (!value) {
@@ -35,46 +35,58 @@ function capitalize(value, lowerRest = false) {
35
35
  return firstChar + rest;
36
36
  }
37
37
 
38
- /**
39
- * Dictionary.
40
- *
41
- * @param key - Value.
42
- * @param data - Value.
43
- * @param culture - Value.
44
- * @returns Result of the operation.
45
- *
46
- * @example
47
- * // vanilla (src/tools/javascript/vanilla/index.mjs)
48
- * // import { dictionary } from '@vettvangur/vanilla'
49
- *
50
- * dictionary(key, data, culture)
38
+ /**
39
+ * Dictionary.
40
+ *
41
+ * @param key - Value.
42
+ * @param data - Value.
43
+ * @param culture - Value.
44
+ * @returns Result of the operation.
45
+ *
46
+ * @example
47
+ * // vanilla (src/tools/javascript/vanilla/index.mjs)
48
+ * // import { dictionary } from '@vettvangur/vanilla'
49
+ *
50
+ * dictionary(key, data, culture)
51
51
  */
52
+ // WeakMap keyed by the input `data` array gives us O(1) memo lookups while
53
+ // still letting unreachable translation arrays be garbage-collected.
54
+ const dictionaryIndex = new WeakMap();
52
55
  function dictionary(key, data, culture = 'is-IS') {
56
+ var _item$values$find;
53
57
  const notFound = '[Translation not found]';
54
58
  if (data === null || data === undefined) {
55
59
  return notFound;
56
60
  }
57
- for (const item of data) {
58
- if (item.itemKey === key) {
59
- var _item$values$find;
60
- const translation = (_item$values$find = item.values.find(b => b.language === culture)) === null || _item$values$find === void 0 ? void 0 : _item$values$find.value;
61
- return translation || notFound;
61
+ let byKey = dictionaryIndex.get(data);
62
+ if (!byKey) {
63
+ byKey = new Map();
64
+ for (const _item of data) {
65
+ if (_item && _item.itemKey) {
66
+ byKey.set(_item.itemKey, _item);
67
+ }
62
68
  }
69
+ dictionaryIndex.set(data, byKey);
63
70
  }
64
- return notFound;
71
+ const item = byKey.get(key);
72
+ if (!item) {
73
+ return notFound;
74
+ }
75
+ const translation = (_item$values$find = item.values.find(b => b.language === culture)) === null || _item$values$find === void 0 ? void 0 : _item$values$find.value;
76
+ return translation || notFound;
65
77
  }
66
78
 
67
- /**
68
- * Return whether empty is enabled.
69
- *
70
- * @param value - Value.
71
- * @returns Boolean indicating whether empty is enabled.
72
- *
73
- * @example
74
- * // vanilla (src/tools/javascript/vanilla/index.mjs)
75
- * // import { isEmpty } from '@vettvangur/vanilla'
76
- *
77
- * isEmpty(value)
79
+ /**
80
+ * Return whether empty is enabled.
81
+ *
82
+ * @param value - Value.
83
+ * @returns Boolean indicating whether empty is enabled.
84
+ *
85
+ * @example
86
+ * // vanilla (src/tools/javascript/vanilla/index.mjs)
87
+ * // import { isEmpty } from '@vettvangur/vanilla'
88
+ *
89
+ * isEmpty(value)
78
90
  */
79
91
  function isEmpty(value) {
80
92
  if (value === '' || value === undefined || value === null) {
@@ -83,45 +95,42 @@ function isEmpty(value) {
83
95
  return false;
84
96
  }
85
97
 
86
- /**
87
- * Get translation.
88
- *
89
- * @param key - Value.
90
- * @param translations - Value.
91
- * @returns Result of the operation.
92
- *
93
- * @example
94
- * // vanilla (src/tools/javascript/vanilla/index.mjs)
95
- * // import { getTranslation } from '@vettvangur/vanilla'
96
- *
97
- * getTranslation(key, translations)
98
+ /**
99
+ * Get translation.
100
+ *
101
+ * @param key - Value.
102
+ * @param translations - Value.
103
+ * @returns Result of the operation.
104
+ *
105
+ * @example
106
+ * // vanilla (src/tools/javascript/vanilla/index.mjs)
107
+ * // import { getTranslation } from '@vettvangur/vanilla'
108
+ *
109
+ * getTranslation(key, translations)
98
110
  */
99
111
  function getTranslation(key, translations) {
100
112
  if (!translations) {
101
113
  return;
102
114
  }
103
- const translation = translations[key];
104
- if (!translation) {
105
- console.error(`[vettvangur-vanilla :: getTranslation] No translation for key ${key} found.`);
106
- return;
107
- }
108
- return translation;
115
+ return translations[key];
109
116
  }
110
117
 
111
- /**
112
- * Get culture.
113
- *
114
- * @param path - Value.
115
- * @returns Result of the operation.
116
- *
117
- * @example
118
- * // vanilla (src/tools/javascript/vanilla/index.mjs)
119
- * // import { getCulture } from '@vettvangur/vanilla'
120
- *
121
- * getCulture(path)
118
+ /**
119
+ * Get culture.
120
+ *
121
+ * @param path - Value.
122
+ * @returns Result of the operation.
123
+ *
124
+ * @example
125
+ * // vanilla (src/tools/javascript/vanilla/index.mjs)
126
+ * // import { getCulture } from '@vettvangur/vanilla'
127
+ *
128
+ * getCulture(path)
122
129
  */
123
130
  function getCulture(path) {
124
- const match = path.match(/^\/([a-z]{2})(\/|$)/i);
131
+ // Match BCP-47-ish prefixes: 2-3 letter language, optionally followed by
132
+ // a region subtag (e.g. `en-US`, `eng-US`, `is`, `pt-BR`).
133
+ const match = path.match(/^\/([a-z]{2,3}(?:-[a-z]{2,4})?)(\/|$)/i);
125
134
  return match ? match[1].toLowerCase() : null;
126
135
  }
127
136
 
@@ -25,20 +25,6 @@
25
25
  * capitalize(value, lowerRest)
26
26
  */
27
27
  export function capitalize(value: any, lowerRest?: boolean): any;
28
- /**
29
- * Dictionary.
30
- *
31
- * @param key - Value.
32
- * @param data - Value.
33
- * @param culture - Value.
34
- * @returns Result of the operation.
35
- *
36
- * @example
37
- * // vanilla (src/tools/javascript/vanilla/index.mjs)
38
- * // import { dictionary } from '@vettvangur/vanilla'
39
- *
40
- * dictionary(key, data, culture)
41
- */
42
28
  export function dictionary(key: any, data: any, culture?: string): any;
43
29
  /**
44
30
  * Fetcher.
@@ -113,7 +99,7 @@ export function isEmpty(value: any): boolean;
113
99
  *
114
100
  * preloadTimeout()
115
101
  */
116
- export function preloadTimeout(): void;
102
+ export function preloadTimeout(): () => void;
117
103
  /**
118
104
  * Click outside.
119
105
  *
@@ -126,7 +112,7 @@ export function preloadTimeout(): void;
126
112
  *
127
113
  * clickOutside(callback)
128
114
  */
129
- export function clickOutside(callback: any): void;
115
+ export function clickOutside(callback: any): () => void;
130
116
  /**
131
117
  * Get translation.
132
118
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vettvangur/vanilla",
3
- "version": "0.0.58",
3
+ "version": "0.0.60",
4
4
  "description": "Vettvangur | Vanilla JS Utility Library",
5
5
  "access": "public",
6
6
  "type": "module",