@sohanemon/utils 6.2.7 → 6.2.10
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/index.d.mts +665 -0
- package/dist/index.mjs +664 -0
- package/package.json +42 -18
- package/dist/components/html-injector.d.ts +0 -50
- package/dist/components/html-injector.js +0 -108
- package/dist/components/index.d.ts +0 -5
- package/dist/components/index.js +0 -7
- package/dist/components/media-wrapper.d.ts +0 -10
- package/dist/components/media-wrapper.js +0 -14
- package/dist/components/responsive-indicator.d.ts +0 -2
- package/dist/components/responsive-indicator.js +0 -68
- package/dist/components/scrollable-marker.d.ts +0 -1
- package/dist/components/scrollable-marker.js +0 -56
- package/dist/functions/cookie.d.ts +0 -6
- package/dist/functions/cookie.js +0 -22
- package/dist/functions/deepmerge.d.ts +0 -59
- package/dist/functions/deepmerge.js +0 -116
- package/dist/functions/hydrate.d.ts +0 -15
- package/dist/functions/hydrate.js +0 -31
- package/dist/functions/index.d.ts +0 -8
- package/dist/functions/index.js +0 -8
- package/dist/functions/object.d.ts +0 -93
- package/dist/functions/object.js +0 -67
- package/dist/functions/poll.d.ts +0 -38
- package/dist/functions/poll.js +0 -69
- package/dist/functions/schedule.d.ts +0 -12
- package/dist/functions/schedule.js +0 -29
- package/dist/functions/shield.d.ts +0 -18
- package/dist/functions/shield.js +0 -15
- package/dist/functions/utils.d.ts +0 -243
- package/dist/functions/utils.js +0 -439
- package/dist/hooks/action.d.ts +0 -20
- package/dist/hooks/action.js +0 -84
- package/dist/hooks/async.d.ts +0 -30
- package/dist/hooks/async.js +0 -82
- package/dist/hooks/index.d.ts +0 -192
- package/dist/hooks/index.js +0 -533
- package/dist/hooks/schedule.d.ts +0 -36
- package/dist/hooks/schedule.js +0 -68
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -2
- package/dist/types/gates.d.ts +0 -133
- package/dist/types/gates.js +0 -1
- package/dist/types/guards.d.ts +0 -6
- package/dist/types/guards.js +0 -29
- package/dist/types/index.d.ts +0 -3
- package/dist/types/index.js +0 -3
- package/dist/types/utilities.d.ts +0 -62
- package/dist/types/utilities.js +0 -1
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,665 @@
|
|
|
1
|
+
import { ClassValue } from "clsx";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/functions/cookie.d.ts
|
|
5
|
+
declare const setClientSideCookie: (name: string, value: string, days?: number, path?: string) => void;
|
|
6
|
+
declare const deleteClientSideCookie: (name: string, path?: string) => void;
|
|
7
|
+
declare const hasClientSideCookie: (name: string) => boolean;
|
|
8
|
+
declare const getClientSideCookie: (name: string) => {
|
|
9
|
+
value: string | undefined;
|
|
10
|
+
};
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/functions/deepmerge.d.ts
|
|
13
|
+
type TAllKeys<T> = T extends any ? keyof T : never;
|
|
14
|
+
type TIndexValue<T, K$1 extends PropertyKey, D = never> = T extends any ? K$1 extends keyof T ? T[K$1] : D : never;
|
|
15
|
+
type TPartialKeys<T, K$1 extends keyof T> = Omit<T, K$1> & Partial<Pick<T, K$1>> extends infer O ? { [P in keyof O]: O[P] } : never;
|
|
16
|
+
type TFunction = (...a: any[]) => any;
|
|
17
|
+
type TPrimitives = string | number | boolean | bigint | symbol | Date | TFunction;
|
|
18
|
+
type TMerged<T> = [T] extends [Array<any>] ? { [K in keyof T]: TMerged<T[K]> } : [T] extends [TPrimitives] ? T : [T] extends [object] ? TPartialKeys<{ [K in TAllKeys<T>]: TMerged<TIndexValue<T, K>> }, never> : T;
|
|
19
|
+
/**
|
|
20
|
+
* Deeply merges multiple objects, with later sources taking precedence.
|
|
21
|
+
* Handles nested objects, arrays, and special object types with circular reference detection.
|
|
22
|
+
*
|
|
23
|
+
* Features:
|
|
24
|
+
* - Deep merging of nested objects
|
|
25
|
+
* - Configurable array merging strategies
|
|
26
|
+
* - Circular reference detection and handling
|
|
27
|
+
* - Support for symbols and special objects (Date, RegExp, etc.)
|
|
28
|
+
* - Type-safe with improved generics
|
|
29
|
+
* - Optional cloning to avoid mutation
|
|
30
|
+
* - Custom merge functions for specific keys
|
|
31
|
+
*
|
|
32
|
+
* @template T - The target object type
|
|
33
|
+
* @param target - The target object to merge into
|
|
34
|
+
* @param sources - Source objects to merge from (can have additional properties)
|
|
35
|
+
* @param options - Configuration options
|
|
36
|
+
* @param options.arrayMerge - How to merge arrays: 'replace' (default), 'concat', or 'merge'
|
|
37
|
+
* @param options.clone - Whether to clone the target (default: true)
|
|
38
|
+
* @param options.customMerge - Custom merge function for specific keys
|
|
39
|
+
* @param options.maxDepth - Maximum recursion depth (default: 100)
|
|
40
|
+
* @returns The merged object with proper typing
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* // Basic merge
|
|
44
|
+
* deepmerge({ a: 1 }, { b: 2 }) // { a: 1, b: 2 }
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* // Nested merge
|
|
48
|
+
* deepmerge({ a: { x: 1 } }, { a: { y: 2 } }) // { a: { x: 1, y: 2 } }
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* // Array concat
|
|
52
|
+
* deepmerge({ arr: [1] }, { arr: [2] }, { arrayMerge: 'concat' }) // { arr: [1, 2] }
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* // Sources with extra properties
|
|
56
|
+
* deepmerge({ a: 1 }, { b: 2, c: 3 }) // { a: 1, b: 2, c: 3 }
|
|
57
|
+
*/
|
|
58
|
+
declare function deepmerge<T extends Record<string, any>, S$1 extends Record<string, any>[]>(target: T, ...sources: S$1): TMerged<T | S$1[number]>;
|
|
59
|
+
declare function deepmerge<T extends Record<string, any>, S$1 extends Record<string, any>[]>(target: T, sources: S$1, options?: {
|
|
60
|
+
arrayMerge?: 'replace' | 'concat' | 'merge' | ((target: any[], source: any[]) => any[]);
|
|
61
|
+
clone?: boolean;
|
|
62
|
+
customMerge?: (key: string | symbol, targetValue: any, sourceValue: any) => any;
|
|
63
|
+
maxDepth?: number;
|
|
64
|
+
}): TMerged<T | S$1[number]>;
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/functions/hydrate.d.ts
|
|
67
|
+
type Hydrate<T> = T extends null ? undefined : T extends (infer U)[] ? Hydrate<U>[] : T extends object ? { [K in keyof T]: Hydrate<T[K]> } : T;
|
|
68
|
+
/**
|
|
69
|
+
* Converts all `null` values to `undefined` in the data structure recursively.
|
|
70
|
+
*
|
|
71
|
+
* @param data - Any input data (object, array, primitive)
|
|
72
|
+
* @returns Same type as input, but with all nulls replaced by undefined
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* hydrate({ a: null, b: 'test' }) // { a: undefined, b: 'test' }
|
|
76
|
+
* hydrate([null, 1, { c: null }]) // [undefined, 1, { c: undefined }]
|
|
77
|
+
*/
|
|
78
|
+
declare function hydrate<T>(data: T): Hydrate<T>;
|
|
79
|
+
//#endregion
|
|
80
|
+
//#region src/functions/object.d.ts
|
|
81
|
+
/**
|
|
82
|
+
* Type representing a path split into segments
|
|
83
|
+
* @template S - The original path string type
|
|
84
|
+
*/
|
|
85
|
+
type SplitPath<S$1 extends string> = S$1 extends `${infer First}.${infer Rest}` ? [First, ...SplitPath<Rest>] : [S$1];
|
|
86
|
+
/**
|
|
87
|
+
* Recursive type to resolve nested object types based on path
|
|
88
|
+
* @template T - Current object type
|
|
89
|
+
* @template K - Array of path segments
|
|
90
|
+
*/
|
|
91
|
+
type GetValue<T, K$1 extends Array<string | number>> = K$1 extends [infer First, ...infer Rest] ? First extends keyof T ? GetValue<T[First], Rest extends Array<string | number> ? Rest : []> : First extends `${number}` ? T extends any[] ? GetValue<T[number], Rest extends Array<string | number> ? Rest : []> : undefined : undefined : T;
|
|
92
|
+
/**
|
|
93
|
+
* Get a nested value from an object using array path segments
|
|
94
|
+
* @template T - Object type
|
|
95
|
+
* @template K - Path segments array type
|
|
96
|
+
* @template D - Default value type
|
|
97
|
+
* @param obj - Source object
|
|
98
|
+
* @param path - Array of path segments
|
|
99
|
+
* @param defaultValue - Fallback value if path not found
|
|
100
|
+
* @returns Value at path or default value
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* getObjectValue({a: [{b: 1}]}, ['a', 0, 'b']) // 1
|
|
104
|
+
*/
|
|
105
|
+
declare function getObjectValue<T, K$1 extends Array<string | number>, D>(obj: T, path: K$1, defaultValue: D): Exclude<GetValue<T, K$1>, undefined> | D;
|
|
106
|
+
/**
|
|
107
|
+
* Get a nested value from an object using array path segments
|
|
108
|
+
* @template T - Object type
|
|
109
|
+
* @template K - Path segments array type
|
|
110
|
+
* @param obj - Source object
|
|
111
|
+
* @param path - Array of path segments
|
|
112
|
+
* @returns Value at path or undefined
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* getObjectValue({a: [{b: 1}]}, ['a', 0, 'b']) // 1
|
|
116
|
+
*/
|
|
117
|
+
declare function getObjectValue<T, K$1 extends Array<string | number>>(obj: T, path: K$1): GetValue<T, K$1> | undefined;
|
|
118
|
+
/**
|
|
119
|
+
* Get a nested value from an object using dot notation path
|
|
120
|
+
* @template T - Object type
|
|
121
|
+
* @template S - Path string literal type
|
|
122
|
+
* @template D - Default value type
|
|
123
|
+
* @param obj - Source object
|
|
124
|
+
* @param path - Dot-separated path string
|
|
125
|
+
* @param defaultValue - Fallback value if path not found
|
|
126
|
+
* @returns Value at path or default value
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* getObjectValue({a: [{b: 1}]}, 'a.0.b', 2) // 1
|
|
130
|
+
*/
|
|
131
|
+
declare function getObjectValue<T, S$1 extends string, D>(obj: T, path: S$1, defaultValue: D): Exclude<GetValue<T, SplitPath<S$1>>, undefined> | D;
|
|
132
|
+
/**
|
|
133
|
+
* Get a nested value from an object using dot notation path
|
|
134
|
+
* @template T - Object type
|
|
135
|
+
* @template S - Path string literal type
|
|
136
|
+
* @param obj - Source object
|
|
137
|
+
* @param path - Dot-separated path string
|
|
138
|
+
* @returns Value at path or undefined
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* getObjectValue({a: [{b: 1}]}, 'a.0.b') // 1
|
|
142
|
+
*/
|
|
143
|
+
declare function getObjectValue<T, S$1 extends string>(obj: T, path: S$1): GetValue<T, SplitPath<S$1>> | undefined;
|
|
144
|
+
/**
|
|
145
|
+
* Extend an object or function with additional properties while
|
|
146
|
+
* preserving the original type information.
|
|
147
|
+
*
|
|
148
|
+
* Works with both plain objects and callable functions since
|
|
149
|
+
* functions in JavaScript are objects too.
|
|
150
|
+
*
|
|
151
|
+
* @template T The base object or function type
|
|
152
|
+
* @template P The additional properties type
|
|
153
|
+
*
|
|
154
|
+
* @param base - The object or function to extend
|
|
155
|
+
* @param props - An object containing properties to attach
|
|
156
|
+
*
|
|
157
|
+
* @returns The same object/function, augmented with the given properties
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* // Extend an object
|
|
161
|
+
* const obj = extendProps({ a: 1 }, { b: "hello" });
|
|
162
|
+
* // obj has { a: number; b: string }
|
|
163
|
+
*
|
|
164
|
+
* // Extend a function
|
|
165
|
+
* const fn = (x: number) => x * 2;
|
|
166
|
+
* const enhanced = extendProps(fn, { name: "doubler" });
|
|
167
|
+
* // enhanced is callable and also has { name: string }
|
|
168
|
+
*/
|
|
169
|
+
declare function extendProps<T extends object, P$1 extends object>(base: T, props: P$1): T & P$1;
|
|
170
|
+
//#endregion
|
|
171
|
+
//#region src/functions/poll.d.ts
|
|
172
|
+
/**
|
|
173
|
+
* Repeatedly polls an async `cond` function UNTIL it returns a TRUTHY value,
|
|
174
|
+
* or until the operation times out or is aborted.
|
|
175
|
+
*
|
|
176
|
+
* Designed for waiting on async jobs, external state, or delayed availability.
|
|
177
|
+
*
|
|
178
|
+
* @template T The type of the successful result.
|
|
179
|
+
*
|
|
180
|
+
* @param cond
|
|
181
|
+
* A function returning a Promise that resolves to:
|
|
182
|
+
* - a truthy value `T` → stop polling and return it
|
|
183
|
+
* - falsy/null/undefined → continue polling
|
|
184
|
+
*
|
|
185
|
+
* @param options
|
|
186
|
+
* Configuration options:
|
|
187
|
+
* - `interval` (number) — Time between polls in ms (default: 5000 ms)
|
|
188
|
+
* - `timeout` (number) — Max total duration before failing (default: 5 min)
|
|
189
|
+
* - `jitter` (boolean) — Add small random offset (±10%) to intervals to avoid sync bursts (default: true)
|
|
190
|
+
* - `signal` (AbortSignal) — Optional abort signal to cancel polling
|
|
191
|
+
*
|
|
192
|
+
* @returns
|
|
193
|
+
* Resolves with the truthy value `T` when successful.
|
|
194
|
+
* Throws `AbortError` if aborted
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```ts
|
|
198
|
+
* const job = await poll(async () => {
|
|
199
|
+
* const status = await getJobStatus();
|
|
200
|
+
* return status === 'done' ? status : null;
|
|
201
|
+
* }, { interval: 3000, timeout: 60000 });
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
declare function poll<T>(cond: () => Promise<T | null | false | undefined>, {
|
|
205
|
+
interval,
|
|
206
|
+
timeout,
|
|
207
|
+
jitter,
|
|
208
|
+
signal
|
|
209
|
+
}?: Partial<{
|
|
210
|
+
interval: number;
|
|
211
|
+
timeout: number;
|
|
212
|
+
signal: AbortSignal;
|
|
213
|
+
jitter: boolean;
|
|
214
|
+
}>): Promise<T>;
|
|
215
|
+
//#endregion
|
|
216
|
+
//#region src/functions/schedule.d.ts
|
|
217
|
+
type Task = () => Promise<void> | void;
|
|
218
|
+
interface ScheduleOpts {
|
|
219
|
+
retry?: number;
|
|
220
|
+
delay?: number;
|
|
221
|
+
timeout?: number;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Runs a function asynchronously in the background.
|
|
225
|
+
* Returns immediately, retries on failure if configured.
|
|
226
|
+
* Logs total time taken.
|
|
227
|
+
*/
|
|
228
|
+
declare function schedule(task: Task, options?: ScheduleOpts): void;
|
|
229
|
+
//#endregion
|
|
230
|
+
//#region src/functions/shield.d.ts
|
|
231
|
+
/**
|
|
232
|
+
* A helper to run sync or async operations safely without try/catch.
|
|
233
|
+
*
|
|
234
|
+
* Returns a tuple `[error, data]`:
|
|
235
|
+
* - `error`: the thrown error (if any), otherwise `null`
|
|
236
|
+
* - `data`: the resolved value (if successful), otherwise `null`
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```ts
|
|
240
|
+
* const [err, value] = shield(() => riskySync());
|
|
241
|
+
* if (err) console.error(err);
|
|
242
|
+
*
|
|
243
|
+
* const [asyncErr, result] = await shield(fetchData());
|
|
244
|
+
* if (asyncErr) throw asyncErr;
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
declare function shield<T, E = Error>(operation: Promise<T>): Promise<[E | null, T | null]>;
|
|
248
|
+
declare function shield<T, E = Error>(operation: () => T): [E | null, T | null];
|
|
249
|
+
//#endregion
|
|
250
|
+
//#region src/functions/utils.d.ts
|
|
251
|
+
/**
|
|
252
|
+
* Utility to merge class names with Tailwind CSS and additional custom merging logic.
|
|
253
|
+
*
|
|
254
|
+
* @param {...ClassValue[]} inputs - Class names to merge.
|
|
255
|
+
* @returns {string} - A string of merged class names.
|
|
256
|
+
*/
|
|
257
|
+
declare function cn(...inputs: ClassValue[]): string;
|
|
258
|
+
/**
|
|
259
|
+
* @deprecated Use isLinkActive instead.
|
|
260
|
+
*
|
|
261
|
+
* Determines if a navigation link is active based on the current path.
|
|
262
|
+
*
|
|
263
|
+
* @param href - The target URL.
|
|
264
|
+
* @param path - The current browser path.
|
|
265
|
+
* @returns - True if the navigation is active, false otherwise.
|
|
266
|
+
*/
|
|
267
|
+
declare function isNavActive(href: string, path: string): boolean;
|
|
268
|
+
/**
|
|
269
|
+
* Checks if a link is active, considering optional localization prefixes.
|
|
270
|
+
*
|
|
271
|
+
* @param {Object} params - Parameters object.
|
|
272
|
+
* @param {string} params.path - The target path of the link.
|
|
273
|
+
* @param {string} params.currentPath - The current browser path.
|
|
274
|
+
* @param {string[]} [params.locales=['en', 'es', 'de', 'zh', 'bn', 'fr', 'it', 'nl']] - Supported locale prefixes.
|
|
275
|
+
* @returns {boolean} - True if the link is active, false otherwise.
|
|
276
|
+
*/
|
|
277
|
+
declare function isLinkActive({
|
|
278
|
+
path,
|
|
279
|
+
currentPath,
|
|
280
|
+
locales,
|
|
281
|
+
exact
|
|
282
|
+
}: {
|
|
283
|
+
path: string;
|
|
284
|
+
currentPath: string;
|
|
285
|
+
locales?: string[];
|
|
286
|
+
exact?: boolean;
|
|
287
|
+
}): boolean;
|
|
288
|
+
/**
|
|
289
|
+
* Cleans a file path by removing the `/public/` prefix if present.
|
|
290
|
+
*
|
|
291
|
+
* @param src - The source path to clean.
|
|
292
|
+
* @returns - The cleaned path.
|
|
293
|
+
*/
|
|
294
|
+
declare function cleanSrc(src: string): string;
|
|
295
|
+
/**
|
|
296
|
+
* Smoothly scrolls to the top or bottom of a specified container.
|
|
297
|
+
*
|
|
298
|
+
* @param containerSelector - The CSS selector or React ref for the container.
|
|
299
|
+
* @param to - Specifies whether to scroll to the top or bottom.
|
|
300
|
+
*/
|
|
301
|
+
declare const scrollTo: (containerSelector: string | React.RefObject<HTMLDivElement>, to: "top" | "bottom") => void;
|
|
302
|
+
/**
|
|
303
|
+
* Copies a given string to the clipboard.
|
|
304
|
+
*
|
|
305
|
+
* @param value - The value to copy to the clipboard.
|
|
306
|
+
* @param [onSuccess=() => {}] - Optional callback executed after successful copy.
|
|
307
|
+
*/
|
|
308
|
+
declare const copyToClipboard: (value: string, onSuccess?: () => void) => void;
|
|
309
|
+
/**
|
|
310
|
+
* Converts camelCase, PascalCase, kebab-case, snake_case into normal case.
|
|
311
|
+
*
|
|
312
|
+
* @param inputString - The string need to be converted into normal case
|
|
313
|
+
* @returns - Normal Case
|
|
314
|
+
*/
|
|
315
|
+
declare function convertToNormalCase(inputString: string): string;
|
|
316
|
+
/**
|
|
317
|
+
* Converts a string to a URL-friendly slug by trimming, converting to lowercase,
|
|
318
|
+
* replacing diacritics, removing invalid characters, and replacing spaces with hyphens.
|
|
319
|
+
* @param {string} [str] - The input string to convert.
|
|
320
|
+
* @returns {string} The generated slug.
|
|
321
|
+
* @example
|
|
322
|
+
* convertToSlug("Hello World!"); // "hello-world"
|
|
323
|
+
* convertToSlug("Déjà Vu"); // "deja-vu"
|
|
324
|
+
*/
|
|
325
|
+
declare const convertToSlug: (str: string) => string;
|
|
326
|
+
/**
|
|
327
|
+
* Checks if the code is running in a server-side environment.
|
|
328
|
+
*
|
|
329
|
+
* @returns - True if the code is executed in SSR (Server-Side Rendering) context, false otherwise
|
|
330
|
+
*/
|
|
331
|
+
declare const isSSR: boolean;
|
|
332
|
+
/**
|
|
333
|
+
* Converts an SVG string to a Base64-encoded string.
|
|
334
|
+
*
|
|
335
|
+
* @param str - The SVG string to encode
|
|
336
|
+
* @returns - Base64-encoded string representation of the SVG
|
|
337
|
+
*/
|
|
338
|
+
declare const svgToBase64: (str: string) => string;
|
|
339
|
+
/**
|
|
340
|
+
* Pauses execution for the specified time.
|
|
341
|
+
*
|
|
342
|
+
* `signal` allows cancelling the sleep via AbortSignal.
|
|
343
|
+
*
|
|
344
|
+
* @param time - Time in milliseconds to sleep (default is 1000ms)
|
|
345
|
+
* @param signal - Optional AbortSignal to cancel the sleep early
|
|
346
|
+
* @returns - A Promise that resolves after the specified time or when aborted
|
|
347
|
+
*/
|
|
348
|
+
declare const sleep: (time?: number, signal?: AbortSignal) => Promise<void>;
|
|
349
|
+
type DebouncedFunction<F$1 extends (...args: any[]) => any> = {
|
|
350
|
+
(...args: Parameters<F$1>): ReturnType<F$1> | undefined;
|
|
351
|
+
readonly isPending: boolean;
|
|
352
|
+
};
|
|
353
|
+
/**
|
|
354
|
+
* Creates a debounced function that delays invoking the provided function until
|
|
355
|
+
* after the specified `wait` time has elapsed since the last invocation.
|
|
356
|
+
*
|
|
357
|
+
* If the `immediate` option is set to `true`, the function will be invoked immediately
|
|
358
|
+
* on the leading edge of the wait interval. Subsequent calls during the wait interval
|
|
359
|
+
* will reset the timer but not invoke the function until the interval elapses again.
|
|
360
|
+
*
|
|
361
|
+
* The returned function includes the `isPending` property to check if the debounce
|
|
362
|
+
* timer is currently active.
|
|
363
|
+
*
|
|
364
|
+
* @typeParam F - The type of the function to debounce.
|
|
365
|
+
*
|
|
366
|
+
* @param function_ - The function to debounce.
|
|
367
|
+
* @param wait - The number of milliseconds to delay (default is 100ms).
|
|
368
|
+
* @param options - An optional object with the following properties:
|
|
369
|
+
* - `immediate` (boolean): If `true`, invokes the function on the leading edge
|
|
370
|
+
* of the wait interval instead of the trailing edge.
|
|
371
|
+
*
|
|
372
|
+
* @returns A debounced version of the provided function, enhanced with the `isPending` property.
|
|
373
|
+
*
|
|
374
|
+
* @throws {TypeError} If the first parameter is not a function.
|
|
375
|
+
* @throws {RangeError} If the `wait` parameter is negative.
|
|
376
|
+
*
|
|
377
|
+
* @example
|
|
378
|
+
* const log = debounce((message: string) => console.log(message), 200);
|
|
379
|
+
* log('Hello'); // Logs "Hello" after 200ms if no other call is made.
|
|
380
|
+
* console.log(log.isPending); // true if the timer is active.
|
|
381
|
+
*/
|
|
382
|
+
declare function debounce<F$1 extends (...args: any[]) => any>(function_: F$1, wait?: number, options?: {
|
|
383
|
+
immediate: boolean;
|
|
384
|
+
}): DebouncedFunction<F$1>;
|
|
385
|
+
type ThrottledFunction<F$1 extends (...args: any[]) => any> = {
|
|
386
|
+
(...args: Parameters<F$1>): ReturnType<F$1> | undefined;
|
|
387
|
+
readonly isPending: boolean;
|
|
388
|
+
};
|
|
389
|
+
/**
|
|
390
|
+
* Creates a throttled function that invokes the provided function at most once
|
|
391
|
+
* every `wait` milliseconds.
|
|
392
|
+
*
|
|
393
|
+
* If the `leading` option is set to `true`, the function will be invoked immediately
|
|
394
|
+
* on the leading edge of the throttle interval. If the `trailing` option is set to `true`,
|
|
395
|
+
* the function will also be invoked at the end of the throttle interval if additional
|
|
396
|
+
* calls were made during the interval.
|
|
397
|
+
*
|
|
398
|
+
* The returned function includes the `isPending` property to check if the throttle
|
|
399
|
+
* timer is currently active.
|
|
400
|
+
*
|
|
401
|
+
* @typeParam F - The type of the function to throttle.
|
|
402
|
+
*
|
|
403
|
+
* @param function_ - The function to throttle.
|
|
404
|
+
* @param wait - The number of milliseconds to wait between invocations (default is 100ms).
|
|
405
|
+
* @param options - An optional object with the following properties:
|
|
406
|
+
* - `leading` (boolean): If `true`, invokes the function on the leading edge of the interval.
|
|
407
|
+
* - `trailing` (boolean): If `true`, invokes the function on the trailing edge of the interval.
|
|
408
|
+
*
|
|
409
|
+
* @returns A throttled version of the provided function, enhanced with the `isPending` property.
|
|
410
|
+
*
|
|
411
|
+
* @throws {TypeError} If the first parameter is not a function.
|
|
412
|
+
* @throws {RangeError} If the `wait` parameter is negative.
|
|
413
|
+
*
|
|
414
|
+
* @example
|
|
415
|
+
* const log = throttle((message: string) => console.log(message), 200);
|
|
416
|
+
* log('Hello'); // Logs "Hello" immediately if leading is true.
|
|
417
|
+
* console.log(log.isPending); // true if the timer is active.
|
|
418
|
+
*/
|
|
419
|
+
declare function throttle<F$1 extends (...args: any[]) => any>(function_: F$1, wait?: number, options?: {
|
|
420
|
+
leading?: boolean;
|
|
421
|
+
trailing?: boolean;
|
|
422
|
+
}): ThrottledFunction<F$1>;
|
|
423
|
+
/**
|
|
424
|
+
* Formats a string by replacing each '%s' placeholder with the corresponding argument.
|
|
425
|
+
* This function mimics the basic behavior of C's printf for %s substitution.
|
|
426
|
+
*
|
|
427
|
+
* It supports both calls like `printf(format, ...args)` and `printf(format, argsArray)`.
|
|
428
|
+
*
|
|
429
|
+
* @param format - The format string containing '%s' placeholders.
|
|
430
|
+
* @param args - The values to substitute into the placeholders, either as separate arguments or as a single array.
|
|
431
|
+
* @returns The formatted string with all '%s' replaced by the provided arguments.
|
|
432
|
+
*
|
|
433
|
+
* @example
|
|
434
|
+
* ```ts
|
|
435
|
+
* const message = printf("%s love %s", "I", "Bangladesh");
|
|
436
|
+
* // message === "I love Bangladesh"
|
|
437
|
+
*
|
|
438
|
+
* const arr = ["I", "Bangladesh"];
|
|
439
|
+
* const message2 = printf("%s love %s", arr);
|
|
440
|
+
* // message2 === "I love Bangladesh"
|
|
441
|
+
*
|
|
442
|
+
* // If there are too few arguments:
|
|
443
|
+
* const incomplete = printf("Bangladesh is %s %s", "beautiful");
|
|
444
|
+
* // incomplete === "Bangladesh is beautiful"
|
|
445
|
+
* ```
|
|
446
|
+
*/
|
|
447
|
+
declare function printf(format: string, ...args: unknown[]): string;
|
|
448
|
+
/**
|
|
449
|
+
* Merges multiple refs into a single ref callback.
|
|
450
|
+
*
|
|
451
|
+
* @param refs - An array of refs to merge.
|
|
452
|
+
*
|
|
453
|
+
* @returns - A function that updates the merged ref with the provided value.
|
|
454
|
+
*/
|
|
455
|
+
type MergeRefs = <T>(...refs: Array<React.Ref<T> | undefined>) => React.RefCallback<T>;
|
|
456
|
+
declare const mergeRefs: MergeRefs;
|
|
457
|
+
/**
|
|
458
|
+
* Navigates to the specified client-side hash without ssr.
|
|
459
|
+
* use `scroll-margin-top` with css to add margins
|
|
460
|
+
*
|
|
461
|
+
* @param id - The ID of the element without # to navigate to.
|
|
462
|
+
*
|
|
463
|
+
* @example goToClientSideHash('my-element');
|
|
464
|
+
*/
|
|
465
|
+
declare function goToClientSideHash(id: string, opts?: ScrollIntoViewOptions): void;
|
|
466
|
+
/**
|
|
467
|
+
* Escapes a string for use in a regular expression.
|
|
468
|
+
*
|
|
469
|
+
* @param str - The string to escape
|
|
470
|
+
* @returns - The escaped string
|
|
471
|
+
*
|
|
472
|
+
* @example
|
|
473
|
+
* const escapedString = escapeRegExp('Hello, world!');
|
|
474
|
+
* // escapedString === 'Hello\\, world!'
|
|
475
|
+
*/
|
|
476
|
+
declare function escapeRegExp(str: string): string;
|
|
477
|
+
/**
|
|
478
|
+
* Normalizes a string by:
|
|
479
|
+
* - Applying Unicode normalization (NFC)
|
|
480
|
+
* - Optionally removing diacritic marks (accents)
|
|
481
|
+
* - Optionally trimming leading/trailing non-alphanumeric characters
|
|
482
|
+
* - Optionally converting to lowercase
|
|
483
|
+
*
|
|
484
|
+
* @param str - The string to normalize
|
|
485
|
+
* @param options - Normalization options
|
|
486
|
+
* @param options.lowercase - Whether to convert the result to lowercase (default: true)
|
|
487
|
+
* @param options.removeAccents - Whether to remove diacritic marks like accents (default: true)
|
|
488
|
+
* @param options.removeNonAlphanumeric - Whether to trim non-alphanumeric characters from the edges (default: true)
|
|
489
|
+
* @returns The normalized string
|
|
490
|
+
*/
|
|
491
|
+
declare function normalizeText(str?: string | null, options?: {
|
|
492
|
+
lowercase?: boolean;
|
|
493
|
+
removeAccents?: boolean;
|
|
494
|
+
removeNonAlphanumeric?: boolean;
|
|
495
|
+
}): string;
|
|
496
|
+
//#endregion
|
|
497
|
+
//#region src/types/utilities.d.ts
|
|
498
|
+
type Keys<T extends object> = keyof T;
|
|
499
|
+
type Values<T extends object> = T[keyof T];
|
|
500
|
+
type DeepPartial<T> = T extends Function ? T : T extends Array<infer U> ? Array<DeepPartial<U>> : T extends object ? { [K in keyof T]?: DeepPartial<T[K]> } : T;
|
|
501
|
+
type SelectivePartial<T, K$1 extends keyof T> = Omit<T, K$1> & Partial<Pick<T, K$1>>;
|
|
502
|
+
type DeepRequired<T> = T extends Function ? T : T extends Array<infer U> ? Array<DeepRequired<U>> : T extends object ? { [K in keyof T]-?: DeepRequired<T[K]> } : T;
|
|
503
|
+
type SelectiveRequired<T, K$1 extends keyof T> = Omit<T, K$1> & Required<Pick<T, K$1>>;
|
|
504
|
+
type Never<T> = { [K in keyof T]: never };
|
|
505
|
+
type Nullable<T> = T extends object ? { [P in keyof T]: Nullable<T[P]> } : T | null;
|
|
506
|
+
type Optional<T> = T extends object ? { [P in keyof T]: Optional<T[P]> } : T | undefined;
|
|
507
|
+
type Nullish<T> = T extends object ? { [P in keyof T]: Nullish<T[P]> } : T | null | undefined;
|
|
508
|
+
type Maybe<T> = T extends object ? { [P in keyof T]?: Nullish<T[P]> } : T | null | undefined;
|
|
509
|
+
type DeepReadonly<T> = T extends Function ? T : T extends Array<infer U> ? ReadonlyArray<DeepReadonly<U>> : T extends object ? { readonly [K in keyof T]: DeepReadonly<T[K]> } : T;
|
|
510
|
+
type Mutable<T> = { -readonly [P in keyof T]: T[P] };
|
|
511
|
+
type KeysOfType<T, U$1> = { [K in keyof T]: T[K] extends U$1 ? K : never }[keyof T];
|
|
512
|
+
type OmitByType<T, U$1> = { [K in keyof T as T[K] extends U$1 ? never : K]: T[K] };
|
|
513
|
+
type RequiredKeys<T, K$1 extends keyof T> = Omit<T, K$1> & Required<Pick<T, K$1>>;
|
|
514
|
+
type Diff<T, U$1> = Omit<T, keyof U$1> & Omit<U$1, keyof T>;
|
|
515
|
+
type Intersection<T extends object, U$1 extends object> = Pick<T, Extract<keyof T, keyof U$1> & Extract<keyof U$1, keyof T>>;
|
|
516
|
+
type Merge<T extends object, U$1 extends object, I = Diff<T, U$1> & Intersection<U$1, T> & Diff<U$1, T>> = Pick<I, keyof I>;
|
|
517
|
+
type Substract<T extends object, U$1 extends object> = Omit<T, keyof U$1>;
|
|
518
|
+
type AllOrNone<T> = T | { [P in keyof T]?: never };
|
|
519
|
+
type OneOf<T> = { [K in keyof T]: Pick<T, K> }[keyof T];
|
|
520
|
+
type TwoOf<T> = { [K in keyof T]: { [L in Exclude<keyof T, K>]: Pick<T, K | L> }[Exclude<keyof T, K>] }[keyof T];
|
|
521
|
+
type Prettify<T> = { [K in keyof T]: T[K] } & {};
|
|
522
|
+
type NestedKeyOf<ObjectType extends object, IgnoreKeys extends string = never> = { [Key in keyof ObjectType & string]: Key extends IgnoreKeys ? never : ObjectType[Key] extends object ? ObjectType[Key] extends Array<any> ? Key : `${Key}` | `${Key}.${NestedKeyOf<ObjectType[Key], IgnoreKeys>}` : `${Key}` }[keyof ObjectType & string];
|
|
523
|
+
type Without<T, U$1> = { [P in Exclude<keyof T, keyof U$1>]?: never };
|
|
524
|
+
//#endregion
|
|
525
|
+
//#region src/types/gates.d.ts
|
|
526
|
+
type BUFFER<T> = T;
|
|
527
|
+
type IMPLIES<T, U$1> = T extends U$1 ? true : false;
|
|
528
|
+
type XOR_Binary<T, U$1> = T | U$1 extends object ? (Without<T, U$1> & U$1) | (Without<U$1, T> & T) : T | U$1;
|
|
529
|
+
type XNOR_Binary<T, U$1> = (T & U$1) | (Without<T, U$1> & Without<U$1, T>);
|
|
530
|
+
/**
|
|
531
|
+
* Computes a type-level AND (all must true) for a tuple of types.
|
|
532
|
+
*
|
|
533
|
+
* Truth table for 3 arguments:
|
|
534
|
+
*
|
|
535
|
+
* A B C = AND
|
|
536
|
+
* 1 1 1 = 1
|
|
537
|
+
* 1 1 0 = 0
|
|
538
|
+
* 1 0 1 = 0
|
|
539
|
+
* 1 0 0 = 0
|
|
540
|
+
* 0 1 1 = 0
|
|
541
|
+
* 0 1 0 = 0
|
|
542
|
+
* 0 0 1 = 0
|
|
543
|
+
* 0 0 0 = 0
|
|
544
|
+
*
|
|
545
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
546
|
+
*/
|
|
547
|
+
type AND<T extends any[]> = T extends [infer F, ...infer R] ? R extends any[] ? F & AND<R> : F : unknown;
|
|
548
|
+
/**
|
|
549
|
+
* Computes a type-level OR (At least one) for a tuple of types.
|
|
550
|
+
*
|
|
551
|
+
* Truth table for 3 arguments:
|
|
552
|
+
*
|
|
553
|
+
* A B C = OR
|
|
554
|
+
* 1 1 1 = 1
|
|
555
|
+
* 1 1 0 = 1
|
|
556
|
+
* 1 0 1 = 1
|
|
557
|
+
* 1 0 0 = 1
|
|
558
|
+
* 0 1 1 = 1
|
|
559
|
+
* 0 1 0 = 1
|
|
560
|
+
* 0 0 1 = 1
|
|
561
|
+
* 0 0 0 = 0
|
|
562
|
+
*
|
|
563
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
564
|
+
*/
|
|
565
|
+
type OR<T extends any[]> = T extends [infer F, ...infer R] ? R extends any[] ? F | OR<R> : F : never;
|
|
566
|
+
/**
|
|
567
|
+
* Computes a type-level XOR (only one/odd) for a tuple of types.
|
|
568
|
+
*
|
|
569
|
+
* Truth table for 3 arguments:
|
|
570
|
+
*
|
|
571
|
+
* A B C = XOR
|
|
572
|
+
* 1 1 1 = 1
|
|
573
|
+
* 1 1 0 = 0
|
|
574
|
+
* 1 0 1 = 0
|
|
575
|
+
* 1 0 0 = 1
|
|
576
|
+
* 0 1 1 = 0
|
|
577
|
+
* 0 1 0 = 1
|
|
578
|
+
* 0 0 1 = 1
|
|
579
|
+
* 0 0 0 = 0
|
|
580
|
+
*
|
|
581
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
582
|
+
*/
|
|
583
|
+
type XOR<T extends any[]> = T extends [infer F, ...infer R] ? R extends [infer S, ...infer Rest] ? XOR<[XOR_Binary<F, S>, ...Rest]> : F : never;
|
|
584
|
+
/**
|
|
585
|
+
* Computes a type-level XNOR (All or None true) for a tuple of types.
|
|
586
|
+
*
|
|
587
|
+
* Truth table for 3 arguments:
|
|
588
|
+
*
|
|
589
|
+
* A B C = XNOR
|
|
590
|
+
* 1 1 1 = 0
|
|
591
|
+
* 1 1 0 = 1
|
|
592
|
+
* 1 0 1 = 1
|
|
593
|
+
* 1 0 0 = 0
|
|
594
|
+
* 0 1 1 = 1
|
|
595
|
+
* 0 1 0 = 0
|
|
596
|
+
* 0 0 1 = 0
|
|
597
|
+
* 0 0 0 = 1
|
|
598
|
+
*
|
|
599
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
600
|
+
*/
|
|
601
|
+
type XNOR<T extends any[]> = T extends [infer F, ...infer R] ? R extends [infer S, ...infer Rest] ? XNOR<[XNOR_Binary<F, S>, ...Rest]> : F : never;
|
|
602
|
+
/**
|
|
603
|
+
* Computes a type-level NOT for a tuple of types.
|
|
604
|
+
*
|
|
605
|
+
* Truth table for 3 arguments:
|
|
606
|
+
*
|
|
607
|
+
* A B C = NOT
|
|
608
|
+
* 1 1 1 = 0
|
|
609
|
+
* 1 1 0 = 0
|
|
610
|
+
* 1 0 1 = 0
|
|
611
|
+
* 1 0 0 = 0
|
|
612
|
+
* 0 1 1 = 0
|
|
613
|
+
* 0 1 0 = 0
|
|
614
|
+
* 0 0 1 = 0
|
|
615
|
+
* 0 0 0 = 1
|
|
616
|
+
*
|
|
617
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
618
|
+
*/
|
|
619
|
+
type NOT<T> = { [P in keyof T]?: never };
|
|
620
|
+
/**
|
|
621
|
+
* Computes a type-level NAND for a tuple of types.
|
|
622
|
+
*
|
|
623
|
+
* Truth table for 3 arguments:
|
|
624
|
+
*
|
|
625
|
+
* A B C = NAND
|
|
626
|
+
* 1 1 1 = 0
|
|
627
|
+
* 1 1 0 = 1
|
|
628
|
+
* 1 0 1 = 1
|
|
629
|
+
* 1 0 0 = 1
|
|
630
|
+
* 0 1 1 = 1
|
|
631
|
+
* 0 1 0 = 1
|
|
632
|
+
* 0 0 1 = 1
|
|
633
|
+
* 0 0 0 = 1
|
|
634
|
+
*
|
|
635
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
636
|
+
*/
|
|
637
|
+
type NAND<T extends any[]> = NOT<AND<T>>;
|
|
638
|
+
/**
|
|
639
|
+
* Computes a type-level NOR for a tuple of types.
|
|
640
|
+
*
|
|
641
|
+
* Truth table for 3 arguments:
|
|
642
|
+
*
|
|
643
|
+
* A B C = NOR
|
|
644
|
+
* 1 1 1 = 0
|
|
645
|
+
* 1 1 0 = 0
|
|
646
|
+
* 1 0 1 = 0
|
|
647
|
+
* 1 0 0 = 0
|
|
648
|
+
* 0 1 1 = 0
|
|
649
|
+
* 0 1 0 = 0
|
|
650
|
+
* 0 0 1 = 0
|
|
651
|
+
* 0 0 0 = 1
|
|
652
|
+
*
|
|
653
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
654
|
+
*/
|
|
655
|
+
type NOR<T extends any[]> = NOT<OR<T>>;
|
|
656
|
+
//#endregion
|
|
657
|
+
//#region src/types/guards.d.ts
|
|
658
|
+
type Primitive = string | number | bigint | boolean | symbol | null | undefined;
|
|
659
|
+
type Falsy = false | '' | 0 | null | undefined;
|
|
660
|
+
declare const isFalsy: (val: unknown) => val is Falsy;
|
|
661
|
+
declare const isNullish: (val: unknown) => val is null | undefined;
|
|
662
|
+
declare const isPrimitive: (val: unknown) => val is Primitive;
|
|
663
|
+
declare function isPlainObject(value: unknown): value is Record<string, any>;
|
|
664
|
+
//#endregion
|
|
665
|
+
export { AND, AllOrNone, BUFFER, DeepPartial, DeepReadonly, DeepRequired, Diff, Falsy, IMPLIES, Intersection, Keys, KeysOfType, Maybe, Merge, MergeRefs, Mutable, NAND, NOR, NOT, NestedKeyOf, Never, Nullable, Nullish, OR, OmitByType, OneOf, Optional, Prettify, Primitive, RequiredKeys, ScheduleOpts, SelectivePartial, SelectiveRequired, Substract, Task, TwoOf, Values, Without, XNOR, XNOR_Binary, XOR, XOR_Binary, cleanSrc, cn, convertToNormalCase, convertToSlug, copyToClipboard, debounce, deepmerge, deleteClientSideCookie, escapeRegExp, extendProps, getClientSideCookie, getObjectValue, goToClientSideHash, hasClientSideCookie, hydrate, isFalsy, isLinkActive, isNavActive, isNullish, isPlainObject, isPrimitive, isSSR, mergeRefs, normalizeText, poll, printf, schedule, scrollTo, setClientSideCookie, shield, sleep, svgToBase64, throttle };
|