@mkmonkeycat/dom-utils 0.1.0 → 1.0.0

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.
@@ -1,179 +0,0 @@
1
- /**
2
- * Debounce function that delays the execution of a callback until after a specified wait time has elapsed.
3
- * @param callback - The function to debounce.
4
- * @param wait - The delay in milliseconds.
5
- * @returns A debounced function with cancel and flush methods.
6
- */
7
- export const debounce = <T extends (...args: unknown[]) => unknown>(
8
- callback: T,
9
- wait: number,
10
- ): ((...args: Parameters<T>) => void) & { cancel: () => void; flush: () => void } => {
11
- let timeoutId: ReturnType<typeof setTimeout> | null = null;
12
- let lastArgs: Parameters<T> | null = null;
13
-
14
- const debounced = (...args: Parameters<T>) => {
15
- lastArgs = args;
16
- if (timeoutId) clearTimeout(timeoutId);
17
- timeoutId = setTimeout(() => {
18
- callback(...lastArgs!);
19
- timeoutId = null;
20
- lastArgs = null;
21
- }, wait);
22
- };
23
-
24
- debounced.cancel = () => {
25
- if (timeoutId) clearTimeout(timeoutId);
26
- timeoutId = null;
27
- lastArgs = null;
28
- };
29
-
30
- debounced.flush = () => {
31
- if (timeoutId) {
32
- callback(...lastArgs!);
33
- clearTimeout(timeoutId);
34
- timeoutId = null;
35
- lastArgs = null;
36
- }
37
- };
38
-
39
- return debounced;
40
- };
41
-
42
- /**
43
- * Throttle function that executes a callback at most once every specified wait time.
44
- * @param callback - The function to throttle.
45
- * @param wait - The minimum interval in milliseconds between executions.
46
- * @returns A throttled function with cancel and flush methods.
47
- */
48
- export const throttle = <T extends (...args: unknown[]) => unknown>(
49
- callback: T,
50
- wait: number,
51
- ): ((...args: Parameters<T>) => void) & { cancel: () => void; flush: () => void } => {
52
- let timeoutId: ReturnType<typeof setTimeout> | null = null;
53
- let lastArgs: Parameters<T> | null = null;
54
- let lastCallTime = 0;
55
-
56
- const cleanup = () => {
57
- if (timeoutId) {
58
- clearTimeout(timeoutId);
59
- timeoutId = null;
60
- }
61
- };
62
-
63
- const throttled = (...args: Parameters<T>) => {
64
- const now = Date.now();
65
- lastArgs = args;
66
-
67
- if (lastCallTime === 0) {
68
- callback(...args);
69
- lastCallTime = now;
70
- } else {
71
- const elapsed = now - lastCallTime;
72
- if (elapsed >= wait) {
73
- callback(...args);
74
- lastCallTime = now;
75
- cleanup();
76
- } else {
77
- cleanup();
78
- timeoutId = setTimeout(() => {
79
- callback(...lastArgs!);
80
- lastCallTime = Date.now();
81
- timeoutId = null;
82
- }, wait - elapsed);
83
- }
84
- }
85
- };
86
-
87
- throttled.cancel = () => {
88
- cleanup();
89
- lastCallTime = 0;
90
- lastArgs = null;
91
- };
92
-
93
- throttled.flush = () => {
94
- if (lastCallTime > 0 && lastArgs !== null) {
95
- callback(...lastArgs);
96
- }
97
- cleanup();
98
- lastCallTime = 0;
99
- lastArgs = null;
100
- };
101
-
102
- return throttled;
103
- };
104
-
105
- /**
106
- * Creates a promise that resolves after a specified delay.
107
- * @param ms - The delay in milliseconds.
108
- * @returns A promise that resolves after the delay.
109
- */
110
- export const delay = (ms: number): Promise<void> => {
111
- return new Promise((resolve) => setTimeout(resolve, ms));
112
- };
113
-
114
- /**
115
- * Pauses execution for a specified duration in async contexts.
116
- * @param ms - The duration to sleep in milliseconds.
117
- * @returns A promise that resolves after the sleep duration.
118
- */
119
- export const sleep = (ms: number): Promise<void> => delay(ms);
120
-
121
- /**
122
- * Creates a promise that rejects if the given promise doesn't resolve within the specified timeout.
123
- * @param promise - The promise to apply a timeout to.
124
- * @param ms - The timeout duration in milliseconds.
125
- * @param message - Optional error message for the timeout error.
126
- * @returns A promise that resolves with the original promise's value or rejects on timeout.
127
- */
128
- export const timeout = <T>(
129
- promise: Promise<T>,
130
- ms: number,
131
- message = `Operation timed out after ${ms}ms`,
132
- ): Promise<T> => {
133
- return Promise.race([
134
- promise,
135
- new Promise<T>((_, reject) => setTimeout(() => reject(new Error(message)), ms)),
136
- ]);
137
- };
138
-
139
- /**
140
- * A no-operation function that does nothing when called.
141
- */
142
- export const noop = (): void => {};
143
-
144
- /**
145
- * Retries a function until it succeeds or reaches the maximum number of attempts.
146
- * @param fn - The async function to retry.
147
- * @param options - Configuration options for retry behavior.
148
- * @returns A promise that resolves with the function result or rejects if all attempts fail.
149
- */
150
- export const retry = async <T>(
151
- fn: () => Promise<T>,
152
- options: {
153
- /** Maximum number of attempts. Default: 3 */
154
- maxAttempts?: number;
155
- /** Delay in milliseconds between attempts. Default: 1000 */
156
- delay?: number;
157
- /** Whether to use exponential backoff. Default: false */
158
- exponential?: boolean;
159
- } = {},
160
- ): Promise<T> => {
161
- const { maxAttempts = 3, delay = 1000, exponential = false } = options;
162
-
163
- let lastError: Error | undefined;
164
-
165
- for (let attempt = 1; attempt <= maxAttempts; attempt++) {
166
- try {
167
- return await fn();
168
- } catch (error) {
169
- lastError = error instanceof Error ? error : new Error(String(error));
170
-
171
- if (attempt < maxAttempts) {
172
- const waitTime = exponential ? delay * Math.pow(2, attempt - 1) : delay;
173
- await new Promise((resolve) => setTimeout(resolve, waitTime));
174
- }
175
- }
176
- }
177
-
178
- throw lastError || new Error('Retry failed');
179
- };
@@ -1,14 +0,0 @@
1
- /**
2
- * A type that represents a value that can be either a single instance of type T or an array of type T.
3
- */
4
- export type SingleOrArray<T> = T | T[];
5
-
6
- /**
7
- * A type that represents a value that can be either a direct value of type T or a Promise that resolves to type T.
8
- */
9
- export type MaybePromise<T> = T | Promise<T>;
10
-
11
- /**
12
- * A type that represents a value that can be null or undefined.
13
- */
14
- export type Nullable<T> = T | null | undefined;
package/tsconfig.json DELETED
@@ -1,26 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "useDefineForClassFields": true,
5
- "module": "ESNext",
6
- "lib": ["ES2022", "DOM", "DOM.Iterable"],
7
- "types": ["vite/client"],
8
- "skipLibCheck": true,
9
-
10
- /* Bundler mode */
11
- "moduleResolution": "bundler",
12
- "allowImportingTsExtensions": true,
13
- "verbatimModuleSyntax": true,
14
- "moduleDetection": "force",
15
- "noEmit": true,
16
-
17
- /* Linting */
18
- "strict": true,
19
- "noUnusedLocals": true,
20
- "noUnusedParameters": true,
21
- "erasableSyntaxOnly": true,
22
- "noFallthroughCasesInSwitch": true,
23
- "noUncheckedSideEffectImports": true
24
- },
25
- "include": ["src"]
26
- }
package/vite.config.ts DELETED
@@ -1,21 +0,0 @@
1
- import { resolve } from 'path';
2
- import { fileURLToPath } from 'url';
3
- import { defineConfig } from 'vite';
4
- import dts from 'vite-plugin-dts';
5
-
6
- const name = 'MKDomUtils';
7
- const fileName = name.toLowerCase();
8
- const __dirname = fileURLToPath(new URL('.', import.meta.url));
9
-
10
- export default defineConfig({
11
- build: {
12
- lib: {
13
- name,
14
- entry: resolve(__dirname, 'src/index.ts'),
15
- formats: ['es', 'cjs', 'umd', 'iife'],
16
- fileName: (format) => `${fileName}${format.includes('es') ? '' : `.${format}`}.js`,
17
- },
18
- rollupOptions: { output: { assetFileNames: () => `${fileName}[extname]` } },
19
- },
20
- plugins: [dts()],
21
- });
package/vitest.config.ts DELETED
@@ -1,12 +0,0 @@
1
- import { defineConfig } from 'vitest/config';
2
-
3
- export default defineConfig({
4
- test: {
5
- include: ['tests/**/*.test.ts'],
6
- environment: 'jsdom',
7
- setupFiles: ['./tests/setup.ts'],
8
- coverage: {
9
- provider: 'v8',
10
- },
11
- },
12
- });