@vettvangur/vanilla 0.0.2 → 0.0.3

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": "@vettvangur/vanilla",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Vettvangur | Vanilla JS Utility Library",
5
5
  "access": "private",
6
6
  "type": "module",
@@ -32,6 +32,6 @@
32
32
  "scripts": {
33
33
  "bundle": "pnpm types & rollup -c",
34
34
  "dist": "pnpm publish",
35
- "types": "tsc -p tsconfig.json & shx mv dist/types/index.d.mts dist/types/index.d.ts"
35
+ "types": "tsc -p tsconfig.json && shx mv dist/types/index.d.mts dist/types/index.d.ts"
36
36
  }
37
37
  }
@@ -1,249 +0,0 @@
1
- /**
2
- * Capitalizes the first letter of a given string.
3
- *
4
- * @function capitalize
5
- * @param {string|null|undefined} value - The string to capitalize. If null or undefined, returns an empty string.
6
- * @param {boolean} [lowerRest=false] - If true, converts the rest of the string to lowercase.
7
- * @returns {string} The capitalized string, or an empty string if the input is falsy.
8
- *
9
- * @example capitalize(string)
10
- */
11
- export function capitalize(value: string | null | undefined, lowerRest?: boolean): string;
12
- /**
13
- * Retrieves a translated string from a dictionary based on a given key and culture.
14
- *
15
- * @function dictionary
16
- * @param {string} key - The key for the translation.
17
- * @param {Array} data - The array of translation objects.
18
- * @param {string} [culture='is-IS'] - The language code to search for (default: 'is-IS').
19
- * @returns {string} The translated string if found, otherwise '[Translation not found]'.
20
- *
21
- * @example
22
- * dictionary(key)
23
- */
24
- export function dictionary(key: string, data: any[], culture?: string): string;
25
- /**
26
- * Fetches data from a given URL with customizable options and error handling.
27
- *
28
- * @async
29
- * @function fetcher
30
- * @param {Object} params - The parameters for the fetcher function.
31
- * @param {string} params.url - The URL to fetch data from.
32
- * @param {RequestInit|null} [params.options={}] - Fetch API options (headers, method, body, etc.).
33
- * @param {boolean} [params.throwOnError=false] - Whether to throw an error on non-OK HTTP responses.
34
- * @returns {Promise<Object|null>} The parsed JSON response or null if an error occurs.
35
- * @throws Will throw an error if `throwOnError` is true and the fetch fails or returns a non-OK status.
36
- * @see https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
37
- *
38
- * @example
39
- * const getData = await fetcher({
40
- * url,
41
- * {
42
- * method: 'POST',
43
- * headers: {
44
- * 'Content-Type': 'application/json'
45
- * }
46
- * }
47
- * })
48
- */
49
- export function fetcher({ url, options, throwOnError }: {
50
- url: string;
51
- options?: RequestInit | null;
52
- throwOnError?: boolean;
53
- }): Promise<any | null>;
54
- /**
55
- * Loads environment variables from `.env` and `.env.[NODE_ENV]` files.
56
- *
57
- * This function first loads the base `.env` file, then loads an environment-specific file
58
- * based on `NODE_ENV` (e.g., `.env.production`, `.env.development`). If `NODE_ENV` is not set,
59
- * it defaults to `'development'`.
60
- *
61
- * @async
62
- * @function loadEnvFiles
63
- * @param {boolean} [showMessage=true] - Whether to log a message indicating which env file was loaded.
64
- * @throws {Error} Throws an error if loading the environment-specific file fails.
65
- * @returns {Promise<void>} Resolves when environment files are loaded.
66
- */
67
- export function loadEnvFiles(showMessage?: boolean): Promise<void>;
68
- /**
69
- * Retrieves a template based on an alias, capitalizing the alias before lookup.
70
- *
71
- * @param {string} alias - The alias to search for in the templates object.
72
- * @param {Object} templates - An object containing templates indexed by alias.
73
- * @returns {*} - The matching template, or the 'Subpage' template if not found.
74
- *
75
- * @example
76
- * const template = getTemplate('name', templates);
77
- *
78
- */
79
- export function getTemplate(alias: string, templates: any): any;
80
- /**
81
- * Checks if a given value is empty (i.e., '', undefined, or null).
82
- *
83
- * @param {*} value - The value to check.
84
- * @returns {boolean} - Returns `true` if the value is empty, otherwise `false`.
85
- *
86
- * @example
87
- * console.log(isEmpty('')); // true
88
- * console.log(isEmpty(null)); // true
89
- * console.log(isEmpty('hello')); // false
90
- */
91
- export function isEmpty(value: any): boolean;
92
- /**
93
- * Compiles a module map, converting a selector-importer pair into a `Map` for efficient lookups.
94
- *
95
- * @function createModuleMap
96
- * @param {Object} moduleMap - The module map where selectors are keys and dynamic imports are values.
97
- * @returns {Map} A Map where selectors are keys and dynamic import functions are values.
98
- *
99
- * @example
100
- * const moduleMap = {
101
- * '.selector': () => import('components/component'),
102
- * '.selector-1, .selector-2, .selector-3': () => import('components/component')
103
- * };
104
- * const compiledMap = createModuleMap(moduleMap);
105
- */
106
- export function createModuleMap(moduleMap: any): Map<any, any>;
107
- /**
108
- * Adds a preload class to the body element after a short delay.
109
- * Also hides the preload animation after another delay.
110
- *
111
- * @function preloadTimeout
112
- *
113
- * @example
114
- * preloadTimeout(); // Adds 'loaded' class after a short delay, hides preload animation after another delay.
115
- */
116
- export function preloadTimeout(): void;
117
- /**
118
- * Lazy loads modules based on a selector in the provided module map using the IntersectionObserver API.
119
- *
120
- * @function lazyModules
121
- * @param {Map} moduleMap - A map of selectors to dynamic import functions.
122
- * @param {Element} [root=document] - The root element to query for selectors (defaults to `document`).
123
- *
124
- * @example
125
- * const moduleMap = new Map([
126
- * ['.selector', () => import('components/component')],
127
- * ['.selector-1, .selector-2, .selector-3', () => import('components/compomnent')],
128
- * ]);
129
- * lazyModules(moduleMap); // Lazy load the components when their selectors are in view.
130
- */
131
- export function lazyModules(moduleMap: Map<any, any>, root?: Element): void;
132
- /**
133
- * Initializes core scripts with an optional callback function.
134
- *
135
- * @function initCoreScripts
136
- * @param {Function} callback - The callback function to initialize core scripts.
137
- *
138
- * @example
139
- * initCoreScripts(() => { console.log('Core scripts initialized'); });
140
- */
141
- export function initCoreScripts(callback?: Function): void;
142
- /**
143
- * Handles the `DOMContentLoaded` event to initialize core scripts and lazy load modules.
144
- *
145
- * @function onDomReady
146
- * @param {Map} moduleMap - The compiled module map for lazy loading.
147
- * @param {Function} initCoreScriptsCallback - The callback function to initialize core scripts.
148
- * @param {Function} [callback] - An optional callback to execute during `DOMContentLoaded` event handling.
149
- *
150
- * @example
151
- * onDomReady(moduleMap, () => { console.log('Core scripts initialized'); }, () => { console.log('DOM content loaded'); });
152
- */
153
- export function onDomReady(moduleMap: Map<any, any>, initCoreScriptsCallback: Function, callback?: Function): void;
154
- /**
155
- * Handles the `popstate` event to initialize core scripts and lazy load modules.
156
- *
157
- * @function onPopstate
158
- * @param {Map} moduleMap - The compiled module map for lazy loading.
159
- * @param {Function} initCoreScriptsCallback - The callback function to initialize core scripts.
160
- * @param {Function} [callback] - An optional callback to execute during `popstate` event handling.
161
- *
162
- * @example
163
- * onPopstate(moduleMap, () => { console.log('Core scripts initialized'); }, () => { console.log('Popstate event triggered'); });
164
- */
165
- export function onPopstate(moduleMap: Map<any, any>, initCoreScriptsCallback: Function, callback?: Function): void;
166
- /**
167
- * Handles the `htmx:beforeSwap` event to perform actions before HTMX swaps.
168
- *
169
- * @function onHtmxBeforeSwap
170
- * @param {Function} callback - The callback function to execute during `htmx:beforeSwap` event handling.
171
- *
172
- * @example
173
- * onHtmxBeforeSwap((e) => { console.log('HTMX before swap:', e); });
174
- */
175
- export function onHtmxBeforeSwap(callback?: Function): void;
176
- /**
177
- * Handles the `htmx:afterSwap` event to initialize core scripts and lazy load modules.
178
- *
179
- * @function onHtmxAfterSwap
180
- * @param {Map} moduleMap - The compiled module map for lazy loading.
181
- * @param {Function} [callback] - An optional callback to execute during `htmx:afterSwap` event handling.
182
- *
183
- * @example
184
- * onHtmxAfterSwap(moduleMap, () => { console.log('HTMX after swap completed'); });
185
- */
186
- export function onHtmxAfterSwap(moduleMap: Map<any, any>, callback?: Function): void;
187
- /**
188
- * Handles the `htmx:afterSettle` event to initialize core scripts and lazy load modules.
189
- *
190
- * @function onHtmxAfterSettle
191
- * @param {Map} moduleMap - The compiled module map for lazy loading.
192
- * @param {Function} initCoreScriptsCallback - The callback function to initialize core scripts.
193
- * @param {Function} [callback] - An optional callback to execute during `htmx:afterSettle` event handling.
194
- *
195
- * @example
196
- * onHtmxAfterSettle(moduleMap, () => { console.log('Core scripts initialized'); }, () => { console.log('HTMX settle completed'); });
197
- */
198
- export function onHtmxAfterSettle(moduleMap: Map<any, any>, initCoreScriptsCallback: Function, callback?: Function): void;
199
- /**
200
- * Handles click outside and ESC key events to trigger a callback and close open elements with the `open` class.
201
- *
202
- * @function clickOutside
203
- * @param {Function} callback - The callback function to be executed when a click outside or ESC key event occurs.
204
- * @description
205
- * This function listens for `click` events and `keyup` events:
206
- * - When a click outside of `.co-trigger` is detected, the provided callback is executed.
207
- * - When the `ESC` key is pressed, the provided callback is executed, and any open elements (with the `open` class) are closed.
208
- *
209
- * @example
210
- * ClickOutside(() => {
211
- * console.log('Click outside or ESC pressed!');
212
- * });
213
- */
214
- export function clickOutside(callback: Function): void;
215
- export namespace regexr {
216
- /**
217
- * Tests whether a given value matches a regex pattern.
218
- *
219
- * @param {Object} params - Parameters for testing regex.
220
- * @param {string} params.value - The string to test against the regex.
221
- * @param {string} params.pattern - The regex pattern to match.
222
- * @param {string} [params.flags='g'] - Regex flags (default: 'g').
223
- * @returns {boolean} Returns `true` if the value matches the pattern, otherwise `false`.
224
- *
225
- * @example
226
- * const isMatch = regexr.match(pattern)
227
- * console.log(isMatch) // return true or false
228
- */
229
- function test({ value, pattern, flags }: {
230
- value: string;
231
- pattern: string;
232
- flags?: string;
233
- }): boolean;
234
- /**
235
- * Matches a given value against a regex pattern and returns the matches.
236
- *
237
- * @param {Object} params - Parameters for matching regex.
238
- * @param {string} params.value - The string to match against the regex.
239
- * @param {string} params.pattern - The regex pattern to match.
240
- * @param {string} [params.flags='g'] - Regex flags (default: 'g').
241
- * @returns {Array<string>|null} Returns an array of matches, or `null` if no match is found.
242
- */
243
- function match({ value, pattern, flags }: {
244
- value: string;
245
- pattern: string;
246
- flags?: string;
247
- }): Array<string> | null;
248
- }
249
- export function flatten(obj: any, prefix?: string): any;