@sohanemon/utils 6.4.7 → 7.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.
package/dist/index.d.ts CHANGED
@@ -1,613 +1,295 @@
1
- import { A as deepmerge, C as Task, D as getObjectValue, E as extendProps, M as getClientSideCookie, N as hasClientSideCookie, O as hydrate, P as setClientSideCookie, S as ScheduleOpts, T as poll, _ as scrollTo, a as convertToNormalCase, b as throttle, c as debounce, d as isLinkActive, f as isNavActive, g as printf, h as normalizeText, i as cn, j as deleteClientSideCookie, k as DeepMergeOptions, l as escapeRegExp, m as mergeRefs, n as MergeRefs, o as convertToSlug, p as isSSR, r as cleanSrc, s as copyToClipboard, t as workerize, u as goToClientSideHash, v as sleep, w as schedule, x as shield, y as svgToBase64 } from "./index-CG2oc6F7.js";
1
+ import { ClassValue } from "clsx";
2
+ import * as React from "react";
3
+ export * from "@ts-utilities/core";
2
4
 
3
- //#region src/types/utilities.d.ts
5
+ //#region src/functions/cookie.d.ts
4
6
 
5
7
  /**
6
- * Extracts the keys of an object type as a union type.
8
+ * Sets a client-side cookie with optional expiration and path.
7
9
  *
8
- * @template T - The object type to extract keys from
9
- * @returns A union of all keys in the object type
10
+ * @param name - The name of the cookie
11
+ * @param value - The value to store in the cookie
12
+ * @param days - Optional number of days until the cookie expires
13
+ * @param path - Optional path for the cookie (defaults to '/')
10
14
  *
11
15
  * @example
12
- * ```ts
13
- * type User = { name: string; age: number };
14
- * type UserKeys = Keys<User>; // 'name' | 'age'
15
- * ```
16
- */
17
- type Keys<T extends object> = keyof T;
18
- /**
19
- * Extracts the values of an object type as a union type.
20
- *
21
- * @template T - The object type to extract values from
22
- * @returns A union of all values in the object type
16
+ * // Set a cookie that expires in 7 days
17
+ * setClientSideCookie('userId', '12345', 7);
23
18
  *
24
19
  * @example
25
- * ```ts
26
- * type User = { name: string; age: number };
27
- * type UserValues = Values<User>; // string | number
28
- * ```
20
+ * // Set a session cookie (no expiration)
21
+ * setClientSideCookie('sessionId', 'abc123');
29
22
  */
30
- type Values<T extends object> = T[keyof T];
23
+ declare const setClientSideCookie: (name: string, value: string, days?: number, path?: string) => void;
31
24
  /**
32
- * Makes all properties of an object type optional recursively.
33
- *
34
- * This type traverses through nested objects and arrays, making all properties optional.
35
- * Functions and primitives are left unchanged.
25
+ * Deletes a client-side cookie by setting its expiration to a past date.
36
26
  *
37
- * @template T - The type to make deeply partial
38
- * @returns A type with all properties optional recursively
27
+ * @param name - The name of the cookie to delete
28
+ * @param path - Optional path for the cookie (defaults to '/')
39
29
  *
40
30
  * @example
41
- * ```ts
42
- * type Config = {
43
- * server: { host: string; port: number };
44
- * features: string[];
45
- * };
46
- *
47
- * type PartialConfig = DeepPartial<Config>;
48
- * // {
49
- * // server?: { host?: string; port?: number };
50
- * // features?: string[];
51
- * // }
52
- * ```
31
+ * // Delete a cookie
32
+ * deleteClientSideCookie('userId');
53
33
  */
54
- 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;
34
+ declare const deleteClientSideCookie: (name: string, path?: string) => void;
55
35
  /**
56
- * Makes only specified properties of an object type optional.
36
+ * Checks if a client-side cookie exists.
57
37
  *
58
- * @template T - The base object type
59
- * @template K - The keys to make optional
60
- * @returns An object type with specified properties optional
38
+ * @param name - The name of the cookie to check
39
+ * @returns True if the cookie exists, false otherwise
61
40
  *
62
41
  * @example
63
- * ```ts
64
- * type User = { name: string; age: number; email: string };
65
- * type PartialUser = SelectivePartial<User, 'age' | 'email'>;
66
- * // { name: string; age?: number; email?: string }
67
- * ```
42
+ * // Check if a cookie exists
43
+ * if (hasClientSideCookie('userId')) {
44
+ * console.log('User is logged in');
45
+ * }
68
46
  */
69
- type SelectivePartial<T, K$1 extends keyof T> = Omit<T, K$1> & Partial<Pick<T, K$1>>;
47
+ declare const hasClientSideCookie: (name: string) => boolean;
70
48
  /**
71
- * Makes all properties of an object type required recursively.
49
+ * Retrieves the value of a client-side cookie.
72
50
  *
73
- * This type traverses through nested objects and arrays, making all properties required.
74
- * Functions and primitives are left unchanged.
75
- *
76
- * @template T - The type to make deeply required
77
- * @returns A type with all properties required recursively
51
+ * @param name - The name of the cookie to retrieve
52
+ * @returns An object containing the cookie value, or undefined if not found
78
53
  *
79
54
  * @example
80
- * ```ts
81
- * type PartialConfig = {
82
- * server?: { host?: string; port?: number };
83
- * };
84
- *
85
- * type RequiredConfig = DeepRequired<PartialConfig>;
86
- * // {
87
- * // server: { host: string; port: number };
88
- * // }
89
- * ```
55
+ * // Get a cookie value
56
+ * const { value } = getClientSideCookie('userId');
57
+ * if (value) {
58
+ * console.log('User ID:', value);
59
+ * }
90
60
  */
91
- 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;
61
+ declare const getClientSideCookie: (name: string) => {
62
+ value: string | undefined;
63
+ };
64
+ //#endregion
65
+ //#region src/functions/utils.d.ts
92
66
  /**
93
- * Makes only specified properties of an object type required.
67
+ * Utility to merge class names with Tailwind CSS and additional custom merging logic.
94
68
  *
95
- * @template T - The base object type
96
- * @template K - The keys to make required
97
- * @returns An object type with specified properties required
69
+ * @param {...ClassValue[]} inputs - Class names to merge.
70
+ * @returns {string} - A string of merged class names.
98
71
  *
99
72
  * @example
100
73
  * ```ts
101
- * type PartialUser = { name?: string; age?: number; email?: string };
102
- * type RequiredUser = SelectiveRequired<PartialUser, 'name'>;
103
- * // { name: string; age?: number; email?: string }
74
+ * cn('px-2 py-1', 'bg-red-500') // 'px-2 py-1 bg-red-500'
75
+ * cn('px-2', 'px-4') // 'px-4' (Tailwind resolves conflicts)
104
76
  * ```
105
77
  */
106
- type SelectiveRequired<T, K$1 extends keyof T> = Omit<T, K$1> & Required<Pick<T, K$1>>;
78
+ declare function cn(...inputs: ClassValue[]): string;
107
79
  /**
108
- * Creates a type where all properties are never (useful for excluding types).
80
+ * @deprecated Use isLinkActive instead.
109
81
  *
110
- * This can be used to create mutually exclusive types or to exclude certain properties.
82
+ * Determines if a navigation link is active based on the current path.
111
83
  *
112
- * @template T - The object type to transform
113
- * @returns An object type with all properties set to never
84
+ * @param href - The target URL.
85
+ * @param path - The current browser path.
86
+ * @returns - True if the navigation is active, false otherwise.
114
87
  *
115
88
  * @example
116
89
  * ```ts
117
- * type User = { name: string; age: number };
118
- * type ExcludedUser = Never<User>; // { name: never; age: never }
90
+ * isNavActive('/about', '/about/team') // true
91
+ * isNavActive('/contact', '/about') // false
119
92
  * ```
120
93
  */
121
- type Never<T> = { [K in keyof T]: never };
94
+ declare function isNavActive(href: string, path: string): boolean;
122
95
  /**
123
- * Makes all properties of an object type nullable recursively.
124
- *
125
- * @template T - The type to make nullable
126
- * @returns A type where all properties can be null
96
+ * Checks if a link is active, considering optional localization prefixes.
127
97
  *
128
- * @example
129
- * ```ts
130
- * type User = { name: string; profile: { age: number } };
131
- * type NullableUser = Nullable<User>;
132
- * // { name: string | null; profile: { age: number | null } | null }
133
- * ```
134
- */
135
- type Nullable<T> = T extends object ? { [P in keyof T]: Nullable<T[P]> } : T | null;
136
- /**
137
- * Makes all properties of an object type optional (undefined) recursively.
98
+ * Compares paths while ignoring locale prefixes (e.g., /en/, /fr/) for consistent
99
+ * navigation highlighting across different language versions of the same page.
138
100
  *
139
- * @template T - The type to make optional
140
- * @returns A type where all properties can be undefined
101
+ * @param params - Parameters object.
102
+ * @param params.path - The target path of the link.
103
+ * @param params.currentPath - The current browser path.
104
+ * @param params.locales - Supported locale prefixes to ignore during comparison.
105
+ * @param params.exact - Whether to require exact path match (default: true). If false, checks if current path starts with target path.
106
+ * @returns True if the link is active, false otherwise.
141
107
  *
142
108
  * @example
143
109
  * ```ts
144
- * type User = { name: string; profile: { age: number } };
145
- * type OptionalUser = Optional<User>;
146
- * // { name: string | undefined; profile: { age: number | undefined } | undefined }
147
- * ```
148
- */
149
- type Optional<T> = T extends object ? { [P in keyof T]: Optional<T[P]> } : T | undefined;
150
- /**
151
- * Makes all properties of an object type nullish (null or undefined) recursively.
110
+ * // Exact match
111
+ * isLinkActive({ path: '/about', currentPath: '/about' }) // true
112
+ * isLinkActive({ path: '/about', currentPath: '/about/team' }) // false
152
113
  *
153
- * @template T - The type to make nullish
154
- * @returns A type where all properties can be null or undefined
114
+ * // With locales
115
+ * isLinkActive({ path: '/about', currentPath: '/en/about' }) // true
116
+ * isLinkActive({ path: '/about', currentPath: '/fr/about' }) // true
155
117
  *
156
- * @example
157
- * ```ts
158
- * type User = { name: string; profile: { age: number } };
159
- * type NullishUser = Nullish<User>;
160
- * // { name: string | null | undefined; profile: { age: number | null | undefined } | null | undefined }
118
+ * // Partial match
119
+ * isLinkActive({ path: '/blog', currentPath: '/blog/post-1', exact: false }) // true
161
120
  * ```
162
121
  */
163
- type Nullish<T> = T extends object ? { [P in keyof T]: Nullish<T[P]> } : T | null | undefined;
122
+ declare function isLinkActive({
123
+ path,
124
+ currentPath,
125
+ locales,
126
+ exact
127
+ }: {
128
+ path: string;
129
+ currentPath: string;
130
+ locales?: string[];
131
+ exact?: boolean;
132
+ }): boolean;
164
133
  /**
165
- * Makes all properties of an object type optional and nullish recursively.
134
+ * Cleans a file path by removing the `/public/` prefix if present.
166
135
  *
167
- * This combines optional properties with nullish values.
136
+ * Useful when working with static assets that may have been processed
137
+ * or when normalizing paths between development and production environments.
168
138
  *
169
- * @template T - The type to make maybe
170
- * @returns A type where all properties are optional and can be null or undefined
139
+ * @param src - The source path to clean.
140
+ * @returns The cleaned path with `/public/` prefix removed and whitespace trimmed.
171
141
  *
172
142
  * @example
173
143
  * ```ts
174
- * type User = { name: string; profile: { age: number } };
175
- * type MaybeUser = Maybe<User>;
176
- * // { name?: string | null | undefined; profile?: { age?: number | null | undefined } | null | undefined }
144
+ * cleanSrc('/public/images/logo.png') // '/images/logo.png'
145
+ * cleanSrc('images/logo.png') // 'images/logo.png'
146
+ * cleanSrc(' /public/docs/readme.md ') // '/docs/readme.md'
177
147
  * ```
178
148
  */
179
- type Maybe<T> = T extends object ? { [P in keyof T]?: Nullish<T[P]> } : T | null | undefined;
149
+ declare function cleanSrc(src: string): string;
180
150
  /**
181
- * Makes all properties of an object type readonly recursively.
151
+ * Smoothly scrolls to the top or bottom of a specified container.
182
152
  *
183
- * This type traverses through nested objects and arrays, making all properties readonly.
184
- * Functions and primitives are left unchanged.
153
+ * Provides smooth scrolling animation to either end of a scrollable element.
154
+ * Accepts either a CSS selector string or a React ref to the container element.
185
155
  *
186
- * @template T - The type to make deeply readonly
187
- * @returns A type with all properties readonly recursively
156
+ * @param containerSelector - The CSS selector string or React ref for the scrollable container.
157
+ * @param to - Direction to scroll: 'top' for top of container, 'bottom' for bottom.
188
158
  *
189
159
  * @example
190
160
  * ```ts
191
- * type Config = {
192
- * server: { host: string; port: number };
193
- * features: string[];
194
- * };
161
+ * // Scroll to top using selector
162
+ * scrollTo('.main-content', 'top');
195
163
  *
196
- * type ReadonlyConfig = DeepReadonly<Config>;
197
- * // {
198
- * // readonly server: { readonly host: string; readonly port: number };
199
- * // readonly features: readonly string[];
200
- * // }
164
+ * // Scroll to bottom using ref
165
+ * scrollTo(containerRef, 'bottom');
201
166
  * ```
202
167
  */
203
- 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;
168
+ declare const scrollTo: (containerSelector: string | React.RefObject<HTMLDivElement>, to: "top" | "bottom") => void;
204
169
  /**
205
- * Removes readonly modifier from all properties of an object type recursively.
170
+ * Copies a given string to the clipboard using the modern Clipboard API.
206
171
  *
207
- * @template T - The readonly type to make mutable
208
- * @returns A type with all readonly modifiers removed
172
+ * Safely attempts to copy text to the user's clipboard. Falls back gracefully
173
+ * if the Clipboard API is not available or if the operation fails.
209
174
  *
210
- * @example
211
- * ```ts
212
- * type ReadonlyUser = { readonly name: string; readonly profile: { readonly age: number } };
213
- * type MutableUser = Mutable<ReadonlyUser>;
214
- * // { name: string; profile: { age: number } }
215
- * ```
216
- */
217
- type Mutable<T> = { -readonly [P in keyof T]: T[P] };
218
- /**
219
- * Extracts keys of an object type that have values of a specific type.
220
- *
221
- * @template T - The object type to search
222
- * @template U - The value type to match
223
- * @returns A union of keys whose values match the specified type
175
+ * @param value - The text value to copy to the clipboard.
176
+ * @param onSuccess - Optional callback executed after successful copy operation.
224
177
  *
225
178
  * @example
226
179
  * ```ts
227
- * type User = { name: string; age: number; active: boolean };
228
- * type StringKeys = KeysOfType<User, string>; // 'name'
229
- * type NumberKeys = KeysOfType<User, number>; // 'age'
180
+ * copyToClipboard('Hello World!', () => {
181
+ * console.log('Text copied successfully');
182
+ * });
230
183
  * ```
231
184
  */
232
- type KeysOfType<T, U$1> = { [K in keyof T]: T[K] extends U$1 ? K : never }[keyof T];
185
+ declare const copyToClipboard: (value: string, onSuccess?: () => void) => void;
233
186
  /**
234
- * Omits properties from an object type that have values of a specific type.
187
+ * Checks if the code is running in a server-side environment.
235
188
  *
236
- * @template T - The object type to filter
237
- * @template U - The value type to exclude
238
- * @returns An object type without properties of the specified value type
189
+ * @returns - True if the code is executed in SSR (Server-Side Rendering) context, false otherwise
239
190
  *
240
191
  * @example
241
192
  * ```ts
242
- * type Mixed = { name: string; age: number; active: boolean };
243
- * type WithoutStrings = OmitByType<Mixed, string>; // { age: number; active: boolean }
193
+ * if (isSSR) {
194
+ * // Server-side only code
195
+ * console.log('Running on server');
196
+ * } else {
197
+ * // Client-side only code
198
+ * window.addEventListener('load', () => {});
199
+ * }
244
200
  * ```
245
201
  */
246
- type OmitByType<T, U$1> = { [K in keyof T as T[K] extends U$1 ? never : K]: T[K] };
202
+ declare const isSSR: boolean;
247
203
  /**
248
- * Makes specified properties required while keeping others as-is.
204
+ * Converts an SVG string to a Base64-encoded string.
249
205
  *
250
- * @template T - The base object type
251
- * @template K - The keys to make required
252
- * @returns An object type with specified properties required
206
+ * @param str - The SVG string to encode
207
+ * @returns - Base64-encoded string representation of the SVG
253
208
  *
254
209
  * @example
255
210
  * ```ts
256
- * type PartialUser = { name?: string; age?: number; email?: string };
257
- * type RequiredNameUser = RequiredKeys<PartialUser, 'name'>;
258
- * // { name: string; age?: number; email?: string }
211
+ * const svg = '<svg><circle cx="50" cy="50" r="40"/></svg>';
212
+ * const base64 = svgToBase64(svg);
213
+ * // Use in data URL: `data:image/svg+xml;base64,${base64}`
259
214
  * ```
260
215
  */
261
- type RequiredKeys<T, K$1 extends keyof T> = Omit<T, K$1> & Required<Pick<T, K$1>>;
216
+ declare const svgToBase64: (str: string) => string;
262
217
  /**
263
- * Computes the symmetric difference between two object types.
264
- *
265
- * Properties that exist in either T or U but not in both.
218
+ * Merges multiple refs into a single ref callback.
266
219
  *
267
- * @template T - First object type
268
- * @template U - Second object type
269
- * @returns Properties unique to T or U
220
+ * @param refs - An array of refs to merge.
221
+ * @returns - A function that updates the merged ref with the provided value.
270
222
  *
271
223
  * @example
272
- * ```ts
273
- * type A = { x: number; y: string };
274
- * type B = { y: string; z: boolean };
275
- * type DiffAB = Diff<A, B>; // { x: number; z: boolean }
276
- * ```
277
- */
278
- type Diff<T, U$1> = Omit<T, keyof U$1> & Omit<U$1, keyof T>;
279
- /**
280
- * Computes the intersection of two object types (properties present in both).
224
+ * ```tsx
225
+ * const MyComponent = () => {
226
+ * const ref1 = useRef<HTMLDivElement>(null);
227
+ * const ref2 = useRef<HTMLDivElement>(null);
281
228
  *
282
- * @template T - First object type
283
- * @template U - Second object type
284
- * @returns Properties that exist in both T and U
229
+ * const mergedRef = mergeRefs(ref1, ref2);
285
230
  *
286
- * @example
287
- * ```ts
288
- * type A = { x: number; y: string };
289
- * type B = { y: string; z: boolean };
290
- * type IntersectionAB = Intersection<A, B>; // { y: string }
231
+ * return <div ref={mergedRef}>Content</div>;
232
+ * };
291
233
  * ```
292
234
  */
293
- type Intersection<T extends object, U$1 extends object> = Pick<T, Extract<keyof T, keyof U$1> & Extract<keyof U$1, keyof T>>;
235
+ type MergeRefs = <T>(...refs: Array<React.Ref<T> | undefined>) => React.RefCallback<T>;
236
+ declare const mergeRefs: MergeRefs;
294
237
  /**
295
- * Merges two object types, combining their properties.
238
+ * Navigates to the specified client-side hash without triggering SSR.
296
239
  *
297
- * @template T - First object type
298
- * @template U - Second object type
299
- * @returns A merged object type with properties from both
240
+ * Smoothly scrolls to an element by ID and updates the URL hash.
241
+ * Use `scroll-margin-top` CSS property to add margins for fixed headers.
300
242
  *
301
- * @example
302
- * ```ts
303
- * type A = { x: number; y: string };
304
- * type B = { y: boolean; z: string };
305
- * type Merged = Merge<A, B>; // { x: number; y: boolean; z: string }
306
- * ```
307
- */
308
- 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>;
309
- /**
310
- * Subtracts properties of one object type from another.
311
- *
312
- * @template T - The object type to subtract from
313
- * @template U - The object type whose properties to subtract
314
- * @returns T without properties that exist in U
243
+ * @param id - The ID of the element (without #) to navigate to.
244
+ * @param opts - Additional options for scrollIntoView.
315
245
  *
316
246
  * @example
317
247
  * ```ts
318
- * type A = { x: number; y: string; z: boolean };
319
- * type B = { y: string };
320
- * type Subtracted = Substract<A, B>; // { x: number; z: boolean }
321
- * ```
322
- */
323
- type Substract<T extends object, U$1 extends object> = Omit<T, keyof U$1>;
324
- /**
325
- * Represents either all properties present or none of them.
248
+ * // Navigate to an element
249
+ * goToClientSideHash('section-about');
326
250
  *
327
- * Useful for creating mutually exclusive configurations.
328
- *
329
- * @template T - The object type
330
- * @returns Either the full object or an empty object with optional properties
331
- *
332
- * @example
333
- * ```ts
334
- * type Config = { host: string; port: number };
335
- * type AllOrNoneConfig = AllOrNone<Config>;
336
- * // { host: string; port: number } | {}
251
+ * // With custom scroll behavior
252
+ * goToClientSideHash('contact', { behavior: 'auto', block: 'center' });
337
253
  * ```
338
254
  */
339
- type AllOrNone<T> = T | { [P in keyof T]?: never };
255
+ declare function goToClientSideHash(id: string, opts?: ScrollIntoViewOptions): void;
256
+ //#endregion
257
+ //#region src/functions/worker.d.ts
340
258
  /**
341
- * Represents exactly one property from an object type being present.
259
+ * Converts a regular function into a workerized version that runs in a Web Worker.
342
260
  *
343
- * Useful for creating discriminated unions or mutually exclusive options.
261
+ * This provides the ultimate DX for Web Workers - just write a normal function
262
+ * and "workerize" it to run in the background without blocking the UI.
344
263
  *
345
- * @template T - The object type
346
- * @returns A union where only one property is present at a time
264
+ * @param fn - The function to workerize
265
+ * @returns A function that calls the original function in a worker and returns a Promise
347
266
  *
348
267
  * @example
349
268
  * ```ts
350
- * type Action = { type: 'create'; payload: string } | { type: 'update'; id: number };
351
- * type OneAction = OneOf<Action>;
352
- * // { type: 'create'; payload: string } | { type: 'update'; id: number }
353
- * ```
354
- */
355
- type OneOf<T> = { [K in keyof T]: Pick<T, K> }[keyof T];
356
- /**
357
- * Represents exactly two properties from an object type being present.
358
- *
359
- * @template T - The object type
360
- * @returns A union where exactly two properties are present at a time
361
- *
362
- * @example
363
- * ```ts
364
- * type Config = { a: number; b: string; c: boolean };
365
- * type TwoConfig = TwoOf<Config>;
366
- * // { a: number; b: string } | { a: number; c: boolean } | { b: string; c: boolean }
367
- * ```
368
- */
369
- type TwoOf<T> = { [K in keyof T]: { [L in Exclude<keyof T, K>]: Pick<T, K | L> }[Exclude<keyof T, K>] }[keyof T];
370
- /**
371
- * Prettifies a complex type by expanding it for better readability in tooltips.
372
- *
373
- * This type doesn't change the runtime type but helps with IntelliSense display.
269
+ * // Define a normal function
270
+ * function fibonacci(n: number): number {
271
+ * if (n <= 1) return n;
272
+ * return fibonacci(n - 1) + fibonacci(n - 2);
273
+ * }
374
274
  *
375
- * @template T - The type to prettify
376
- * @returns The same type but expanded for better readability
275
+ * // Workerize it
276
+ * const workerizedFib = workerize(fibonacci);
377
277
  *
378
- * @example
379
- * ```ts
380
- * type Complex = { a: string } & { b: number };
381
- * type PrettyComplex = Prettify<Complex>; // Shows as { a: string; b: number }
278
+ * // Use like a normal async function!
279
+ * const result = await workerizedFib(35);
280
+ * console.log(result); // Works just like the original function
382
281
  * ```
383
- */
384
- type Prettify<T> = T extends infer U ? U extends object ? { [K in keyof U]: U[K] } & {} : U : never;
385
- /**
386
- * Extracts all nested keys of an object type as dot-separated strings.
387
- *
388
- * @template ObjectType - The object type to extract nested keys from
389
- * @template IgnoreKeys - Keys to ignore during extraction
390
- * @returns A union of dot-separated string paths
391
282
  *
392
283
  * @example
393
284
  * ```ts
394
- * type User = {
395
- * name: string;
396
- * profile: { age: number; address: { city: string } };
397
- * tags: string[];
398
- * };
399
- *
400
- * type UserPaths = NestedKeyOf<User>;
401
- * // 'name' | 'profile' | 'profile.age' | 'profile.address' | 'profile.address.city' | 'tags'
402
- * ```
403
- */
404
- 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];
405
- /**
406
- * Creates a type that excludes properties present in another type.
407
- *
408
- * This is useful for creating mutually exclusive types.
409
- *
410
- * @template T - The base type
411
- * @template U - The type whose properties to exclude
412
- * @returns A type with properties from T that are not in U
285
+ * // Works with any function signature
286
+ * const sumArray = workerize((arr: number[]) =>
287
+ * arr.reduce((a, b) => a + b, 0)
288
+ * );
413
289
  *
414
- * @example
415
- * ```ts
416
- * type A = { x: number; y: string };
417
- * type B = { y: string };
418
- * type WithoutB = Without<A, B>; // { x?: never }
290
+ * const result = await sumArray([1, 2, 3, 4, 5]); // 15
419
291
  * ```
420
292
  */
421
- type Without<T, U$1> = { [P in Exclude<keyof T, keyof U$1>]?: never };
422
- //#endregion
423
- //#region src/types/gates.d.ts
424
- type BUFFER<T> = T;
425
- type IMPLIES<T, U$1> = T extends U$1 ? true : false;
426
- 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;
427
- type XNOR_Binary<T, U$1> = (T & U$1) | (Without<T, U$1> & Without<U$1, T>);
428
- /**
429
- * Computes a type-level AND (all must true) for a tuple of types.
430
- *
431
- * Truth table for 3 arguments:
432
- *
433
- * A B C = AND
434
- * 1 1 1 = 1
435
- * 1 1 0 = 0
436
- * 1 0 1 = 0
437
- * 1 0 0 = 0
438
- * 0 1 1 = 0
439
- * 0 1 0 = 0
440
- * 0 0 1 = 0
441
- * 0 0 0 = 0
442
- *
443
- * @template T - Tuple of boolean-like types (1/0)
444
- */
445
- type AND<T extends any[]> = T extends [infer F, ...infer R] ? R extends any[] ? F & AND<R> : F : unknown;
446
- /**
447
- * Computes a type-level OR (At least one) for a tuple of types.
448
- *
449
- * Truth table for 3 arguments:
450
- *
451
- * A B C = OR
452
- * 1 1 1 = 1
453
- * 1 1 0 = 1
454
- * 1 0 1 = 1
455
- * 1 0 0 = 1
456
- * 0 1 1 = 1
457
- * 0 1 0 = 1
458
- * 0 0 1 = 1
459
- * 0 0 0 = 0
460
- *
461
- * @template T - Tuple of boolean-like types (1/0)
462
- */
463
- type OR<T extends any[]> = T extends [infer F, ...infer R] ? R extends any[] ? F | OR<R> : F : never;
464
- /**
465
- * Computes a type-level XOR (only one/odd) for a tuple of types.
466
- *
467
- * Truth table for 3 arguments:
468
- *
469
- * A B C = XOR
470
- * 1 1 1 = 1
471
- * 1 1 0 = 0
472
- * 1 0 1 = 0
473
- * 1 0 0 = 1
474
- * 0 1 1 = 0
475
- * 0 1 0 = 1
476
- * 0 0 1 = 1
477
- * 0 0 0 = 0
478
- *
479
- * @template T - Tuple of boolean-like types (1/0)
480
- */
481
- 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;
482
- /**
483
- * Computes a type-level XNOR (All or None true) for a tuple of types.
484
- *
485
- * Truth table for 3 arguments:
486
- *
487
- * A B C = XNOR
488
- * 1 1 1 = 0
489
- * 1 1 0 = 1
490
- * 1 0 1 = 1
491
- * 1 0 0 = 0
492
- * 0 1 1 = 1
493
- * 0 1 0 = 0
494
- * 0 0 1 = 0
495
- * 0 0 0 = 1
496
- *
497
- * @template T - Tuple of boolean-like types (1/0)
498
- */
499
- 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;
500
- /**
501
- * Computes a type-level NOT for a tuple of types.
502
- *
503
- * Truth table for 3 arguments:
504
- *
505
- * A B C = NOT
506
- * 1 1 1 = 0
507
- * 1 1 0 = 0
508
- * 1 0 1 = 0
509
- * 1 0 0 = 0
510
- * 0 1 1 = 0
511
- * 0 1 0 = 0
512
- * 0 0 1 = 0
513
- * 0 0 0 = 1
514
- *
515
- * @template T - Tuple of boolean-like types (1/0)
516
- */
517
- type NOT<T> = { [P in keyof T]?: never };
518
- /**
519
- * Computes a type-level NAND for a tuple of types.
520
- *
521
- * Truth table for 3 arguments:
522
- *
523
- * A B C = NAND
524
- * 1 1 1 = 0
525
- * 1 1 0 = 1
526
- * 1 0 1 = 1
527
- * 1 0 0 = 1
528
- * 0 1 1 = 1
529
- * 0 1 0 = 1
530
- * 0 0 1 = 1
531
- * 0 0 0 = 1
532
- *
533
- * @template T - Tuple of boolean-like types (1/0)
534
- */
535
- type NAND<T extends any[]> = NOT<AND<T>>;
536
- /**
537
- * Computes a type-level NOR for a tuple of types.
538
- *
539
- * Truth table for 3 arguments:
540
- *
541
- * A B C = NOR
542
- * 1 1 1 = 0
543
- * 1 1 0 = 0
544
- * 1 0 1 = 0
545
- * 1 0 0 = 0
546
- * 0 1 1 = 0
547
- * 0 1 0 = 0
548
- * 0 0 1 = 0
549
- * 0 0 0 = 1
550
- *
551
- * @template T - Tuple of boolean-like types (1/0)
552
- */
553
- type NOR<T extends any[]> = NOT<OR<T>>;
554
- //#endregion
555
- //#region src/types/guards.d.ts
556
- /**
557
- * Represents primitive JavaScript types including null and undefined.
558
- */
559
- type Primitive = string | number | bigint | boolean | symbol | null | undefined;
560
- /**
561
- * Represents all falsy values in JavaScript.
562
- */
563
- type Falsy = false | '' | 0 | null | undefined;
564
- /**
565
- * Type guard that checks if a value is falsy.
566
- *
567
- * @param val - The value to check
568
- * @returns True if the value is falsy, false otherwise
569
- *
570
- * @example
571
- * if (isFalsy(value)) {
572
- * console.log('Value is falsy');
573
- * }
574
- */
575
- declare const isFalsy: (val: unknown) => val is Falsy;
576
- /**
577
- * Type guard that checks if a value is null or undefined.
578
- *
579
- * @param val - The value to check
580
- * @returns True if the value is null or undefined, false otherwise
581
- *
582
- * @example
583
- * if (isNullish(value)) {
584
- * console.log('Value is null or undefined');
585
- * }
586
- */
587
- declare const isNullish: (val: unknown) => val is null | undefined;
588
- /**
589
- * Type guard that checks if a value is a primitive type.
590
- *
591
- * @param val - The value to check
592
- * @returns True if the value is a primitive, false otherwise
593
- *
594
- * @example
595
- * if (isPrimitive(value)) {
596
- * console.log('Value is a primitive type');
597
- * }
598
- */
599
- declare const isPrimitive: (val: unknown) => val is Primitive;
600
- /**
601
- * Type guard that checks if a value is a plain object (not an array, function, etc.).
602
- *
603
- * @param value - The value to check
604
- * @returns True if the value is a plain object, false otherwise
605
- *
606
- * @example
607
- * if (isPlainObject(value)) {
608
- * console.log('Value is a plain object');
609
- * }
610
- */
611
- declare function isPlainObject(value: unknown): value is Record<string, any>;
293
+ declare function workerize<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => Promise<ReturnType<T>>;
612
294
  //#endregion
613
- export { AND, AllOrNone, BUFFER, DeepMergeOptions, 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, workerize };
295
+ export { MergeRefs, cleanSrc, cn, copyToClipboard, deleteClientSideCookie, getClientSideCookie, goToClientSideHash, hasClientSideCookie, isLinkActive, isNavActive, isSSR, mergeRefs, scrollTo, setClientSideCookie, svgToBase64, workerize };