@sohanemon/utils 6.3.8 → 6.4.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/README.md +12 -2
- package/dist/components/index.cjs +1 -1
- package/dist/components/index.d.cts +101 -5
- package/dist/components/index.d.ts +97 -1
- package/dist/components/index.js +1 -1
- package/dist/{functions-DdbPkP6e.cjs → functions-BQWC9vZ0.cjs} +1 -1
- package/dist/hooks/index.cjs +1 -1
- package/dist/hooks/index.d.cts +337 -23
- package/dist/hooks/index.d.ts +337 -23
- package/dist/hooks/index.js +1 -1
- package/dist/hooks-C9b8HxlS.js +1 -0
- package/dist/hooks-CswCcD42.cjs +1 -0
- package/dist/{index-BZaFEd2-.d.ts → index-CKE8ocfo.d.ts} +479 -59
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +890 -57
- package/dist/index.d.ts +443 -1
- package/dist/schedule-BqFAJlSO.d.cts +43 -0
- package/package.json +4 -2
- package/dist/hooks-Brgpm2Un.cjs +0 -1
- package/dist/hooks-CKCxXGDE.js +0 -1
- package/dist/schedule-CRsY0oqG.d.cts +0 -15
package/dist/index.d.ts
CHANGED
|
@@ -1,31 +1,423 @@
|
|
|
1
|
-
import { A as getClientSideCookie, C as schedule, D as hydrate, E as getObjectValue, M as setClientSideCookie, O as deepmerge, S as Task, T as extendProps, _ as sleep, a as convertToSlug, b as shield, c as escapeRegExp, d as isNavActive, f as isSSR, g as scrollTo, h as printf, i as convertToNormalCase, j as hasClientSideCookie, k as deleteClientSideCookie, l as goToClientSideHash, m as normalizeText, n as cleanSrc, o as copyToClipboard, p as mergeRefs, r as cn, s as debounce, t as MergeRefs, u as isLinkActive, v as svgToBase64, w as poll, x as ScheduleOpts, y as throttle } from "./index-
|
|
1
|
+
import { A as getClientSideCookie, C as schedule, D as hydrate, E as getObjectValue, M as setClientSideCookie, O as deepmerge, S as Task, T as extendProps, _ as sleep, a as convertToSlug, b as shield, c as escapeRegExp, d as isNavActive, f as isSSR, g as scrollTo, h as printf, i as convertToNormalCase, j as hasClientSideCookie, k as deleteClientSideCookie, l as goToClientSideHash, m as normalizeText, n as cleanSrc, o as copyToClipboard, p as mergeRefs, r as cn, s as debounce, t as MergeRefs, u as isLinkActive, v as svgToBase64, w as poll, x as ScheduleOpts, y as throttle } from "./index-CKE8ocfo.js";
|
|
2
2
|
|
|
3
3
|
//#region src/types/utilities.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Extracts the keys of an object type as a union type.
|
|
7
|
+
*
|
|
8
|
+
* @template T - The object type to extract keys from
|
|
9
|
+
* @returns A union of all keys in the object type
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* type User = { name: string; age: number };
|
|
14
|
+
* type UserKeys = Keys<User>; // 'name' | 'age'
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
4
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
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* type User = { name: string; age: number };
|
|
27
|
+
* type UserValues = Values<User>; // string | number
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
5
30
|
type Values<T extends object> = T[keyof T];
|
|
31
|
+
/**
|
|
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.
|
|
36
|
+
*
|
|
37
|
+
* @template T - The type to make deeply partial
|
|
38
|
+
* @returns A type with all properties optional recursively
|
|
39
|
+
*
|
|
40
|
+
* @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
|
+
* ```
|
|
53
|
+
*/
|
|
6
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;
|
|
55
|
+
/**
|
|
56
|
+
* Makes only specified properties of an object type optional.
|
|
57
|
+
*
|
|
58
|
+
* @template T - The base object type
|
|
59
|
+
* @template K - The keys to make optional
|
|
60
|
+
* @returns An object type with specified properties optional
|
|
61
|
+
*
|
|
62
|
+
* @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
|
+
* ```
|
|
68
|
+
*/
|
|
7
69
|
type SelectivePartial<T, K$1 extends keyof T> = Omit<T, K$1> & Partial<Pick<T, K$1>>;
|
|
70
|
+
/**
|
|
71
|
+
* Makes all properties of an object type required recursively.
|
|
72
|
+
*
|
|
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
|
|
78
|
+
*
|
|
79
|
+
* @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
|
+
* ```
|
|
90
|
+
*/
|
|
8
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;
|
|
92
|
+
/**
|
|
93
|
+
* Makes only specified properties of an object type required.
|
|
94
|
+
*
|
|
95
|
+
* @template T - The base object type
|
|
96
|
+
* @template K - The keys to make required
|
|
97
|
+
* @returns An object type with specified properties required
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* type PartialUser = { name?: string; age?: number; email?: string };
|
|
102
|
+
* type RequiredUser = SelectiveRequired<PartialUser, 'name'>;
|
|
103
|
+
* // { name: string; age?: number; email?: string }
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
9
106
|
type SelectiveRequired<T, K$1 extends keyof T> = Omit<T, K$1> & Required<Pick<T, K$1>>;
|
|
107
|
+
/**
|
|
108
|
+
* Creates a type where all properties are never (useful for excluding types).
|
|
109
|
+
*
|
|
110
|
+
* This can be used to create mutually exclusive types or to exclude certain properties.
|
|
111
|
+
*
|
|
112
|
+
* @template T - The object type to transform
|
|
113
|
+
* @returns An object type with all properties set to never
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* type User = { name: string; age: number };
|
|
118
|
+
* type ExcludedUser = Never<User>; // { name: never; age: never }
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
10
121
|
type Never<T> = { [K in keyof T]: never };
|
|
122
|
+
/**
|
|
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
|
|
127
|
+
*
|
|
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
|
+
*/
|
|
11
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.
|
|
138
|
+
*
|
|
139
|
+
* @template T - The type to make optional
|
|
140
|
+
* @returns A type where all properties can be undefined
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```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
|
+
*/
|
|
12
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.
|
|
152
|
+
*
|
|
153
|
+
* @template T - The type to make nullish
|
|
154
|
+
* @returns A type where all properties can be null or undefined
|
|
155
|
+
*
|
|
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 }
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
13
163
|
type Nullish<T> = T extends object ? { [P in keyof T]: Nullish<T[P]> } : T | null | undefined;
|
|
164
|
+
/**
|
|
165
|
+
* Makes all properties of an object type optional and nullish recursively.
|
|
166
|
+
*
|
|
167
|
+
* This combines optional properties with nullish values.
|
|
168
|
+
*
|
|
169
|
+
* @template T - The type to make maybe
|
|
170
|
+
* @returns A type where all properties are optional and can be null or undefined
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```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 }
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
14
179
|
type Maybe<T> = T extends object ? { [P in keyof T]?: Nullish<T[P]> } : T | null | undefined;
|
|
180
|
+
/**
|
|
181
|
+
* Makes all properties of an object type readonly recursively.
|
|
182
|
+
*
|
|
183
|
+
* This type traverses through nested objects and arrays, making all properties readonly.
|
|
184
|
+
* Functions and primitives are left unchanged.
|
|
185
|
+
*
|
|
186
|
+
* @template T - The type to make deeply readonly
|
|
187
|
+
* @returns A type with all properties readonly recursively
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* type Config = {
|
|
192
|
+
* server: { host: string; port: number };
|
|
193
|
+
* features: string[];
|
|
194
|
+
* };
|
|
195
|
+
*
|
|
196
|
+
* type ReadonlyConfig = DeepReadonly<Config>;
|
|
197
|
+
* // {
|
|
198
|
+
* // readonly server: { readonly host: string; readonly port: number };
|
|
199
|
+
* // readonly features: readonly string[];
|
|
200
|
+
* // }
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
15
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;
|
|
204
|
+
/**
|
|
205
|
+
* Removes readonly modifier from all properties of an object type recursively.
|
|
206
|
+
*
|
|
207
|
+
* @template T - The readonly type to make mutable
|
|
208
|
+
* @returns A type with all readonly modifiers removed
|
|
209
|
+
*
|
|
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
|
+
*/
|
|
16
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
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```ts
|
|
227
|
+
* type User = { name: string; age: number; active: boolean };
|
|
228
|
+
* type StringKeys = KeysOfType<User, string>; // 'name'
|
|
229
|
+
* type NumberKeys = KeysOfType<User, number>; // 'age'
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
17
232
|
type KeysOfType<T, U$1> = { [K in keyof T]: T[K] extends U$1 ? K : never }[keyof T];
|
|
233
|
+
/**
|
|
234
|
+
* Omits properties from an object type that have values of a specific type.
|
|
235
|
+
*
|
|
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
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```ts
|
|
242
|
+
* type Mixed = { name: string; age: number; active: boolean };
|
|
243
|
+
* type WithoutStrings = OmitByType<Mixed, string>; // { age: number; active: boolean }
|
|
244
|
+
* ```
|
|
245
|
+
*/
|
|
18
246
|
type OmitByType<T, U$1> = { [K in keyof T as T[K] extends U$1 ? never : K]: T[K] };
|
|
247
|
+
/**
|
|
248
|
+
* Makes specified properties required while keeping others as-is.
|
|
249
|
+
*
|
|
250
|
+
* @template T - The base object type
|
|
251
|
+
* @template K - The keys to make required
|
|
252
|
+
* @returns An object type with specified properties required
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```ts
|
|
256
|
+
* type PartialUser = { name?: string; age?: number; email?: string };
|
|
257
|
+
* type RequiredNameUser = RequiredKeys<PartialUser, 'name'>;
|
|
258
|
+
* // { name: string; age?: number; email?: string }
|
|
259
|
+
* ```
|
|
260
|
+
*/
|
|
19
261
|
type RequiredKeys<T, K$1 extends keyof T> = Omit<T, K$1> & Required<Pick<T, K$1>>;
|
|
262
|
+
/**
|
|
263
|
+
* Computes the symmetric difference between two object types.
|
|
264
|
+
*
|
|
265
|
+
* Properties that exist in either T or U but not in both.
|
|
266
|
+
*
|
|
267
|
+
* @template T - First object type
|
|
268
|
+
* @template U - Second object type
|
|
269
|
+
* @returns Properties unique to T or U
|
|
270
|
+
*
|
|
271
|
+
* @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
|
+
*/
|
|
20
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).
|
|
281
|
+
*
|
|
282
|
+
* @template T - First object type
|
|
283
|
+
* @template U - Second object type
|
|
284
|
+
* @returns Properties that exist in both T and U
|
|
285
|
+
*
|
|
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 }
|
|
291
|
+
* ```
|
|
292
|
+
*/
|
|
21
293
|
type Intersection<T extends object, U$1 extends object> = Pick<T, Extract<keyof T, keyof U$1> & Extract<keyof U$1, keyof T>>;
|
|
294
|
+
/**
|
|
295
|
+
* Merges two object types, combining their properties.
|
|
296
|
+
*
|
|
297
|
+
* @template T - First object type
|
|
298
|
+
* @template U - Second object type
|
|
299
|
+
* @returns A merged object type with properties from both
|
|
300
|
+
*
|
|
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
|
+
*/
|
|
22
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
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* ```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
|
+
*/
|
|
23
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.
|
|
326
|
+
*
|
|
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 } | {}
|
|
337
|
+
* ```
|
|
338
|
+
*/
|
|
24
339
|
type AllOrNone<T> = T | { [P in keyof T]?: never };
|
|
340
|
+
/**
|
|
341
|
+
* Represents exactly one property from an object type being present.
|
|
342
|
+
*
|
|
343
|
+
* Useful for creating discriminated unions or mutually exclusive options.
|
|
344
|
+
*
|
|
345
|
+
* @template T - The object type
|
|
346
|
+
* @returns A union where only one property is present at a time
|
|
347
|
+
*
|
|
348
|
+
* @example
|
|
349
|
+
* ```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
|
+
*/
|
|
25
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
|
+
*/
|
|
26
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.
|
|
374
|
+
*
|
|
375
|
+
* @template T - The type to prettify
|
|
376
|
+
* @returns The same type but expanded for better readability
|
|
377
|
+
*
|
|
378
|
+
* @example
|
|
379
|
+
* ```ts
|
|
380
|
+
* type Complex = { a: string } & { b: number };
|
|
381
|
+
* type PrettyComplex = Prettify<Complex>; // Shows as { a: string; b: number }
|
|
382
|
+
* ```
|
|
383
|
+
*/
|
|
27
384
|
type Prettify<T> = { [K in keyof T]: T[K] } & {};
|
|
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
|
+
*
|
|
392
|
+
* @example
|
|
393
|
+
* ```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
|
+
*/
|
|
28
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
|
|
413
|
+
*
|
|
414
|
+
* @example
|
|
415
|
+
* ```ts
|
|
416
|
+
* type A = { x: number; y: string };
|
|
417
|
+
* type B = { y: string };
|
|
418
|
+
* type WithoutB = Without<A, B>; // { x?: never }
|
|
419
|
+
* ```
|
|
420
|
+
*/
|
|
29
421
|
type Without<T, U$1> = { [P in Exclude<keyof T, keyof U$1>]?: never };
|
|
30
422
|
//#endregion
|
|
31
423
|
//#region src/types/gates.d.ts
|
|
@@ -161,11 +553,61 @@ type NAND<T extends any[]> = NOT<AND<T>>;
|
|
|
161
553
|
type NOR<T extends any[]> = NOT<OR<T>>;
|
|
162
554
|
//#endregion
|
|
163
555
|
//#region src/types/guards.d.ts
|
|
556
|
+
/**
|
|
557
|
+
* Represents primitive JavaScript types including null and undefined.
|
|
558
|
+
*/
|
|
164
559
|
type Primitive = string | number | bigint | boolean | symbol | null | undefined;
|
|
560
|
+
/**
|
|
561
|
+
* Represents all falsy values in JavaScript.
|
|
562
|
+
*/
|
|
165
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
|
+
*/
|
|
166
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
|
+
*/
|
|
167
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
|
+
*/
|
|
168
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
|
+
*/
|
|
169
611
|
declare function isPlainObject(value: unknown): value is Record<string, any>;
|
|
170
612
|
//#endregion
|
|
171
613
|
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 };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
//#region src/functions/schedule.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A task function that can be synchronous or asynchronous.
|
|
4
|
+
*/
|
|
5
|
+
type Task = () => Promise<void> | void;
|
|
6
|
+
/**
|
|
7
|
+
* Options for configuring the schedule function.
|
|
8
|
+
*/
|
|
9
|
+
interface ScheduleOpts {
|
|
10
|
+
/** Number of retry attempts on failure. Defaults to 0. */
|
|
11
|
+
retry?: number;
|
|
12
|
+
/** Delay in milliseconds between retries. Defaults to 0. */
|
|
13
|
+
delay?: number;
|
|
14
|
+
/** Maximum time in milliseconds to wait for the task to complete. */
|
|
15
|
+
timeout?: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Runs a function asynchronously in the background without blocking the main thread.
|
|
19
|
+
*
|
|
20
|
+
* Executes the task immediately using setTimeout, with optional retry logic on failure.
|
|
21
|
+
* Useful for non-critical operations like analytics, logging, or background processing.
|
|
22
|
+
* Logs execution time and retry attempts to the console.
|
|
23
|
+
*
|
|
24
|
+
* @param task - The function to execute asynchronously
|
|
25
|
+
* @param options - Configuration options for retries and timing
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* // Simple background task
|
|
30
|
+
* schedule(() => {
|
|
31
|
+
* console.log('Background work done');
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* // Task with retry on failure
|
|
35
|
+
* schedule(
|
|
36
|
+
* () => sendAnalytics(),
|
|
37
|
+
* { retry: 3, delay: 1000 }
|
|
38
|
+
* );
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
declare function schedule(task: Task, options?: ScheduleOpts): void;
|
|
42
|
+
//#endregion
|
|
43
|
+
export { Task as n, schedule as r, ScheduleOpts as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sohanemon/utils",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.4.0",
|
|
4
4
|
"author": "Sohan Emon <sohanemon@outlook.com>",
|
|
5
5
|
"description": "",
|
|
6
6
|
"type": "module",
|
|
@@ -78,6 +78,7 @@
|
|
|
78
78
|
"@vitest/ui": "^4.0.14",
|
|
79
79
|
"jsdom": "^27.2.0",
|
|
80
80
|
"tsdown": "^0.16.4",
|
|
81
|
+
"typedoc-plugin-markdown": "^4.9.0",
|
|
81
82
|
"typescript": "^5.9.3",
|
|
82
83
|
"vite": "npm:rolldown-vite@^7.2.5",
|
|
83
84
|
"vitest": "^4.0.14"
|
|
@@ -86,7 +87,8 @@
|
|
|
86
87
|
"@iconify/react": "^6.0.2",
|
|
87
88
|
"@radix-ui/react-slot": "^1.2.4",
|
|
88
89
|
"clsx": "^2.1.1",
|
|
89
|
-
"tailwind-merge": "^3.3.1"
|
|
90
|
+
"tailwind-merge": "^3.3.1",
|
|
91
|
+
"typedoc": "^0.28.15"
|
|
90
92
|
},
|
|
91
93
|
"peerDependencies": {
|
|
92
94
|
"react": "^19.2.0",
|
package/dist/hooks-Brgpm2Un.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));const c=require(`./functions-DdbPkP6e.cjs`);let l=require(`react`);l=s(l);let u=require(`@radix-ui/react-slot`),d=require(`react/jsx-runtime`);const f=(e,t)=>{let[n,r]=l.useState(`idle`),[i,a]=l.useState(null),[o,s]=l.useState(null),[c,u]=l.useState(void 0),[d,f]=l.useTransition(),p=n===`idle`,m=n===`loading`||d,h=n===`success`,g=n===`error`,_=l.useCallback(e=>{a(e),r(`success`),t?.onSuccess?.(e),t?.onSettled?.()},[t]),v=l.useCallback(e=>{s(e),r(`error`),t?.onError?.(e),t?.onSettled?.()},[t]),y=l.useCallback(t=>{u(t),r(`loading`),s(null),f(()=>{e(t).then(_).catch(v)})},[e,_,v]);return{execute:y,executeAsync:l.useCallback(t=>new Promise((n,i)=>{u(t),r(`loading`),s(null),f(()=>{e(t).then(e=>{_(e),n(e)}).catch(e=>{v(e),i(e)})})}),[e,_,v]),reset:l.useCallback(()=>{r(`idle`),a(null),s(null),u(void 0)},[]),useExecute:e=>{w(()=>{y(e)},[])},data:i,error:o,input:c,isIdle:p,isLoading:m,isSuccess:h,isError:g}};function p(e,t={}){let{mode:n=`manual`,deps:r,onSuccess:i,onError:a,onSettled:o}=t,[s,c]=l.useState(void 0),[u,d]=l.useState(void 0),[f,p]=l.useState(`idle`),m=l.useRef(null),h=l.useCallback(i||(()=>{}),[i]),g=l.useCallback(a||(()=>{}),[a]),_=l.useCallback(o||(()=>{}),[o]),v=l.useCallback(async()=>{m.current&&m.current.abort(),m.current=new AbortController;let t=m.current.signal;p(`pending`),d(void 0);try{let n=await e(t);return t.aborted||(c(n),p(`success`),await h(n),await _(n,void 0)),n}catch(e){if(e instanceof Error&&e.name===`AbortError`)return;let n=e instanceof Error?e:Error(String(e)),r=n;throw t.aborted||(d(r),p(`error`),await g(r),await _(void 0,r)),n}},[e,h,g,_]);return l.useEffect(()=>{n===`auto`&&!r&&v()},[e,n,v,r]),l.useEffect(()=>{r&&n===`auto`&&v()},r||[]),l.useEffect(()=>()=>{m.current&&m.current.abort()},[]),{data:s,error:u,status:f,isIdle:f===`idle`,isPending:f===`pending`,isSuccess:f===`success`,isError:f===`error`,execute:v}}function m(e={}){let{timeout:t=1e4}=e;return l.useCallback(e=>{let n=()=>{try{l.startTransition(()=>{e()})}catch(e){console.log(`⚡[schedule.tsx] Failed: `,e)}};`requestIdleCallback`in window?requestIdleCallback(n,{timeout:t}):c.n(n)},[t])}function h(e,t=[],n={}){let r=m(n);l.useEffect(()=>{let t;return r(()=>{t=e()}),()=>{typeof t==`function`&&t?.()}},[r,...t])}const g=l.createContext(null),_=({children:e})=>{let t=l.useRef(null);return(0,d.jsx)(g.Provider,{value:t,children:(0,d.jsx)(u.Slot,{ref:t,children:e})})},v=({threshold:e=300,container:t}={})=>{let n=l.useContext(g),r=t??n,[i,a]=l.useState({scrolledPast:!1,direction:`forward`}),o=l.useRef(0),s=t=>{let n=t>o.current?`forward`:`backward`;a({scrolledPast:t>e,direction:n}),o.current=t};return h(()=>{let e=r?.current,t=e||window,n=e?`scrollTop`:`scrollY`,i=()=>{let e=t[n];s(e)};return t.addEventListener(`scroll`,i),i(),()=>t.removeEventListener(`scroll`,i)},[r,e]),i},y=(e=()=>alert(`clicked outside`))=>{let t=l.useRef(null),n=n=>{t.current&&!t.current.contains(n.target)&&e()};return l.useEffect(()=>(document.addEventListener(`mousedown`,n),document.addEventListener(`touchstart`,n),()=>{document.removeEventListener(`mousedown`,n),document.removeEventListener(`touchstart`,n)})),t};function b(e){let t=l.useMemo(()=>{switch(e){case`sm`:return`(min-width: 640px)`;case`md`:return`(min-width: 768px)`;case`lg`:return`(min-width: 1024px)`;case`xl`:return`(min-width: 1280px)`;case`2xl`:return`(min-width: 1536px)`;default:return e}},[e]),n=e=>typeof window<`u`?window.matchMedia(e).matches:!1,[r,i]=l.useState(n(t)),a=()=>{i(n(t))};return w(()=>{let e=window.matchMedia(t);return a(),e.addEventListener(`change`,a),()=>{e.removeEventListener(`change`,a)}},[t]),r}function x(e){l.useEffect(e,[])}function S(e,t){let n=l.useRef(!0);l.useEffect(()=>{if(n.current)n.current=!1;else return e()},t)}function C(e,t=500){let[n,r]=l.useState(e);return l.useEffect(()=>{let n=setTimeout(()=>r(e),t);return()=>{clearTimeout(n)}},[e,t]),n}const w=typeof window<`u`?l.useLayoutEffect:l.useEffect;function T(e,t=1e3){let n=l.useRef(e);w(()=>{n.current=e},[e]),l.useEffect(()=>{if(!t&&t!==0)return;let e=setTimeout(()=>n.current(),t);return()=>clearTimeout(e)},[t])}function E(e,t,n){l.useEffect(()=>(window.addEventListener(e,t,n),()=>window.removeEventListener(e,t,n)),[e,t,n])}const D=(e,t)=>{let[n,r]=l.useState(t);return l.useEffect(()=>{let t=sessionStorage.getItem(e);t&&r(JSON.parse(t))},[e]),[n,l.useCallback(t=>{r(n=>{let r=typeof t==`function`?t(n):t;return sessionStorage.setItem(e,JSON.stringify(r)),r})},[e])]},O=(e,t)=>{let[n,r]=l.useState(t);return l.useEffect(()=>{let t=localStorage.getItem(e);t&&r(JSON.parse(t))},[e]),[n,l.useCallback(t=>{let i;i=typeof t==`function`?t(n):t,localStorage.setItem(e,JSON.stringify(i)),r(i)},[e])]},k=(e,t)=>{let[n,r]=l.useState(t);return l.useEffect(()=>{let t=new URLSearchParams(window.location.search).get(e);t!==null&&r(t)},[e]),[n,t=>{let n=new URLSearchParams(window.location.search);n.set(e,String(t)),window.history.pushState({},``,`${window.location.pathname}?${n}`),r(t)}]},A=e=>{let[t,n]=l.useState(null),r=l.useRef(null);return l.useLayoutEffect(()=>{let t=document.querySelector(e);if(!t)return;r.current!==t&&(r.current=t,n(t));let i=new ResizeObserver(()=>{r.current!==t&&(r.current=t,n(t))});return i.observe(t),()=>{i.disconnect()}},[e]),t};function j(){let[e,t]=l.useState(!1);return l.useEffect(()=>{t(!0)},[]),e}function M(){l.useLayoutEffect(()=>{let e=window.getComputedStyle(document.body).overflow;return document.body.style.overflow=`hidden`,()=>{document.body.style.overflow=e}},[])}function N({timeout:e=2e3}){let[t,n]=l.useState(!1);return{isCopied:t,copy:t=>{c.c(t,()=>{n(!0),setTimeout(()=>{n(!1)},e)})}}}const P=({blockIds:e=[],margin:t=0,substract:n=!0,dynamic:r=!1})=>{let[i,a]=l.useState(500),o=()=>{let r=e.reduce((e,t)=>e+(document.getElementById(t)?.clientHeight||0),0);a(n?window.innerHeight-r-t:r+t)};return w(()=>{if(o(),r){if(typeof r==`string`){let e=document.getElementById(r),t=new ResizeObserver(e=>{for(let t of e)o()});return e&&t.observe(e),()=>t?.disconnect()}return window.addEventListener(`resize`,o),()=>window.removeEventListener(`resize`,o)}},[]),i},F=({blockIds:e=[],margin:t=0,substract:n=!0,dynamic:r=!1,onChange:i})=>{let[a,o]=l.useState({height:500,width:500}),s=l.useCallback(()=>{let r=e.reduce((e,t)=>e+(document.getElementById(t)?.clientHeight||0),0),a=e.reduce((e,t)=>e+(document.getElementById(t)?.clientWidth||0),0),s=n?window.innerHeight-r-t:r+t,c=n?window.innerWidth-a-t:a+t;o(e=>e.height===s&&e.width===c?e:{height:s,width:c}),i?.({blocksWidth:a,blocksHeight:r,remainingWidth:c,remainingHeight:s})},[e,t,n,i]);return w(()=>{s();let t=[];if(e.length>0){let n=new MutationObserver(t=>{let n=!1;for(let r of t){for(let t of Array.from(r.addedNodes))if(t instanceof Element&&(e.includes(t.id)||e.some(e=>t.querySelector(`#${CSS.escape(e)}`)))){n=!0;break}if(n)break;for(let t of Array.from(r.removedNodes))if(t instanceof Element&&(e.includes(t.id)||e.some(e=>t.querySelector(`#${CSS.escape(e)}`)))){n=!0;break}if(n)break}n&&s()});n.observe(document.body,{childList:!0,subtree:!0}),t.push(()=>n.disconnect())}if(r)if(typeof r==`string`){let e=document.getElementById(r);if(e){let n=new ResizeObserver(s);n.observe(e),t.push(()=>n.unobserve(e)),t.push(()=>n.disconnect())}}else window.addEventListener(`resize`,s),t.push(()=>window.removeEventListener(`resize`,s));return()=>{for(let e of t)e()}},[s,r,e.join(`,`)]),a},I=()=>{let[e,t]=l.useState(!1),n=l.useRef(null),r=l.useRef(null);return x(()=>{let e=r.current;if(!e)return;let i=()=>{t(!0),n.current&&clearTimeout(n.current),n.current=setTimeout(()=>{t(!1)},150)};return i(),e.addEventListener(`scroll`,i),()=>{e.removeEventListener(`scroll`,i),n.current&&clearTimeout(n.current)}}),{isScrolling:e,scrollableContainerRef:r}},L=({offset:e}={})=>{let[t,n]=l.useState(!0),r=l.useRef(null);return x(()=>{let t=r.current;if(!t)return;let i=()=>{n(!(t.scrollTop>(e??10)))};return i(),t.addEventListener(`scroll`,i),()=>{t.removeEventListener(`scroll`,i)}}),{scrollableContainerRef:r,isAtTop:t}},R=({threshold:e=.1,root:t=null,rootMargin:n,onInteractionStart:r,onInteractionEnd:i}={})=>{let[a,o]=l.useState(!1),s=l.useRef(null);return l.useEffect(()=>{if(!s.current)return;let c=new IntersectionObserver(e=>{for(let t of e)t.isIntersecting?a||(r?.(),o(!0)):a&&(i?.(),o(!1))},{threshold:e,root:t,...n&&{rootMargin:n}});return c.observe(s.current),()=>{c.disconnect()}},[e,t,n,r,i,a]),{ref:s,isIntersecting:a}};Object.defineProperty(exports,`C`,{enumerable:!0,get:function(){return m}}),Object.defineProperty(exports,`D`,{enumerable:!0,get:function(){return s}}),Object.defineProperty(exports,`E`,{enumerable:!0,get:function(){return f}}),Object.defineProperty(exports,`S`,{enumerable:!0,get:function(){return _}}),Object.defineProperty(exports,`T`,{enumerable:!0,get:function(){return p}}),Object.defineProperty(exports,`_`,{enumerable:!0,get:function(){return T}}),Object.defineProperty(exports,`a`,{enumerable:!0,get:function(){return x}}),Object.defineProperty(exports,`b`,{enumerable:!0,get:function(){return E}}),Object.defineProperty(exports,`c`,{enumerable:!0,get:function(){return L}}),Object.defineProperty(exports,`d`,{enumerable:!0,get:function(){return w}}),Object.defineProperty(exports,`f`,{enumerable:!0,get:function(){return O}}),Object.defineProperty(exports,`g`,{enumerable:!0,get:function(){return D}}),Object.defineProperty(exports,`h`,{enumerable:!0,get:function(){return A}}),Object.defineProperty(exports,`i`,{enumerable:!0,get:function(){return F}}),Object.defineProperty(exports,`l`,{enumerable:!0,get:function(){return j}}),Object.defineProperty(exports,`m`,{enumerable:!0,get:function(){return b}}),Object.defineProperty(exports,`n`,{enumerable:!0,get:function(){return N}}),Object.defineProperty(exports,`o`,{enumerable:!0,get:function(){return P}}),Object.defineProperty(exports,`p`,{enumerable:!0,get:function(){return M}}),Object.defineProperty(exports,`r`,{enumerable:!0,get:function(){return C}}),Object.defineProperty(exports,`s`,{enumerable:!0,get:function(){return R}}),Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return y}}),Object.defineProperty(exports,`u`,{enumerable:!0,get:function(){return I}}),Object.defineProperty(exports,`v`,{enumerable:!0,get:function(){return S}}),Object.defineProperty(exports,`w`,{enumerable:!0,get:function(){return h}}),Object.defineProperty(exports,`x`,{enumerable:!0,get:function(){return v}}),Object.defineProperty(exports,`y`,{enumerable:!0,get:function(){return k}});
|
package/dist/hooks-CKCxXGDE.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{c as e,n as t}from"./functions-DdPaJAsm.js";import*as n from"react";import{Slot as r}from"@radix-ui/react-slot";import{jsx as i}from"react/jsx-runtime";const a=(e,t)=>{let[r,i]=n.useState(`idle`),[a,o]=n.useState(null),[s,c]=n.useState(null),[l,u]=n.useState(void 0),[d,f]=n.useTransition(),p=r===`idle`,m=r===`loading`||d,h=r===`success`,g=r===`error`,v=n.useCallback(e=>{o(e),i(`success`),t?.onSuccess?.(e),t?.onSettled?.()},[t]),y=n.useCallback(e=>{c(e),i(`error`),t?.onError?.(e),t?.onSettled?.()},[t]),b=n.useCallback(t=>{u(t),i(`loading`),c(null),f(()=>{e(t).then(v).catch(y)})},[e,v,y]);return{execute:b,executeAsync:n.useCallback(t=>new Promise((n,r)=>{u(t),i(`loading`),c(null),f(()=>{e(t).then(e=>{v(e),n(e)}).catch(e=>{y(e),r(e)})})}),[e,v,y]),reset:n.useCallback(()=>{i(`idle`),o(null),c(null),u(void 0)},[]),useExecute:e=>{_(()=>{b(e)},[])},data:a,error:s,input:l,isIdle:p,isLoading:m,isSuccess:h,isError:g}};function o(e,t={}){let{mode:r=`manual`,deps:i,onSuccess:a,onError:o,onSettled:s}=t,[c,l]=n.useState(void 0),[u,d]=n.useState(void 0),[f,p]=n.useState(`idle`),m=n.useRef(null),h=n.useCallback(a||(()=>{}),[a]),g=n.useCallback(o||(()=>{}),[o]),_=n.useCallback(s||(()=>{}),[s]),v=n.useCallback(async()=>{m.current&&m.current.abort(),m.current=new AbortController;let t=m.current.signal;p(`pending`),d(void 0);try{let n=await e(t);return t.aborted||(l(n),p(`success`),await h(n),await _(n,void 0)),n}catch(e){if(e instanceof Error&&e.name===`AbortError`)return;let n=e instanceof Error?e:Error(String(e)),r=n;throw t.aborted||(d(r),p(`error`),await g(r),await _(void 0,r)),n}},[e,h,g,_]);return n.useEffect(()=>{r===`auto`&&!i&&v()},[e,r,v,i]),n.useEffect(()=>{i&&r===`auto`&&v()},i||[]),n.useEffect(()=>()=>{m.current&&m.current.abort()},[]),{data:c,error:u,status:f,isIdle:f===`idle`,isPending:f===`pending`,isSuccess:f===`success`,isError:f===`error`,execute:v}}function s(e={}){let{timeout:r=1e4}=e;return n.useCallback(e=>{let i=()=>{try{n.startTransition(()=>{e()})}catch(e){console.log(`⚡[schedule.tsx] Failed: `,e)}};`requestIdleCallback`in window?requestIdleCallback(i,{timeout:r}):t(i)},[r])}function c(e,t=[],r={}){let i=s(r);n.useEffect(()=>{let t;return i(()=>{t=e()}),()=>{typeof t==`function`&&t?.()}},[i,...t])}const l=n.createContext(null),u=({children:e})=>{let t=n.useRef(null);return i(l.Provider,{value:t,children:i(r,{ref:t,children:e})})},d=({threshold:e=300,container:t}={})=>{let r=n.useContext(l),i=t??r,[a,o]=n.useState({scrolledPast:!1,direction:`forward`}),s=n.useRef(0),u=t=>{let n=t>s.current?`forward`:`backward`;o({scrolledPast:t>e,direction:n}),s.current=t};return c(()=>{let e=i?.current,t=e||window,n=e?`scrollTop`:`scrollY`,r=()=>{let e=t[n];u(e)};return t.addEventListener(`scroll`,r),r(),()=>t.removeEventListener(`scroll`,r)},[i,e]),a},f=(e=()=>alert(`clicked outside`))=>{let t=n.useRef(null),r=n=>{t.current&&!t.current.contains(n.target)&&e()};return n.useEffect(()=>(document.addEventListener(`mousedown`,r),document.addEventListener(`touchstart`,r),()=>{document.removeEventListener(`mousedown`,r),document.removeEventListener(`touchstart`,r)})),t};function p(e){let t=n.useMemo(()=>{switch(e){case`sm`:return`(min-width: 640px)`;case`md`:return`(min-width: 768px)`;case`lg`:return`(min-width: 1024px)`;case`xl`:return`(min-width: 1280px)`;case`2xl`:return`(min-width: 1536px)`;default:return e}},[e]),r=e=>typeof window<`u`?window.matchMedia(e).matches:!1,[i,a]=n.useState(r(t)),o=()=>{a(r(t))};return _(()=>{let e=window.matchMedia(t);return o(),e.addEventListener(`change`,o),()=>{e.removeEventListener(`change`,o)}},[t]),i}function m(e){n.useEffect(e,[])}function h(e,t){let r=n.useRef(!0);n.useEffect(()=>{if(r.current)r.current=!1;else return e()},t)}function g(e,t=500){let[r,i]=n.useState(e);return n.useEffect(()=>{let n=setTimeout(()=>i(e),t);return()=>{clearTimeout(n)}},[e,t]),r}const _=typeof window<`u`?n.useLayoutEffect:n.useEffect;function v(e,t=1e3){let r=n.useRef(e);_(()=>{r.current=e},[e]),n.useEffect(()=>{if(!t&&t!==0)return;let e=setTimeout(()=>r.current(),t);return()=>clearTimeout(e)},[t])}function y(e,t,r){n.useEffect(()=>(window.addEventListener(e,t,r),()=>window.removeEventListener(e,t,r)),[e,t,r])}const b=(e,t)=>{let[r,i]=n.useState(t);return n.useEffect(()=>{let t=sessionStorage.getItem(e);t&&i(JSON.parse(t))},[e]),[r,n.useCallback(t=>{i(n=>{let r=typeof t==`function`?t(n):t;return sessionStorage.setItem(e,JSON.stringify(r)),r})},[e])]},x=(e,t)=>{let[r,i]=n.useState(t);return n.useEffect(()=>{let t=localStorage.getItem(e);t&&i(JSON.parse(t))},[e]),[r,n.useCallback(t=>{let n;n=typeof t==`function`?t(r):t,localStorage.setItem(e,JSON.stringify(n)),i(n)},[e])]},S=(e,t)=>{let[r,i]=n.useState(t);return n.useEffect(()=>{let t=new URLSearchParams(window.location.search).get(e);t!==null&&i(t)},[e]),[r,t=>{let n=new URLSearchParams(window.location.search);n.set(e,String(t)),window.history.pushState({},``,`${window.location.pathname}?${n}`),i(t)}]},C=e=>{let[t,r]=n.useState(null),i=n.useRef(null);return n.useLayoutEffect(()=>{let t=document.querySelector(e);if(!t)return;i.current!==t&&(i.current=t,r(t));let n=new ResizeObserver(()=>{i.current!==t&&(i.current=t,r(t))});return n.observe(t),()=>{n.disconnect()}},[e]),t};function w(){let[e,t]=n.useState(!1);return n.useEffect(()=>{t(!0)},[]),e}function T(){n.useLayoutEffect(()=>{let e=window.getComputedStyle(document.body).overflow;return document.body.style.overflow=`hidden`,()=>{document.body.style.overflow=e}},[])}function E({timeout:t=2e3}){let[r,i]=n.useState(!1);return{isCopied:r,copy:n=>{e(n,()=>{i(!0),setTimeout(()=>{i(!1)},t)})}}}const D=({blockIds:e=[],margin:t=0,substract:r=!0,dynamic:i=!1})=>{let[a,o]=n.useState(500),s=()=>{let n=e.reduce((e,t)=>e+(document.getElementById(t)?.clientHeight||0),0);o(r?window.innerHeight-n-t:n+t)};return _(()=>{if(s(),i){if(typeof i==`string`){let e=document.getElementById(i),t=new ResizeObserver(e=>{for(let t of e)s()});return e&&t.observe(e),()=>t?.disconnect()}return window.addEventListener(`resize`,s),()=>window.removeEventListener(`resize`,s)}},[]),a},O=({blockIds:e=[],margin:t=0,substract:r=!0,dynamic:i=!1,onChange:a})=>{let[o,s]=n.useState({height:500,width:500}),c=n.useCallback(()=>{let n=e.reduce((e,t)=>e+(document.getElementById(t)?.clientHeight||0),0),i=e.reduce((e,t)=>e+(document.getElementById(t)?.clientWidth||0),0),o=r?window.innerHeight-n-t:n+t,c=r?window.innerWidth-i-t:i+t;s(e=>e.height===o&&e.width===c?e:{height:o,width:c}),a?.({blocksWidth:i,blocksHeight:n,remainingWidth:c,remainingHeight:o})},[e,t,r,a]);return _(()=>{c();let t=[];if(e.length>0){let n=new MutationObserver(t=>{let n=!1;for(let r of t){for(let t of Array.from(r.addedNodes))if(t instanceof Element&&(e.includes(t.id)||e.some(e=>t.querySelector(`#${CSS.escape(e)}`)))){n=!0;break}if(n)break;for(let t of Array.from(r.removedNodes))if(t instanceof Element&&(e.includes(t.id)||e.some(e=>t.querySelector(`#${CSS.escape(e)}`)))){n=!0;break}if(n)break}n&&c()});n.observe(document.body,{childList:!0,subtree:!0}),t.push(()=>n.disconnect())}if(i)if(typeof i==`string`){let e=document.getElementById(i);if(e){let n=new ResizeObserver(c);n.observe(e),t.push(()=>n.unobserve(e)),t.push(()=>n.disconnect())}}else window.addEventListener(`resize`,c),t.push(()=>window.removeEventListener(`resize`,c));return()=>{for(let e of t)e()}},[c,i,e.join(`,`)]),o},k=()=>{let[e,t]=n.useState(!1),r=n.useRef(null),i=n.useRef(null);return m(()=>{let e=i.current;if(!e)return;let n=()=>{t(!0),r.current&&clearTimeout(r.current),r.current=setTimeout(()=>{t(!1)},150)};return n(),e.addEventListener(`scroll`,n),()=>{e.removeEventListener(`scroll`,n),r.current&&clearTimeout(r.current)}}),{isScrolling:e,scrollableContainerRef:i}},A=({offset:e}={})=>{let[t,r]=n.useState(!0),i=n.useRef(null);return m(()=>{let t=i.current;if(!t)return;let n=()=>{r(!(t.scrollTop>(e??10)))};return n(),t.addEventListener(`scroll`,n),()=>{t.removeEventListener(`scroll`,n)}}),{scrollableContainerRef:i,isAtTop:t}},j=({threshold:e=.1,root:t=null,rootMargin:r,onInteractionStart:i,onInteractionEnd:a}={})=>{let[o,s]=n.useState(!1),c=n.useRef(null);return n.useEffect(()=>{if(!c.current)return;let n=new IntersectionObserver(e=>{for(let t of e)t.isIntersecting?o||(i?.(),s(!0)):o&&(a?.(),s(!1))},{threshold:e,root:t,...r&&{rootMargin:r}});return n.observe(c.current),()=>{n.disconnect()}},[e,t,r,i,a,o]),{ref:c,isIntersecting:o}};export{s as C,a as E,u as S,o as T,v as _,m as a,y as b,A as c,_ as d,x as f,b as g,C as h,O as i,w as l,p as m,E as n,D as o,T as p,g as r,j as s,f as t,k as u,h as v,c as w,d as x,S as y};
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
//#region src/functions/schedule.d.ts
|
|
2
|
-
type Task = () => Promise<void> | void;
|
|
3
|
-
interface ScheduleOpts {
|
|
4
|
-
retry?: number;
|
|
5
|
-
delay?: number;
|
|
6
|
-
timeout?: number;
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Runs a function asynchronously in the background.
|
|
10
|
-
* Returns immediately, retries on failure if configured.
|
|
11
|
-
* Logs total time taken.
|
|
12
|
-
*/
|
|
13
|
-
declare function schedule(task: Task, options?: ScheduleOpts): void;
|
|
14
|
-
//#endregion
|
|
15
|
-
export { Task as n, schedule as r, ScheduleOpts as t };
|