@ts-utilities/core 1.1.0 → 1.2.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.cjs +1 -1
- package/dist/index.d.cts +895 -915
- package/dist/index.d.ts +895 -915
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -142,1232 +142,1212 @@ type Hydrate<T> = T extends null ? undefined : T extends (infer U)[] ? Hydrate<U
|
|
|
142
142
|
*/
|
|
143
143
|
declare function hydrate<T>(data: T): Hydrate<T>;
|
|
144
144
|
//#endregion
|
|
145
|
-
//#region src/
|
|
146
|
-
/**
|
|
147
|
-
* Type representing a path split into segments
|
|
148
|
-
* @template S - The original path string type
|
|
149
|
-
*/
|
|
150
|
-
type SplitPath<S$1 extends string> = S$1 extends `${infer First}.${infer Rest}` ? [First, ...SplitPath<Rest>] : [S$1];
|
|
151
|
-
/**
|
|
152
|
-
* Recursive type to resolve nested object types based on path
|
|
153
|
-
* @template T - Current object type
|
|
154
|
-
* @template K - Array of path segments
|
|
155
|
-
*/
|
|
156
|
-
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;
|
|
145
|
+
//#region src/types/utilities.d.ts
|
|
157
146
|
/**
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
* @template
|
|
161
|
-
* @
|
|
162
|
-
* @param obj - Source object
|
|
163
|
-
* @param path - Array of path segments
|
|
164
|
-
* @param defaultValue - Fallback value if path not found
|
|
165
|
-
* @returns Value at path or default value
|
|
147
|
+
* Extracts the keys of an object type as a union type.
|
|
148
|
+
*
|
|
149
|
+
* @template T - The object type to extract keys from
|
|
150
|
+
* @returns A union of all keys in the object type
|
|
166
151
|
*
|
|
167
152
|
* @example
|
|
168
|
-
*
|
|
153
|
+
* ```ts
|
|
154
|
+
* type User = { name: string; age: number };
|
|
155
|
+
* type UserKeys = Keys<User>; // 'name' | 'age'
|
|
156
|
+
* ```
|
|
169
157
|
*/
|
|
170
|
-
|
|
158
|
+
type Keys<T extends object> = keyof T;
|
|
171
159
|
/**
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
* @template
|
|
175
|
-
* @
|
|
176
|
-
* @param path - Array of path segments
|
|
177
|
-
* @returns Value at path or undefined
|
|
160
|
+
* Extracts the values of an object type as a union type.
|
|
161
|
+
*
|
|
162
|
+
* @template T - The object type to extract values from
|
|
163
|
+
* @returns A union of all values in the object type
|
|
178
164
|
*
|
|
179
165
|
* @example
|
|
180
|
-
*
|
|
166
|
+
* ```ts
|
|
167
|
+
* type User = { name: string; age: number };
|
|
168
|
+
* type UserValues = Values<User>; // string | number
|
|
169
|
+
* ```
|
|
181
170
|
*/
|
|
182
|
-
|
|
171
|
+
type Values<T extends object> = T[keyof T];
|
|
183
172
|
/**
|
|
184
|
-
*
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
* @
|
|
190
|
-
* @
|
|
191
|
-
* @returns Value at path or default value
|
|
173
|
+
* Makes all properties of an object type optional recursively.
|
|
174
|
+
*
|
|
175
|
+
* This type traverses through nested objects and arrays, making all properties optional.
|
|
176
|
+
* Functions and primitives are left unchanged.
|
|
177
|
+
*
|
|
178
|
+
* @template T - The type to make deeply partial
|
|
179
|
+
* @returns A type with all properties optional recursively
|
|
192
180
|
*
|
|
193
181
|
* @example
|
|
194
|
-
*
|
|
182
|
+
* ```ts
|
|
183
|
+
* type Config = {
|
|
184
|
+
* server: { host: string; port: number };
|
|
185
|
+
* features: string[];
|
|
186
|
+
* };
|
|
187
|
+
*
|
|
188
|
+
* type PartialConfig = DeepPartial<Config>;
|
|
189
|
+
* // {
|
|
190
|
+
* // server?: { host?: string; port?: number };
|
|
191
|
+
* // features?: string[];
|
|
192
|
+
* // }
|
|
193
|
+
* ```
|
|
195
194
|
*/
|
|
196
|
-
|
|
195
|
+
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;
|
|
197
196
|
/**
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
* @template
|
|
201
|
-
* @
|
|
202
|
-
* @
|
|
203
|
-
* @returns Value at path or undefined
|
|
197
|
+
* Makes only specified properties of an object type optional.
|
|
198
|
+
*
|
|
199
|
+
* @template T - The base object type
|
|
200
|
+
* @template K - The keys to make optional
|
|
201
|
+
* @returns An object type with specified properties optional
|
|
204
202
|
*
|
|
205
203
|
* @example
|
|
206
|
-
*
|
|
204
|
+
* ```ts
|
|
205
|
+
* type User = { name: string; age: number; email: string };
|
|
206
|
+
* type PartialUser = SelectivePartial<User, 'age' | 'email'>;
|
|
207
|
+
* // { name: string; age?: number; email?: string }
|
|
208
|
+
* ```
|
|
207
209
|
*/
|
|
208
|
-
|
|
210
|
+
type SelectivePartial<T, K$1 extends keyof T> = Omit<T, K$1> & Partial<Pick<T, K$1>>;
|
|
209
211
|
/**
|
|
210
|
-
*
|
|
211
|
-
* preserving the original type information.
|
|
212
|
-
*
|
|
213
|
-
* Works with both plain objects and callable functions since
|
|
214
|
-
* functions in JavaScript are objects too. Also handles nullable types.
|
|
215
|
-
*
|
|
216
|
-
* @template T The base object or function type (can be null/undefined)
|
|
217
|
-
* @template P The additional properties type
|
|
212
|
+
* Makes all properties of an object type required recursively.
|
|
218
213
|
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
214
|
+
* This type traverses through nested objects and arrays, making all properties required.
|
|
215
|
+
* Functions and primitives are left unchanged.
|
|
221
216
|
*
|
|
222
|
-
* @
|
|
217
|
+
* @template T - The type to make deeply required
|
|
218
|
+
* @returns A type with all properties required recursively
|
|
223
219
|
*
|
|
224
220
|
* @example
|
|
225
221
|
* ```ts
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
* // Extend a function with metadata
|
|
231
|
-
* const fetchData = (url: string) => fetch(url).then(r => r.json());
|
|
232
|
-
* const enhancedFetch = extendProps(fetchData, {
|
|
233
|
-
* description: 'Data fetching utility',
|
|
234
|
-
* version: '1.0'
|
|
235
|
-
* });
|
|
236
|
-
* // enhancedFetch is callable and has description/version properties
|
|
222
|
+
* type PartialConfig = {
|
|
223
|
+
* server?: { host?: string; port?: number };
|
|
224
|
+
* };
|
|
237
225
|
*
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
*
|
|
226
|
+
* type RequiredConfig = DeepRequired<PartialConfig>;
|
|
227
|
+
* // {
|
|
228
|
+
* // server: { host: string; port: number };
|
|
229
|
+
* // }
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
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;
|
|
233
|
+
/**
|
|
234
|
+
* Makes only specified properties of an object type required.
|
|
243
235
|
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
* theme: 'dark',
|
|
248
|
-
* notifications: true
|
|
249
|
-
* });
|
|
236
|
+
* @template T - The base object type
|
|
237
|
+
* @template K - The keys to make required
|
|
238
|
+
* @returns An object type with specified properties required
|
|
250
239
|
*
|
|
251
|
-
*
|
|
252
|
-
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```ts
|
|
242
|
+
* type PartialUser = { name?: string; age?: number; email?: string };
|
|
243
|
+
* type RequiredUser = SelectiveRequired<PartialUser, 'name'>;
|
|
244
|
+
* // { name: string; age?: number; email?: string }
|
|
255
245
|
* ```
|
|
256
246
|
*/
|
|
257
|
-
|
|
258
|
-
//#endregion
|
|
259
|
-
//#region src/functions/poll.d.ts
|
|
247
|
+
type SelectiveRequired<T, K$1 extends keyof T> = Omit<T, K$1> & Required<Pick<T, K$1>>;
|
|
260
248
|
/**
|
|
261
|
-
*
|
|
262
|
-
* or until the operation times out or is aborted.
|
|
263
|
-
*
|
|
264
|
-
* Designed for waiting on async jobs, external state, or delayed availability.
|
|
249
|
+
* Creates a type where all properties are never (useful for excluding types).
|
|
265
250
|
*
|
|
266
|
-
*
|
|
251
|
+
* This can be used to create mutually exclusive types or to exclude certain properties.
|
|
267
252
|
*
|
|
268
|
-
* @
|
|
269
|
-
*
|
|
270
|
-
* - a truthy value `T` → stop polling and return it
|
|
271
|
-
* - falsy/null/undefined → continue polling
|
|
253
|
+
* @template T - The object type to transform
|
|
254
|
+
* @returns An object type with all properties set to never
|
|
272
255
|
*
|
|
273
|
-
* @
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
|
|
256
|
+
* @example
|
|
257
|
+
* ```ts
|
|
258
|
+
* type User = { name: string; age: number };
|
|
259
|
+
* type ExcludedUser = Never<User>; // { name: never; age: never }
|
|
260
|
+
* ```
|
|
261
|
+
*/
|
|
262
|
+
type Never<T> = { [K in keyof T]: never };
|
|
263
|
+
/**
|
|
264
|
+
* Makes all properties of an object type nullable recursively.
|
|
279
265
|
*
|
|
280
|
-
* @
|
|
281
|
-
*
|
|
282
|
-
* Throws `AbortError` if aborted
|
|
266
|
+
* @template T - The type to make nullable
|
|
267
|
+
* @returns A type where all properties can be null
|
|
283
268
|
*
|
|
284
269
|
* @example
|
|
285
270
|
* ```ts
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
*
|
|
289
|
-
* return status === 'done' ? status : null;
|
|
290
|
-
* }, { interval: 3000, timeout: 60000 });
|
|
271
|
+
* type User = { name: string; profile: { age: number } };
|
|
272
|
+
* type NullableUser = Nullable<User>;
|
|
273
|
+
* // { name: string | null; profile: { age: number | null } | null }
|
|
291
274
|
* ```
|
|
275
|
+
*/
|
|
276
|
+
type Nullable<T> = T extends object ? { [P in keyof T]: Nullable<T[P]> } : T | null;
|
|
277
|
+
/**
|
|
278
|
+
* Makes all properties of an object type optional (undefined) recursively.
|
|
279
|
+
*
|
|
280
|
+
* @template T - The type to make optional
|
|
281
|
+
* @returns A type where all properties can be undefined
|
|
292
282
|
*
|
|
293
283
|
* @example
|
|
294
284
|
* ```ts
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
*
|
|
298
|
-
* await fetch('/api/health');
|
|
299
|
-
* return true;
|
|
300
|
-
* } catch {
|
|
301
|
-
* return null;
|
|
302
|
-
* }
|
|
303
|
-
* }, { interval: 1000, timeout: 30000 });
|
|
285
|
+
* type User = { name: string; profile: { age: number } };
|
|
286
|
+
* type OptionalUser = Optional<User>;
|
|
287
|
+
* // { name: string | undefined; profile: { age: number | undefined } | undefined }
|
|
304
288
|
* ```
|
|
289
|
+
*/
|
|
290
|
+
type Optional<T> = T extends object ? { [P in keyof T]: Optional<T[P]> } : T | undefined;
|
|
291
|
+
/**
|
|
292
|
+
* Makes all properties of an object type nullish (null or undefined) recursively.
|
|
293
|
+
*
|
|
294
|
+
* @template T - The type to make nullish
|
|
295
|
+
* @returns A type where all properties can be null or undefined
|
|
305
296
|
*
|
|
306
297
|
* @example
|
|
307
298
|
* ```ts
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
*
|
|
312
|
-
* try {
|
|
313
|
-
* const result = await poll(
|
|
314
|
-
* () => checkExternalService(),
|
|
315
|
-
* { interval: 2000, signal: controller.signal }
|
|
316
|
-
* );
|
|
317
|
-
* } catch (err) {
|
|
318
|
-
* if (err.name === 'AbortError') {
|
|
319
|
-
* console.log('Polling was cancelled');
|
|
320
|
-
* }
|
|
321
|
-
* }
|
|
299
|
+
* type User = { name: string; profile: { age: number } };
|
|
300
|
+
* type NullishUser = Nullish<User>;
|
|
301
|
+
* // { name: string | null | undefined; profile: { age: number | null | undefined } | null | undefined }
|
|
322
302
|
* ```
|
|
303
|
+
*/
|
|
304
|
+
type Nullish<T> = T extends object ? { [P in keyof T]: Nullish<T[P]> } : T | null | undefined;
|
|
305
|
+
/**
|
|
306
|
+
* Makes all properties of an object type optional and nullish recursively.
|
|
307
|
+
*
|
|
308
|
+
* This combines optional properties with nullish values.
|
|
309
|
+
*
|
|
310
|
+
* @template T - The type to make maybe
|
|
311
|
+
* @returns A type where all properties are optional and can be null or undefined
|
|
323
312
|
*
|
|
324
313
|
* @example
|
|
325
314
|
* ```ts
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
* return confirmations.length > 0 ? confirmations[0] : null;
|
|
330
|
-
* }, { interval: 5000, timeout: 300000 }); // 5 min timeout
|
|
315
|
+
* type User = { name: string; profile: { age: number } };
|
|
316
|
+
* type MaybeUser = Maybe<User>;
|
|
317
|
+
* // { name?: string | null | undefined; profile?: { age?: number | null | undefined } | null | undefined }
|
|
331
318
|
* ```
|
|
332
319
|
*/
|
|
333
|
-
|
|
334
|
-
interval,
|
|
335
|
-
timeout,
|
|
336
|
-
jitter,
|
|
337
|
-
signal
|
|
338
|
-
}?: Partial<{
|
|
339
|
-
interval: number;
|
|
340
|
-
timeout: number;
|
|
341
|
-
signal: AbortSignal;
|
|
342
|
-
jitter: boolean;
|
|
343
|
-
}>): Promise<T>;
|
|
344
|
-
//#endregion
|
|
345
|
-
//#region src/functions/promise.d.ts
|
|
346
|
-
type TaskType<T> = Promise<T> | (() => Promise<T>);
|
|
347
|
-
type AwaitedTuple<T extends readonly TaskType<any>[] | []> = { -readonly [P in keyof T]: Awaited<T[P] extends (() => Promise<infer R>) ? R : T[P]> };
|
|
348
|
-
type ObjectValues<T extends Record<string, TaskType<any>>> = { [K in keyof T]: Awaited<T[K] extends (() => Promise<infer R>) ? R : T[K]> };
|
|
349
|
-
type ConcurrenceResult<T> = {
|
|
350
|
-
results: T;
|
|
351
|
-
errors: Error[];
|
|
352
|
-
succeeded: number;
|
|
353
|
-
failed: number;
|
|
354
|
-
duration: number;
|
|
355
|
-
};
|
|
356
|
-
type ConcurrenceOptions = {
|
|
357
|
-
concurrency?: number;
|
|
358
|
-
timeout?: number;
|
|
359
|
-
signal?: AbortSignal;
|
|
360
|
-
retry?: number;
|
|
361
|
-
retryDelay?: number;
|
|
362
|
-
throwOnFirstError?: boolean;
|
|
363
|
-
ignoreErrors?: boolean;
|
|
364
|
-
};
|
|
365
|
-
declare function withConcurrency<T extends readonly TaskType<any>[] | []>(tasks: T, options?: ConcurrenceOptions): Promise<ConcurrenceResult<AwaitedTuple<T>>>;
|
|
366
|
-
declare function withConcurrency<T extends Record<string, TaskType<any>>>(tasks: T, options?: ConcurrenceOptions): Promise<ConcurrenceResult<ObjectValues<T>>>;
|
|
367
|
-
//#endregion
|
|
368
|
-
//#region src/functions/schedule.d.ts
|
|
369
|
-
/**
|
|
370
|
-
* A task function that can be synchronous or asynchronous.
|
|
371
|
-
*/
|
|
372
|
-
type Task = () => Promise<void> | void;
|
|
373
|
-
/**
|
|
374
|
-
* Options for configuring the schedule function.
|
|
375
|
-
*/
|
|
376
|
-
interface ScheduleOpts {
|
|
377
|
-
/** Number of retry attempts on failure. Defaults to 0. */
|
|
378
|
-
retry?: number;
|
|
379
|
-
/** Delay in milliseconds between retries. Defaults to 0. */
|
|
380
|
-
delay?: number;
|
|
381
|
-
/** Maximum time in milliseconds to wait for the task to complete. */
|
|
382
|
-
timeout?: number;
|
|
383
|
-
}
|
|
320
|
+
type Maybe<T> = T extends object ? { [P in keyof T]?: Nullish<T[P]> } : T | null | undefined;
|
|
384
321
|
/**
|
|
385
|
-
*
|
|
322
|
+
* Makes all properties of an object type readonly recursively.
|
|
386
323
|
*
|
|
387
|
-
*
|
|
388
|
-
*
|
|
389
|
-
* Logs execution time and retry attempts to the console.
|
|
324
|
+
* This type traverses through nested objects and arrays, making all properties readonly.
|
|
325
|
+
* Functions and primitives are left unchanged.
|
|
390
326
|
*
|
|
391
|
-
* @
|
|
392
|
-
* @
|
|
327
|
+
* @template T - The type to make deeply readonly
|
|
328
|
+
* @returns A type with all properties readonly recursively
|
|
393
329
|
*
|
|
394
330
|
* @example
|
|
395
331
|
* ```ts
|
|
396
|
-
*
|
|
397
|
-
*
|
|
398
|
-
*
|
|
399
|
-
* }
|
|
332
|
+
* type Config = {
|
|
333
|
+
* server: { host: string; port: number };
|
|
334
|
+
* features: string[];
|
|
335
|
+
* };
|
|
400
336
|
*
|
|
401
|
-
*
|
|
402
|
-
*
|
|
403
|
-
*
|
|
404
|
-
*
|
|
405
|
-
*
|
|
337
|
+
* type ReadonlyConfig = DeepReadonly<Config>;
|
|
338
|
+
* // {
|
|
339
|
+
* // readonly server: { readonly host: string; readonly port: number };
|
|
340
|
+
* // readonly features: readonly string[];
|
|
341
|
+
* // }
|
|
406
342
|
* ```
|
|
407
343
|
*/
|
|
408
|
-
|
|
409
|
-
//#endregion
|
|
410
|
-
//#region src/functions/shield.d.ts
|
|
344
|
+
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;
|
|
411
345
|
/**
|
|
412
|
-
*
|
|
346
|
+
* Removes readonly modifier from all properties of an object type recursively.
|
|
413
347
|
*
|
|
414
|
-
*
|
|
415
|
-
*
|
|
416
|
-
* - `data`: the resolved value (if successful), otherwise `null`
|
|
348
|
+
* @template T - The readonly type to make mutable
|
|
349
|
+
* @returns A type with all readonly modifiers removed
|
|
417
350
|
*
|
|
418
351
|
* @example
|
|
419
352
|
* ```ts
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
* return 'success';
|
|
424
|
-
* });
|
|
425
|
-
* if (err) {
|
|
426
|
-
* console.error('Operation failed:', err);
|
|
427
|
-
* } else {
|
|
428
|
-
* console.log('Result:', value);
|
|
429
|
-
* }
|
|
430
|
-
*
|
|
431
|
-
* // Asynchronous error handling
|
|
432
|
-
* const [asyncErr, result] = await shield(async () => {
|
|
433
|
-
* const response = await fetch('/api/data');
|
|
434
|
-
* if (!response.ok) throw new Error('API error');
|
|
435
|
-
* return response.json();
|
|
436
|
-
* });
|
|
437
|
-
* if (asyncErr) {
|
|
438
|
-
* console.error('API call failed:', asyncErr);
|
|
439
|
-
* } else {
|
|
440
|
-
* processData(result);
|
|
441
|
-
* }
|
|
442
|
-
*
|
|
443
|
-
* // API calls with fallbacks
|
|
444
|
-
* const [fetchErr, data] = await shield(fetchUserData(userId));
|
|
445
|
-
* const userData = fetchErr ? getCachedUserData(userId) : data;
|
|
446
|
-
*
|
|
447
|
-
* // File operations
|
|
448
|
-
* const [fileErr, content] = shield(() => readFileSync('config.json'));
|
|
449
|
-
* if (fileErr) {
|
|
450
|
-
* console.warn('Could not read config, using defaults');
|
|
451
|
-
* return defaultConfig;
|
|
452
|
-
* }
|
|
353
|
+
* type ReadonlyUser = { readonly name: string; readonly profile: { readonly age: number } };
|
|
354
|
+
* type MutableUser = Mutable<ReadonlyUser>;
|
|
355
|
+
* // { name: string; profile: { age: number } }
|
|
453
356
|
* ```
|
|
357
|
+
*/
|
|
358
|
+
type Mutable<T> = { -readonly [P in keyof T]: T[P] };
|
|
359
|
+
/**
|
|
360
|
+
* Extracts keys of an object type that have values of a specific type.
|
|
361
|
+
*
|
|
362
|
+
* @template T - The object type to search
|
|
363
|
+
* @template U - The value type to match
|
|
364
|
+
* @returns A union of keys whose values match the specified type
|
|
454
365
|
*
|
|
455
366
|
* @example
|
|
456
367
|
* ```ts
|
|
457
|
-
*
|
|
458
|
-
*
|
|
459
|
-
*
|
|
460
|
-
* if (err) {
|
|
461
|
-
* await logError(err);
|
|
462
|
-
* return null;
|
|
463
|
-
* }
|
|
464
|
-
* return result;
|
|
465
|
-
* }
|
|
466
|
-
*
|
|
467
|
-
* // In event handlers
|
|
468
|
-
* function handleSubmit(formData) {
|
|
469
|
-
* const [validationErr, validatedData] = shield(() => validateForm(formData));
|
|
470
|
-
* if (validationErr) {
|
|
471
|
-
* showValidationError(validationErr);
|
|
472
|
-
* return;
|
|
473
|
-
* }
|
|
474
|
-
* submitData(validatedData);
|
|
475
|
-
* }
|
|
368
|
+
* type User = { name: string; age: number; active: boolean };
|
|
369
|
+
* type StringKeys = KeysOfType<User, string>; // 'name'
|
|
370
|
+
* type NumberKeys = KeysOfType<User, number>; // 'age'
|
|
476
371
|
* ```
|
|
477
372
|
*/
|
|
478
|
-
|
|
479
|
-
declare function shield<T, E = Error>(operation: () => T): [E | null, T | null];
|
|
480
|
-
//#endregion
|
|
481
|
-
//#region src/functions/utils-core.d.ts
|
|
373
|
+
type KeysOfType<T, U$1> = { [K in keyof T]: T[K] extends U$1 ? K : never }[keyof T];
|
|
482
374
|
/**
|
|
483
|
-
*
|
|
484
|
-
*
|
|
485
|
-
* Transforms technical naming conventions into human-readable titles by:
|
|
486
|
-
* - Adding spaces between words
|
|
487
|
-
* - Capitalizing the first letter of each word
|
|
488
|
-
* - Handling common separators (-, _, camelCase boundaries)
|
|
375
|
+
* Omits properties from an object type that have values of a specific type.
|
|
489
376
|
*
|
|
490
|
-
* @
|
|
491
|
-
* @
|
|
377
|
+
* @template T - The object type to filter
|
|
378
|
+
* @template U - The value type to exclude
|
|
379
|
+
* @returns An object type without properties of the specified value type
|
|
492
380
|
*
|
|
493
381
|
* @example
|
|
494
382
|
* ```ts
|
|
495
|
-
*
|
|
496
|
-
*
|
|
497
|
-
* convertToNormalCase('snake_case') // 'Snake Case'
|
|
498
|
-
* convertToNormalCase('PascalCase') // 'Pascal Case'
|
|
383
|
+
* type Mixed = { name: string; age: number; active: boolean };
|
|
384
|
+
* type WithoutStrings = OmitByType<Mixed, string>; // { age: number; active: boolean }
|
|
499
385
|
* ```
|
|
500
386
|
*/
|
|
501
|
-
|
|
502
|
-
/**
|
|
503
|
-
* Converts a string to a URL-friendly slug by trimming, converting to lowercase,
|
|
504
|
-
* replacing diacritics, removing invalid characters, and replacing spaces with hyphens.
|
|
505
|
-
* @param {string} [str] - The input string to convert.
|
|
506
|
-
* @returns {string} The generated slug.
|
|
507
|
-
* @example
|
|
508
|
-
* convertToSlug("Hello World!"); // "hello-world"
|
|
509
|
-
* convertToSlug("Déjà Vu"); // "deja-vu"
|
|
510
|
-
*/
|
|
511
|
-
declare const convertToSlug: (str: string) => string;
|
|
387
|
+
type OmitByType<T, U$1> = { [K in keyof T as T[K] extends U$1 ? never : K]: T[K] };
|
|
512
388
|
/**
|
|
513
|
-
*
|
|
389
|
+
* Makes specified properties required while keeping others as-is.
|
|
514
390
|
*
|
|
515
|
-
*
|
|
391
|
+
* @template T - The base object type
|
|
392
|
+
* @template K - The keys to make required
|
|
393
|
+
* @returns An object type with specified properties required
|
|
516
394
|
*
|
|
517
|
-
* @
|
|
518
|
-
*
|
|
519
|
-
*
|
|
395
|
+
* @example
|
|
396
|
+
* ```ts
|
|
397
|
+
* type PartialUser = { name?: string; age?: number; email?: string };
|
|
398
|
+
* type RequiredNameUser = RequiredKeys<PartialUser, 'name'>;
|
|
399
|
+
* // { name: string; age?: number; email?: string }
|
|
400
|
+
* ```
|
|
520
401
|
*/
|
|
521
|
-
|
|
522
|
-
type DebouncedFunction<F$1 extends (...args: any[]) => any> = {
|
|
523
|
-
(...args: Parameters<F$1>): ReturnType<F$1> | undefined;
|
|
524
|
-
readonly isPending: boolean;
|
|
525
|
-
};
|
|
402
|
+
type RequiredKeys<T, K$1 extends keyof T> = Omit<T, K$1> & Required<Pick<T, K$1>>;
|
|
526
403
|
/**
|
|
527
|
-
*
|
|
528
|
-
* after the specified `wait` time has elapsed since the last invocation.
|
|
404
|
+
* Computes the symmetric difference between two object types.
|
|
529
405
|
*
|
|
530
|
-
*
|
|
531
|
-
* on the leading edge of the wait interval. Subsequent calls during the wait interval
|
|
532
|
-
* will reset the timer but not invoke the function until the interval elapses again.
|
|
406
|
+
* Properties that exist in either T or U but not in both.
|
|
533
407
|
*
|
|
534
|
-
*
|
|
535
|
-
*
|
|
408
|
+
* @template T - First object type
|
|
409
|
+
* @template U - Second object type
|
|
410
|
+
* @returns Properties unique to T or U
|
|
536
411
|
*
|
|
537
|
-
* @
|
|
412
|
+
* @example
|
|
413
|
+
* ```ts
|
|
414
|
+
* type A = { x: number; y: string };
|
|
415
|
+
* type B = { y: string; z: boolean };
|
|
416
|
+
* type DiffAB = Diff<A, B>; // { x: number; z: boolean }
|
|
417
|
+
* ```
|
|
418
|
+
*/
|
|
419
|
+
type Diff<T, U$1> = Omit<T, keyof U$1> & Omit<U$1, keyof T>;
|
|
420
|
+
/**
|
|
421
|
+
* Computes the intersection of two object types (properties present in both).
|
|
538
422
|
*
|
|
539
|
-
* @
|
|
540
|
-
* @
|
|
541
|
-
* @
|
|
542
|
-
* - `immediate` (boolean): If `true`, invokes the function on the leading edge
|
|
543
|
-
* of the wait interval instead of the trailing edge.
|
|
423
|
+
* @template T - First object type
|
|
424
|
+
* @template U - Second object type
|
|
425
|
+
* @returns Properties that exist in both T and U
|
|
544
426
|
*
|
|
545
|
-
* @
|
|
427
|
+
* @example
|
|
428
|
+
* ```ts
|
|
429
|
+
* type A = { x: number; y: string };
|
|
430
|
+
* type B = { y: string; z: boolean };
|
|
431
|
+
* type IntersectionAB = Intersection<A, B>; // { y: string }
|
|
432
|
+
* ```
|
|
433
|
+
*/
|
|
434
|
+
type Intersection<T extends object, U$1 extends object> = Pick<T, Extract<keyof T, keyof U$1> & Extract<keyof U$1, keyof T>>;
|
|
435
|
+
/**
|
|
436
|
+
* Merges two object types, combining their properties.
|
|
546
437
|
*
|
|
547
|
-
* @
|
|
548
|
-
* @
|
|
438
|
+
* @template T - First object type
|
|
439
|
+
* @template U - Second object type
|
|
440
|
+
* @returns A merged object type with properties from both
|
|
549
441
|
*
|
|
550
442
|
* @example
|
|
551
443
|
* ```ts
|
|
552
|
-
*
|
|
553
|
-
*
|
|
554
|
-
*
|
|
555
|
-
* console.log(log.isPending); // true if the timer is active.
|
|
556
|
-
*
|
|
557
|
-
* // Immediate execution
|
|
558
|
-
* const save = debounce(() => saveToServer(), 500, { immediate: true });
|
|
559
|
-
* save(); // Executes immediately, then waits 500ms for subsequent calls
|
|
560
|
-
*
|
|
561
|
-
* // Check pending state
|
|
562
|
-
* const debouncedSearch = debounce(searchAPI, 300);
|
|
563
|
-
* debouncedSearch('query');
|
|
564
|
-
* if (debouncedSearch.isPending) {
|
|
565
|
-
* showLoadingIndicator();
|
|
566
|
-
* }
|
|
444
|
+
* type A = { x: number; y: string };
|
|
445
|
+
* type B = { y: boolean; z: string };
|
|
446
|
+
* type Merged = Merge<A, B>; // { x: number; y: boolean; z: string }
|
|
567
447
|
* ```
|
|
568
448
|
*/
|
|
569
|
-
|
|
570
|
-
immediate: boolean;
|
|
571
|
-
}): DebouncedFunction<F$1>;
|
|
572
|
-
type ThrottledFunction<F$1 extends (...args: any[]) => any> = {
|
|
573
|
-
(...args: Parameters<F$1>): ReturnType<F$1> | undefined;
|
|
574
|
-
readonly isPending: boolean;
|
|
575
|
-
};
|
|
449
|
+
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>;
|
|
576
450
|
/**
|
|
577
|
-
*
|
|
578
|
-
* every `wait` milliseconds.
|
|
579
|
-
*
|
|
580
|
-
* If the `leading` option is set to `true`, the function will be invoked immediately
|
|
581
|
-
* on the leading edge of the throttle interval. If the `trailing` option is set to `true`,
|
|
582
|
-
* the function will also be invoked at the end of the throttle interval if additional
|
|
583
|
-
* calls were made during the interval.
|
|
584
|
-
*
|
|
585
|
-
* The returned function includes the `isPending` property to check if the throttle
|
|
586
|
-
* timer is currently active.
|
|
587
|
-
*
|
|
588
|
-
* @typeParam F - The type of the function to throttle.
|
|
589
|
-
*
|
|
590
|
-
* @param function_ - The function to throttle.
|
|
591
|
-
* @param wait - The number of milliseconds to wait between invocations (default is 100ms).
|
|
592
|
-
* @param options - An optional object with the following properties:
|
|
593
|
-
* - `leading` (boolean): If `true`, invokes the function on the leading edge of the interval.
|
|
594
|
-
* - `trailing` (boolean): If `true`, invokes the function on the trailing edge of the interval.
|
|
595
|
-
*
|
|
596
|
-
* @returns A throttled version of the provided function, enhanced with the `isPending` property.
|
|
451
|
+
* Subtracts properties of one object type from another.
|
|
597
452
|
*
|
|
598
|
-
* @
|
|
599
|
-
* @
|
|
453
|
+
* @template T - The object type to subtract from
|
|
454
|
+
* @template U - The object type whose properties to subtract
|
|
455
|
+
* @returns T without properties that exist in U
|
|
600
456
|
*
|
|
601
457
|
* @example
|
|
602
458
|
* ```ts
|
|
603
|
-
*
|
|
604
|
-
*
|
|
605
|
-
*
|
|
606
|
-
* log('World'); // Ignored for 200ms
|
|
607
|
-
* console.log(log.isPending); // true if within throttle window
|
|
608
|
-
*
|
|
609
|
-
* // Trailing edge only
|
|
610
|
-
* const trailingLog = throttle(() => console.log('trailing'), 200, {
|
|
611
|
-
* leading: false,
|
|
612
|
-
* trailing: true
|
|
613
|
-
* });
|
|
614
|
-
* trailingLog(); // No immediate execution
|
|
615
|
-
* // After 200ms: logs "trailing"
|
|
616
|
-
*
|
|
617
|
-
* // Both edges
|
|
618
|
-
* const bothLog = throttle(() => console.log('both'), 200, {
|
|
619
|
-
* leading: true,
|
|
620
|
-
* trailing: true
|
|
621
|
-
* });
|
|
622
|
-
* bothLog(); // Immediate execution
|
|
623
|
-
* // After 200ms: executes again if called during window
|
|
459
|
+
* type A = { x: number; y: string; z: boolean };
|
|
460
|
+
* type B = { y: string };
|
|
461
|
+
* type Subtracted = Substract<A, B>; // { x: number; z: boolean }
|
|
624
462
|
* ```
|
|
625
463
|
*/
|
|
626
|
-
|
|
627
|
-
leading?: boolean;
|
|
628
|
-
trailing?: boolean;
|
|
629
|
-
}): ThrottledFunction<F$1>;
|
|
464
|
+
type Substract<T extends object, U$1 extends object> = Omit<T, keyof U$1>;
|
|
630
465
|
/**
|
|
631
|
-
*
|
|
466
|
+
* Represents either all properties present or none of them.
|
|
632
467
|
*
|
|
633
|
-
*
|
|
634
|
-
* variadic arguments and array-based argument passing. Extra placeholders
|
|
635
|
-
* are left as-is, missing arguments result in empty strings.
|
|
468
|
+
* Useful for creating mutually exclusive configurations.
|
|
636
469
|
*
|
|
637
|
-
* @
|
|
638
|
-
* @
|
|
639
|
-
* @returns The formatted string with placeholders replaced by arguments.
|
|
470
|
+
* @template T - The object type
|
|
471
|
+
* @returns Either the full object or an empty object with optional properties
|
|
640
472
|
*
|
|
641
473
|
* @example
|
|
642
474
|
* ```ts
|
|
643
|
-
*
|
|
644
|
-
*
|
|
645
|
-
*
|
|
646
|
-
* // Using array of arguments
|
|
647
|
-
* printf("%s love %s", ["I", "Bangladesh"]) // "I love Bangladesh"
|
|
648
|
-
*
|
|
649
|
-
* // Extra placeholders remain unchanged
|
|
650
|
-
* printf("%s %s %s", "Hello", "World") // "Hello World %s"
|
|
651
|
-
*
|
|
652
|
-
* // Missing arguments become empty strings
|
|
653
|
-
* printf("%s and %s", "this") // "this and "
|
|
654
|
-
*
|
|
655
|
-
* // Multiple occurrences
|
|
656
|
-
* printf("%s %s %s", "repeat", "repeat", "repeat") // "repeat repeat repeat"
|
|
475
|
+
* type Config = { host: string; port: number };
|
|
476
|
+
* type AllOrNoneConfig = AllOrNone<Config>;
|
|
477
|
+
* // { host: string; port: number } | {}
|
|
657
478
|
* ```
|
|
658
479
|
*/
|
|
659
|
-
|
|
480
|
+
type AllOrNone<T> = T | { [P in keyof T]?: never };
|
|
660
481
|
/**
|
|
661
|
-
*
|
|
662
|
-
*
|
|
663
|
-
* @param str - The string to escape
|
|
664
|
-
* @returns - The escaped string safe for use in RegExp constructor
|
|
665
|
-
*
|
|
666
|
-
* @example
|
|
667
|
-
* ```ts
|
|
668
|
-
* const escapedString = escapeRegExp('Hello, world!');
|
|
669
|
-
* // escapedString === 'Hello\\, world!'
|
|
482
|
+
* Represents exactly one property from an object type being present.
|
|
670
483
|
*
|
|
671
|
-
*
|
|
672
|
-
* ```
|
|
673
|
-
*/
|
|
674
|
-
declare function escapeRegExp(str: string): string;
|
|
675
|
-
/**
|
|
676
|
-
* Normalizes a string by:
|
|
677
|
-
* - Applying Unicode normalization (NFC)
|
|
678
|
-
* - Optionally removing diacritic marks (accents)
|
|
679
|
-
* - Optionally trimming leading/trailing non-alphanumeric characters
|
|
680
|
-
* - Optionally converting to lowercase
|
|
484
|
+
* Useful for creating discriminated unions or mutually exclusive options.
|
|
681
485
|
*
|
|
682
|
-
* @
|
|
683
|
-
* @
|
|
684
|
-
* @param options.lowercase - Whether to convert the result to lowercase (default: true)
|
|
685
|
-
* @param options.removeAccents - Whether to remove diacritic marks like accents (default: true)
|
|
686
|
-
* @param options.removeNonAlphanumeric - Whether to trim non-alphanumeric characters from the edges (default: true)
|
|
687
|
-
* @returns The normalized string
|
|
486
|
+
* @template T - The object type
|
|
487
|
+
* @returns A union where only one property is present at a time
|
|
688
488
|
*
|
|
689
489
|
* @example
|
|
690
490
|
* ```ts
|
|
691
|
-
*
|
|
692
|
-
*
|
|
693
|
-
*
|
|
491
|
+
* type Action = { type: 'create'; payload: string } | { type: 'update'; id: number };
|
|
492
|
+
* type OneAction = OneOf<Action>;
|
|
493
|
+
* // { type: 'create'; payload: string } | { type: 'update'; id: number }
|
|
694
494
|
* ```
|
|
695
495
|
*/
|
|
696
|
-
|
|
697
|
-
lowercase?: boolean;
|
|
698
|
-
removeAccents?: boolean;
|
|
699
|
-
removeNonAlphanumeric?: boolean;
|
|
700
|
-
}): string;
|
|
701
|
-
//#endregion
|
|
702
|
-
//#region src/types/utilities.d.ts
|
|
496
|
+
type OneOf<T> = { [K in keyof T]: Pick<T, K> }[keyof T];
|
|
703
497
|
/**
|
|
704
|
-
*
|
|
498
|
+
* Represents exactly two properties from an object type being present.
|
|
705
499
|
*
|
|
706
|
-
* @template T - The object type
|
|
707
|
-
* @returns A union
|
|
500
|
+
* @template T - The object type
|
|
501
|
+
* @returns A union where exactly two properties are present at a time
|
|
708
502
|
*
|
|
709
503
|
* @example
|
|
710
504
|
* ```ts
|
|
711
|
-
* type
|
|
712
|
-
* type
|
|
505
|
+
* type Config = { a: number; b: string; c: boolean };
|
|
506
|
+
* type TwoConfig = TwoOf<Config>;
|
|
507
|
+
* // { a: number; b: string } | { a: number; c: boolean } | { b: string; c: boolean }
|
|
713
508
|
* ```
|
|
714
509
|
*/
|
|
715
|
-
type
|
|
510
|
+
type TwoOf<T> = { [K in keyof T]: { [L in Exclude<keyof T, K>]: Pick<T, K | L> }[Exclude<keyof T, K>] }[keyof T];
|
|
716
511
|
/**
|
|
717
|
-
*
|
|
512
|
+
* Prettifies a complex type by expanding it for better readability in tooltips.
|
|
718
513
|
*
|
|
719
|
-
*
|
|
720
|
-
*
|
|
514
|
+
* This type doesn't change the runtime type but helps with IntelliSense display.
|
|
515
|
+
*
|
|
516
|
+
* @template T - The type to prettify
|
|
517
|
+
* @returns The same type but expanded for better readability
|
|
721
518
|
*
|
|
722
519
|
* @example
|
|
723
520
|
* ```ts
|
|
724
|
-
* type
|
|
725
|
-
* type
|
|
521
|
+
* type Complex = { a: string } & { b: number };
|
|
522
|
+
* type PrettyComplex = Prettify<Complex>; // Shows as { a: string; b: number }
|
|
726
523
|
* ```
|
|
727
524
|
*/
|
|
728
|
-
type
|
|
525
|
+
type Prettify<T> = T extends infer U ? U extends object ? { [K in keyof U]: U[K] } & {} : U : never;
|
|
729
526
|
/**
|
|
730
|
-
*
|
|
731
|
-
*
|
|
732
|
-
* This type traverses through nested objects and arrays, making all properties optional.
|
|
733
|
-
* Functions and primitives are left unchanged.
|
|
527
|
+
* Extracts all nested keys of an object type as dot-separated strings.
|
|
734
528
|
*
|
|
735
|
-
* @template
|
|
736
|
-
* @
|
|
529
|
+
* @template ObjectType - The object type to extract nested keys from
|
|
530
|
+
* @template IgnoreKeys - Keys to ignore during extraction
|
|
531
|
+
* @returns A union of dot-separated string paths
|
|
737
532
|
*
|
|
738
533
|
* @example
|
|
739
534
|
* ```ts
|
|
740
|
-
*
|
|
741
|
-
*
|
|
742
|
-
*
|
|
743
|
-
*
|
|
535
|
+
* interface User {
|
|
536
|
+
* name: string;
|
|
537
|
+
* profile: { age: number; address: { city: string } };
|
|
538
|
+
* tags: string[];
|
|
539
|
+
* }
|
|
744
540
|
*
|
|
745
|
-
* type
|
|
746
|
-
* //
|
|
747
|
-
*
|
|
748
|
-
* //
|
|
749
|
-
*
|
|
541
|
+
* type UserPaths = NestedKeyOf<User>;
|
|
542
|
+
* // 'name' | 'profile' | 'profile.age' | 'profile.address' | 'profile.address.city' | 'tags'
|
|
543
|
+
*
|
|
544
|
+
* // For literal arrays with objects (assumes const context):
|
|
545
|
+
* const obj = { items: [{ id: 1 }, { id: 2 }] };
|
|
546
|
+
* type ObjPaths = NestedKeyOf<typeof obj>;
|
|
547
|
+
* // 'items' | `items.${number}` | `items.${number}.id`
|
|
750
548
|
* ```
|
|
751
549
|
*/
|
|
752
|
-
type
|
|
550
|
+
type NestedKeyOf<ObjectType extends object, IgnoreKeys extends string = never> = { [Key in keyof ObjectType & string]: Key extends IgnoreKeys ? never : ObjectType[Key] extends readonly (infer U)[] ? U extends object ? ObjectType[Key] extends readonly [any, ...any[]] ? Key | `${Key}.${keyof ObjectType[Key] & `${number}`}` | `${Key}.${keyof ObjectType[Key] & `${number}`}.${NestedKeyOf<U, IgnoreKeys>}` : Key | `${Key}.${number}` | `${Key}.${number}.${NestedKeyOf<U, IgnoreKeys>}` : Key : ObjectType[Key] extends object ? `${Key}` | `${Key}.${NestedKeyOf<ObjectType[Key], IgnoreKeys>}` : `${Key}` }[keyof ObjectType & string];
|
|
753
551
|
/**
|
|
754
|
-
*
|
|
755
|
-
|
|
756
|
-
*
|
|
757
|
-
|
|
758
|
-
* @
|
|
552
|
+
* Creates a type that excludes properties present in another type.
|
|
553
|
+
|
|
554
|
+
* This is useful for creating mutually exclusive types.
|
|
555
|
+
|
|
556
|
+
* @template T - The base type
|
|
557
|
+
* @template U - The type whose properties to exclude
|
|
558
|
+
* @returns A type with properties from T that are not in U
|
|
759
559
|
*
|
|
560
|
+
|
|
760
561
|
* @example
|
|
761
562
|
* ```ts
|
|
762
|
-
* type
|
|
763
|
-
* type
|
|
764
|
-
*
|
|
563
|
+
* type A = { x: number; y: string };
|
|
564
|
+
* type B = { y: string };
|
|
565
|
+
* type Result = Without<A, B>; // { x?: never }
|
|
765
566
|
* ```
|
|
766
567
|
*/
|
|
767
|
-
type
|
|
568
|
+
type Without<T, U$1> = { [P in Exclude<keyof T, keyof U$1>]?: never };
|
|
569
|
+
//#endregion
|
|
570
|
+
//#region src/types/gates.d.ts
|
|
571
|
+
type BUFFER<T> = T;
|
|
572
|
+
type IMPLIES<T, U$1> = T extends U$1 ? true : false;
|
|
573
|
+
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;
|
|
574
|
+
type XNOR_Binary<T, U$1> = (T & U$1) | (Without<T, U$1> & Without<U$1, T>);
|
|
768
575
|
/**
|
|
769
|
-
*
|
|
576
|
+
* Computes a type-level AND (all must true) for a tuple of types.
|
|
770
577
|
*
|
|
771
|
-
*
|
|
772
|
-
* Functions and primitives are left unchanged.
|
|
578
|
+
* Truth table for 3 arguments:
|
|
773
579
|
*
|
|
774
|
-
*
|
|
775
|
-
*
|
|
580
|
+
* A B C = AND
|
|
581
|
+
* 1 1 1 = 1
|
|
582
|
+
* 1 1 0 = 0
|
|
583
|
+
* 1 0 1 = 0
|
|
584
|
+
* 1 0 0 = 0
|
|
585
|
+
* 0 1 1 = 0
|
|
586
|
+
* 0 1 0 = 0
|
|
587
|
+
* 0 0 1 = 0
|
|
588
|
+
* 0 0 0 = 0
|
|
776
589
|
*
|
|
777
|
-
* @
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
*
|
|
590
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
591
|
+
*/
|
|
592
|
+
type AND<T extends any[]> = T extends [infer F, ...infer R] ? R extends any[] ? F & AND<R> : F : unknown;
|
|
593
|
+
/**
|
|
594
|
+
* Computes a type-level OR (At least one) for a tuple of types.
|
|
782
595
|
*
|
|
783
|
-
*
|
|
784
|
-
* // {
|
|
785
|
-
* // server: { host: string; port: number };
|
|
786
|
-
* // }
|
|
787
|
-
* ```
|
|
788
|
-
*/
|
|
789
|
-
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;
|
|
790
|
-
/**
|
|
791
|
-
* Makes only specified properties of an object type required.
|
|
596
|
+
* Truth table for 3 arguments:
|
|
792
597
|
*
|
|
793
|
-
*
|
|
794
|
-
*
|
|
795
|
-
*
|
|
598
|
+
* A B C = OR
|
|
599
|
+
* 1 1 1 = 1
|
|
600
|
+
* 1 1 0 = 1
|
|
601
|
+
* 1 0 1 = 1
|
|
602
|
+
* 1 0 0 = 1
|
|
603
|
+
* 0 1 1 = 1
|
|
604
|
+
* 0 1 0 = 1
|
|
605
|
+
* 0 0 1 = 1
|
|
606
|
+
* 0 0 0 = 0
|
|
796
607
|
*
|
|
797
|
-
* @
|
|
798
|
-
* ```ts
|
|
799
|
-
* type PartialUser = { name?: string; age?: number; email?: string };
|
|
800
|
-
* type RequiredUser = SelectiveRequired<PartialUser, 'name'>;
|
|
801
|
-
* // { name: string; age?: number; email?: string }
|
|
802
|
-
* ```
|
|
608
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
803
609
|
*/
|
|
804
|
-
type
|
|
610
|
+
type OR<T extends any[]> = T extends [infer F, ...infer R] ? R extends any[] ? F | OR<R> : F : never;
|
|
805
611
|
/**
|
|
806
|
-
*
|
|
612
|
+
* Computes a type-level XOR (only one/odd) for a tuple of types.
|
|
807
613
|
*
|
|
808
|
-
*
|
|
614
|
+
* Truth table for 3 arguments:
|
|
809
615
|
*
|
|
810
|
-
*
|
|
811
|
-
*
|
|
616
|
+
* A B C = XOR
|
|
617
|
+
* 1 1 1 = 1
|
|
618
|
+
* 1 1 0 = 0
|
|
619
|
+
* 1 0 1 = 0
|
|
620
|
+
* 1 0 0 = 1
|
|
621
|
+
* 0 1 1 = 0
|
|
622
|
+
* 0 1 0 = 1
|
|
623
|
+
* 0 0 1 = 1
|
|
624
|
+
* 0 0 0 = 0
|
|
812
625
|
*
|
|
813
|
-
* @
|
|
814
|
-
* ```ts
|
|
815
|
-
* type User = { name: string; age: number };
|
|
816
|
-
* type ExcludedUser = Never<User>; // { name: never; age: never }
|
|
817
|
-
* ```
|
|
626
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
818
627
|
*/
|
|
819
|
-
type
|
|
628
|
+
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;
|
|
820
629
|
/**
|
|
821
|
-
*
|
|
822
|
-
*
|
|
823
|
-
* @template T - The type to make nullable
|
|
824
|
-
* @returns A type where all properties can be null
|
|
630
|
+
* Computes a type-level XNOR (All or None true) for a tuple of types.
|
|
825
631
|
*
|
|
826
|
-
*
|
|
827
|
-
* ```ts
|
|
828
|
-
* type User = { name: string; profile: { age: number } };
|
|
829
|
-
* type NullableUser = Nullable<User>;
|
|
830
|
-
* // { name: string | null; profile: { age: number | null } | null }
|
|
831
|
-
* ```
|
|
832
|
-
*/
|
|
833
|
-
type Nullable<T> = T extends object ? { [P in keyof T]: Nullable<T[P]> } : T | null;
|
|
834
|
-
/**
|
|
835
|
-
* Makes all properties of an object type optional (undefined) recursively.
|
|
632
|
+
* Truth table for 3 arguments:
|
|
836
633
|
*
|
|
837
|
-
*
|
|
838
|
-
*
|
|
634
|
+
* A B C = XNOR
|
|
635
|
+
* 1 1 1 = 0
|
|
636
|
+
* 1 1 0 = 1
|
|
637
|
+
* 1 0 1 = 1
|
|
638
|
+
* 1 0 0 = 0
|
|
639
|
+
* 0 1 1 = 1
|
|
640
|
+
* 0 1 0 = 0
|
|
641
|
+
* 0 0 1 = 0
|
|
642
|
+
* 0 0 0 = 1
|
|
839
643
|
*
|
|
840
|
-
* @
|
|
841
|
-
* ```ts
|
|
842
|
-
* type User = { name: string; profile: { age: number } };
|
|
843
|
-
* type OptionalUser = Optional<User>;
|
|
844
|
-
* // { name: string | undefined; profile: { age: number | undefined } | undefined }
|
|
845
|
-
* ```
|
|
644
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
846
645
|
*/
|
|
847
|
-
type
|
|
646
|
+
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;
|
|
848
647
|
/**
|
|
849
|
-
*
|
|
648
|
+
* Computes a type-level NOT for a tuple of types.
|
|
850
649
|
*
|
|
851
|
-
*
|
|
852
|
-
* @returns A type where all properties can be null or undefined
|
|
650
|
+
* Truth table for 3 arguments:
|
|
853
651
|
*
|
|
854
|
-
*
|
|
855
|
-
*
|
|
856
|
-
*
|
|
857
|
-
*
|
|
858
|
-
*
|
|
859
|
-
*
|
|
652
|
+
* A B C = NOT
|
|
653
|
+
* 1 1 1 = 0
|
|
654
|
+
* 1 1 0 = 0
|
|
655
|
+
* 1 0 1 = 0
|
|
656
|
+
* 1 0 0 = 0
|
|
657
|
+
* 0 1 1 = 0
|
|
658
|
+
* 0 1 0 = 0
|
|
659
|
+
* 0 0 1 = 0
|
|
660
|
+
* 0 0 0 = 1
|
|
661
|
+
*
|
|
662
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
860
663
|
*/
|
|
861
|
-
type
|
|
664
|
+
type NOT<T> = { [P in keyof T]?: never };
|
|
862
665
|
/**
|
|
863
|
-
*
|
|
666
|
+
* Computes a type-level NAND for a tuple of types.
|
|
864
667
|
*
|
|
865
|
-
*
|
|
668
|
+
* Truth table for 3 arguments:
|
|
866
669
|
*
|
|
867
|
-
*
|
|
868
|
-
*
|
|
670
|
+
* A B C = NAND
|
|
671
|
+
* 1 1 1 = 0
|
|
672
|
+
* 1 1 0 = 1
|
|
673
|
+
* 1 0 1 = 1
|
|
674
|
+
* 1 0 0 = 1
|
|
675
|
+
* 0 1 1 = 1
|
|
676
|
+
* 0 1 0 = 1
|
|
677
|
+
* 0 0 1 = 1
|
|
678
|
+
* 0 0 0 = 1
|
|
869
679
|
*
|
|
870
|
-
* @
|
|
871
|
-
* ```ts
|
|
872
|
-
* type User = { name: string; profile: { age: number } };
|
|
873
|
-
* type MaybeUser = Maybe<User>;
|
|
874
|
-
* // { name?: string | null | undefined; profile?: { age?: number | null | undefined } | null | undefined }
|
|
875
|
-
* ```
|
|
680
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
876
681
|
*/
|
|
877
|
-
type
|
|
682
|
+
type NAND<T extends any[]> = NOT<AND<T>>;
|
|
878
683
|
/**
|
|
879
|
-
*
|
|
880
|
-
*
|
|
881
|
-
* This type traverses through nested objects and arrays, making all properties readonly.
|
|
882
|
-
* Functions and primitives are left unchanged.
|
|
684
|
+
* Computes a type-level NOR for a tuple of types.
|
|
883
685
|
*
|
|
884
|
-
*
|
|
885
|
-
* @returns A type with all properties readonly recursively
|
|
686
|
+
* Truth table for 3 arguments:
|
|
886
687
|
*
|
|
887
|
-
*
|
|
888
|
-
*
|
|
889
|
-
*
|
|
890
|
-
*
|
|
891
|
-
*
|
|
892
|
-
*
|
|
688
|
+
* A B C = NOR
|
|
689
|
+
* 1 1 1 = 0
|
|
690
|
+
* 1 1 0 = 0
|
|
691
|
+
* 1 0 1 = 0
|
|
692
|
+
* 1 0 0 = 0
|
|
693
|
+
* 0 1 1 = 0
|
|
694
|
+
* 0 1 0 = 0
|
|
695
|
+
* 0 0 1 = 0
|
|
696
|
+
* 0 0 0 = 1
|
|
893
697
|
*
|
|
894
|
-
*
|
|
895
|
-
* // {
|
|
896
|
-
* // readonly server: { readonly host: string; readonly port: number };
|
|
897
|
-
* // readonly features: readonly string[];
|
|
898
|
-
* // }
|
|
899
|
-
* ```
|
|
698
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
900
699
|
*/
|
|
901
|
-
type
|
|
700
|
+
type NOR<T extends any[]> = NOT<OR<T>>;
|
|
701
|
+
//#endregion
|
|
702
|
+
//#region src/types/guards.d.ts
|
|
902
703
|
/**
|
|
903
|
-
*
|
|
704
|
+
* Represents primitive JavaScript types including null and undefined.
|
|
705
|
+
*/
|
|
706
|
+
type Primitive = string | number | bigint | boolean | symbol | null | undefined;
|
|
707
|
+
/**
|
|
708
|
+
* Represents all falsy values in JavaScript.
|
|
709
|
+
*/
|
|
710
|
+
type Falsy = false | '' | 0 | null | undefined;
|
|
711
|
+
/**
|
|
712
|
+
* Type guard that checks if a value is falsy.
|
|
904
713
|
*
|
|
905
|
-
* @
|
|
906
|
-
* @returns
|
|
714
|
+
* @param val - The value to check
|
|
715
|
+
* @returns True if the value is falsy, false otherwise
|
|
907
716
|
*
|
|
908
717
|
* @example
|
|
909
|
-
*
|
|
910
|
-
*
|
|
911
|
-
*
|
|
912
|
-
* // { name: string; profile: { age: number } }
|
|
913
|
-
* ```
|
|
718
|
+
* if (isFalsy(value)) {
|
|
719
|
+
* console.log('Value is falsy');
|
|
720
|
+
* }
|
|
914
721
|
*/
|
|
915
|
-
|
|
722
|
+
declare const isFalsy: (val: unknown) => val is Falsy;
|
|
916
723
|
/**
|
|
917
|
-
*
|
|
724
|
+
* Type guard that checks if a value is null or undefined.
|
|
918
725
|
*
|
|
919
|
-
* @
|
|
920
|
-
* @
|
|
921
|
-
* @returns A union of keys whose values match the specified type
|
|
726
|
+
* @param val - The value to check
|
|
727
|
+
* @returns True if the value is null or undefined, false otherwise
|
|
922
728
|
*
|
|
923
729
|
* @example
|
|
924
|
-
*
|
|
925
|
-
*
|
|
926
|
-
*
|
|
927
|
-
* type NumberKeys = KeysOfType<User, number>; // 'age'
|
|
928
|
-
* ```
|
|
730
|
+
* if (isNullish(value)) {
|
|
731
|
+
* console.log('Value is null or undefined');
|
|
732
|
+
* }
|
|
929
733
|
*/
|
|
930
|
-
|
|
734
|
+
declare const isNullish: (val: unknown) => val is null | undefined;
|
|
931
735
|
/**
|
|
932
|
-
*
|
|
736
|
+
* Type guard that checks if a value is a boolean.
|
|
933
737
|
*
|
|
934
|
-
* @
|
|
935
|
-
* @
|
|
936
|
-
* @returns An object type without properties of the specified value type
|
|
738
|
+
* @param val - The value to check
|
|
739
|
+
* @returns True if the value is a boolean, false otherwise
|
|
937
740
|
*
|
|
938
741
|
* @example
|
|
939
|
-
*
|
|
940
|
-
*
|
|
941
|
-
*
|
|
942
|
-
* ```
|
|
742
|
+
* if (isBoolean(value)) {
|
|
743
|
+
* console.log('Value is a boolean');
|
|
744
|
+
* }
|
|
943
745
|
*/
|
|
944
|
-
|
|
746
|
+
declare const isBoolean: (val: unknown) => val is boolean;
|
|
945
747
|
/**
|
|
946
|
-
*
|
|
748
|
+
* Type guard that checks if a value is a string.
|
|
947
749
|
*
|
|
948
|
-
* @
|
|
949
|
-
* @
|
|
950
|
-
* @returns An object type with specified properties required
|
|
750
|
+
* @param val - The value to check
|
|
751
|
+
* @returns True if the value is a string, false otherwise
|
|
951
752
|
*
|
|
952
753
|
* @example
|
|
953
|
-
*
|
|
954
|
-
*
|
|
955
|
-
*
|
|
956
|
-
* // { name: string; age?: number; email?: string }
|
|
957
|
-
* ```
|
|
754
|
+
* if (isString(value)) {
|
|
755
|
+
* console.log('Value is a string');
|
|
756
|
+
* }
|
|
958
757
|
*/
|
|
959
|
-
|
|
758
|
+
declare const isString: (val: unknown) => val is string;
|
|
960
759
|
/**
|
|
961
|
-
*
|
|
760
|
+
* Type guard that checks whether a value is a finite number.
|
|
962
761
|
*
|
|
963
|
-
*
|
|
762
|
+
* This excludes `NaN`, `Infinity`, and `-Infinity`.
|
|
964
763
|
*
|
|
965
|
-
* @
|
|
966
|
-
* @
|
|
967
|
-
* @returns Properties unique to T or U
|
|
764
|
+
* @param val - The value to check
|
|
765
|
+
* @returns `true` if the value is a finite number
|
|
968
766
|
*
|
|
969
767
|
* @example
|
|
970
|
-
*
|
|
971
|
-
*
|
|
972
|
-
*
|
|
973
|
-
* type DiffAB = Diff<A, B>; // { x: number; z: boolean }
|
|
974
|
-
* ```
|
|
768
|
+
* isFiniteNumber(42); // true
|
|
769
|
+
* isFiniteNumber(NaN); // false
|
|
770
|
+
* isFiniteNumber(Infinity); // false
|
|
975
771
|
*/
|
|
976
|
-
|
|
772
|
+
declare const isFiniteNumber: (val: unknown) => val is number;
|
|
977
773
|
/**
|
|
978
|
-
*
|
|
774
|
+
* Type guard that checks if a value is an array.
|
|
979
775
|
*
|
|
980
|
-
* @
|
|
981
|
-
* @
|
|
982
|
-
* @returns Properties that exist in both T and U
|
|
776
|
+
* @param val - The value to check
|
|
777
|
+
* @returns True if the value is an array, false otherwise
|
|
983
778
|
*
|
|
984
779
|
* @example
|
|
985
|
-
*
|
|
986
|
-
*
|
|
987
|
-
*
|
|
988
|
-
* type IntersectionAB = Intersection<A, B>; // { y: string }
|
|
989
|
-
* ```
|
|
780
|
+
* if (isArray(value)) {
|
|
781
|
+
* console.log('Value is an array');
|
|
782
|
+
* }
|
|
990
783
|
*/
|
|
991
|
-
|
|
784
|
+
declare const isArray: (val: unknown) => val is unknown[];
|
|
992
785
|
/**
|
|
993
|
-
*
|
|
786
|
+
* Type guard that checks if a value is a function.
|
|
994
787
|
*
|
|
995
|
-
* @
|
|
996
|
-
* @
|
|
997
|
-
* @returns A merged object type with properties from both
|
|
788
|
+
* @param val - The value to check
|
|
789
|
+
* @returns True if the value is a function, false otherwise
|
|
998
790
|
*
|
|
999
791
|
* @example
|
|
1000
|
-
*
|
|
1001
|
-
*
|
|
1002
|
-
*
|
|
1003
|
-
* type Merged = Merge<A, B>; // { x: number; y: boolean; z: string }
|
|
1004
|
-
* ```
|
|
792
|
+
* if (isFunction(value)) {
|
|
793
|
+
* console.log('Value is a function');
|
|
794
|
+
* }
|
|
1005
795
|
*/
|
|
1006
|
-
|
|
796
|
+
declare const isFunction: (val: unknown) => val is Function;
|
|
1007
797
|
/**
|
|
1008
|
-
*
|
|
798
|
+
* Type guard that checks if a value is a primitive type.
|
|
1009
799
|
*
|
|
1010
|
-
* @
|
|
1011
|
-
* @
|
|
1012
|
-
* @returns T without properties that exist in U
|
|
800
|
+
* @param val - The value to check
|
|
801
|
+
* @returns True if the value is a primitive, false otherwise
|
|
1013
802
|
*
|
|
1014
803
|
* @example
|
|
1015
|
-
*
|
|
1016
|
-
*
|
|
1017
|
-
*
|
|
1018
|
-
* type Subtracted = Substract<A, B>; // { x: number; z: boolean }
|
|
1019
|
-
* ```
|
|
804
|
+
* if (isPrimitive(value)) {
|
|
805
|
+
* console.log('Value is a primitive type');
|
|
806
|
+
* }
|
|
1020
807
|
*/
|
|
1021
|
-
|
|
808
|
+
declare const isPrimitive: (val: unknown) => val is Primitive;
|
|
1022
809
|
/**
|
|
1023
|
-
*
|
|
1024
|
-
*
|
|
1025
|
-
* Useful for creating mutually exclusive configurations.
|
|
810
|
+
* Type guard that checks if a value is a plain object (not an array, function, etc.).
|
|
1026
811
|
*
|
|
1027
|
-
* @
|
|
1028
|
-
* @returns
|
|
812
|
+
* @param value - The value to check
|
|
813
|
+
* @returns True if the value is a plain object, false otherwise
|
|
1029
814
|
*
|
|
1030
815
|
* @example
|
|
1031
|
-
*
|
|
1032
|
-
*
|
|
1033
|
-
*
|
|
1034
|
-
* // { host: string; port: number } | {}
|
|
1035
|
-
* ```
|
|
816
|
+
* if (isPlainObject(value)) {
|
|
817
|
+
* console.log('Value is a plain object');
|
|
818
|
+
* }
|
|
1036
819
|
*/
|
|
1037
|
-
|
|
820
|
+
declare function isPlainObject(value: unknown): value is Record<string, any>;
|
|
821
|
+
//#endregion
|
|
822
|
+
//#region src/functions/object.d.ts
|
|
1038
823
|
/**
|
|
1039
|
-
*
|
|
1040
|
-
*
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
*
|
|
824
|
+
* Type representing a path split into segments
|
|
825
|
+
* @template S - The original path string type
|
|
826
|
+
*/
|
|
827
|
+
type SplitPath<S$1 extends string> = S$1 extends `${infer First}.${infer Rest}` ? [First, ...SplitPath<Rest>] : [S$1];
|
|
828
|
+
/**
|
|
829
|
+
* Recursive type to resolve nested object types based on path
|
|
830
|
+
* @template T - Current object type
|
|
831
|
+
* @template K - Array of path segments
|
|
832
|
+
*/
|
|
833
|
+
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;
|
|
834
|
+
/**
|
|
835
|
+
* Get a nested value from an object using dot notation path
|
|
836
|
+
* @template T - Object type
|
|
837
|
+
* @template S - Valid path string type constrained by object structure
|
|
838
|
+
* @template D - Default value type
|
|
839
|
+
* @param obj - Source object
|
|
840
|
+
* @param path - Dot-separated path string (constrained to valid paths)
|
|
841
|
+
* @param defaultValue - Fallback value if path not found
|
|
842
|
+
* @returns Value at path or default value
|
|
1045
843
|
*
|
|
1046
844
|
* @example
|
|
1047
|
-
*
|
|
1048
|
-
* type Action = { type: 'create'; payload: string } | { type: 'update'; id: number };
|
|
1049
|
-
* type OneAction = OneOf<Action>;
|
|
1050
|
-
* // { type: 'create'; payload: string } | { type: 'update'; id: number }
|
|
1051
|
-
* ```
|
|
845
|
+
* getObjectValue({a: [{b: 1}]}, 'a.0.b', 2) // 1
|
|
1052
846
|
*/
|
|
1053
|
-
|
|
847
|
+
declare function getObjectValue<const T extends object, S$1 extends NestedKeyOf<T>, D>(obj: T, path: S$1, defaultValue: D): Exclude<GetValue<T, SplitPath<S$1>>, undefined> | D;
|
|
1054
848
|
/**
|
|
1055
|
-
*
|
|
1056
|
-
*
|
|
1057
|
-
* @template
|
|
1058
|
-
* @
|
|
849
|
+
* Get a nested value from an object using dot notation path
|
|
850
|
+
* @template T - Object type
|
|
851
|
+
* @template S - Valid path string type constrained by object structure
|
|
852
|
+
* @param obj - Source object
|
|
853
|
+
* @param path - Dot-separated path string (constrained to valid paths)
|
|
854
|
+
* @returns Value at path or undefined
|
|
1059
855
|
*
|
|
1060
856
|
* @example
|
|
1061
|
-
*
|
|
1062
|
-
* type Config = { a: number; b: string; c: boolean };
|
|
1063
|
-
* type TwoConfig = TwoOf<Config>;
|
|
1064
|
-
* // { a: number; b: string } | { a: number; c: boolean } | { b: string; c: boolean }
|
|
1065
|
-
* ```
|
|
857
|
+
* getObjectValue({a: [{b: 1}]}, 'a.0.b') // 1
|
|
1066
858
|
*/
|
|
1067
|
-
|
|
859
|
+
declare function getObjectValue<const T extends object, S$1 extends NestedKeyOf<T>>(obj: T, path: S$1): GetValue<T, SplitPath<S$1>> | undefined;
|
|
1068
860
|
/**
|
|
1069
|
-
*
|
|
861
|
+
* Extend an object or function with additional properties while
|
|
862
|
+
* preserving the original type information.
|
|
1070
863
|
*
|
|
1071
|
-
*
|
|
864
|
+
* Works with both plain objects and callable functions since
|
|
865
|
+
* functions in JavaScript are objects too. Also handles nullable types.
|
|
1072
866
|
*
|
|
1073
|
-
* @template T
|
|
1074
|
-
* @
|
|
867
|
+
* @template T The base object or function type (can be null/undefined)
|
|
868
|
+
* @template P The additional properties type
|
|
869
|
+
*
|
|
870
|
+
* @param base - The object or function to extend (can be null/undefined)
|
|
871
|
+
* @param props - An object containing properties to attach
|
|
872
|
+
*
|
|
873
|
+
* @returns The same object/function, augmented with the given properties, or the original value if null/undefined
|
|
1075
874
|
*
|
|
1076
875
|
* @example
|
|
1077
876
|
* ```ts
|
|
1078
|
-
*
|
|
1079
|
-
*
|
|
877
|
+
* // Extend a plain object
|
|
878
|
+
* const config = extendProps({ apiUrl: '/api' }, { timeout: 5000 });
|
|
879
|
+
* // config has both apiUrl and timeout properties
|
|
880
|
+
*
|
|
881
|
+
* // Extend a function with metadata
|
|
882
|
+
* const fetchData = (url: string) => fetch(url).then(r => r.json());
|
|
883
|
+
* const enhancedFetch = extendProps(fetchData, {
|
|
884
|
+
* description: 'Data fetching utility',
|
|
885
|
+
* version: '1.0'
|
|
886
|
+
* });
|
|
887
|
+
* // enhancedFetch is callable and has description/version properties
|
|
888
|
+
*
|
|
889
|
+
* // Create plugin system
|
|
890
|
+
* const basePlugin = { name: 'base', enabled: true };
|
|
891
|
+
* const authPlugin = extendProps(basePlugin, {
|
|
892
|
+
* authenticate: (token: string) => validateToken(token)
|
|
893
|
+
* });
|
|
894
|
+
*
|
|
895
|
+
* // Build configuration objects
|
|
896
|
+
* const defaultSettings = { theme: 'light', lang: 'en' };
|
|
897
|
+
* const userSettings = extendProps(defaultSettings, {
|
|
898
|
+
* theme: 'dark',
|
|
899
|
+
* notifications: true
|
|
900
|
+
* });
|
|
901
|
+
*
|
|
902
|
+
* // Handle nullable types (e.g., Supabase Session | null)
|
|
903
|
+
* const session: Session | null = getSession();
|
|
904
|
+
* const extendedSession = extendProps(session, { customProp: 'value' });
|
|
905
|
+
* // extendedSession is (Session & { customProp: string }) | null
|
|
1080
906
|
* ```
|
|
1081
907
|
*/
|
|
1082
|
-
|
|
908
|
+
declare function extendProps<T, P$1 extends object>(base: T, props: P$1): T extends null | undefined ? T : T & P$1;
|
|
909
|
+
//#endregion
|
|
910
|
+
//#region src/functions/poll.d.ts
|
|
1083
911
|
/**
|
|
1084
|
-
*
|
|
912
|
+
* Repeatedly polls an async `cond` function UNTIL it returns a TRUTHY value,
|
|
913
|
+
* or until the operation times out or is aborted.
|
|
1085
914
|
*
|
|
1086
|
-
*
|
|
1087
|
-
*
|
|
1088
|
-
* @
|
|
915
|
+
* Designed for waiting on async jobs, external state, or delayed availability.
|
|
916
|
+
*
|
|
917
|
+
* @template T The type of the successful result.
|
|
918
|
+
*
|
|
919
|
+
* @param cond
|
|
920
|
+
* A function returning a Promise that resolves to:
|
|
921
|
+
* - a truthy value `T` → stop polling and return it
|
|
922
|
+
* - falsy/null/undefined → continue polling
|
|
923
|
+
*
|
|
924
|
+
* @param options
|
|
925
|
+
* Configuration options:
|
|
926
|
+
* - `interval` (number) — Time between polls in ms (default: 5000 ms)
|
|
927
|
+
* - `timeout` (number) — Max total duration before failing (default: 5 min)
|
|
928
|
+
* - `jitter` (boolean) — Add small random offset (±10%) to intervals to avoid sync bursts (default: true)
|
|
929
|
+
* - `signal` (AbortSignal) — Optional abort signal to cancel polling
|
|
930
|
+
*
|
|
931
|
+
* @returns
|
|
932
|
+
* Resolves with the truthy value `T` when successful.
|
|
933
|
+
* Throws `AbortError` if aborted
|
|
1089
934
|
*
|
|
1090
935
|
* @example
|
|
1091
936
|
* ```ts
|
|
1092
|
-
*
|
|
1093
|
-
*
|
|
1094
|
-
*
|
|
1095
|
-
*
|
|
1096
|
-
* };
|
|
937
|
+
* // Poll for job completion
|
|
938
|
+
* const job = await poll(async () => {
|
|
939
|
+
* const status = await getJobStatus();
|
|
940
|
+
* return status === 'done' ? status : null;
|
|
941
|
+
* }, { interval: 3000, timeout: 60000 });
|
|
942
|
+
* ```
|
|
1097
943
|
*
|
|
1098
|
-
*
|
|
1099
|
-
*
|
|
944
|
+
* @example
|
|
945
|
+
* ```ts
|
|
946
|
+
* // Wait for API endpoint to be ready
|
|
947
|
+
* const apiReady = await poll(async () => {
|
|
948
|
+
* try {
|
|
949
|
+
* await fetch('/api/health');
|
|
950
|
+
* return true;
|
|
951
|
+
* } catch {
|
|
952
|
+
* return null;
|
|
953
|
+
* }
|
|
954
|
+
* }, { interval: 1000, timeout: 30000 });
|
|
1100
955
|
* ```
|
|
1101
|
-
*/
|
|
1102
|
-
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];
|
|
1103
|
-
/**
|
|
1104
|
-
* Creates a type that excludes properties present in another type.
|
|
1105
956
|
*
|
|
1106
|
-
*
|
|
957
|
+
* @example
|
|
958
|
+
* ```ts
|
|
959
|
+
* // Poll with abort signal for cancellation
|
|
960
|
+
* const controller = new AbortController();
|
|
961
|
+
* setTimeout(() => controller.abort(), 10000); // Cancel after 10s
|
|
1107
962
|
*
|
|
1108
|
-
*
|
|
1109
|
-
*
|
|
1110
|
-
*
|
|
963
|
+
* try {
|
|
964
|
+
* const result = await poll(
|
|
965
|
+
* () => checkExternalService(),
|
|
966
|
+
* { interval: 2000, signal: controller.signal }
|
|
967
|
+
* );
|
|
968
|
+
* } catch (err) {
|
|
969
|
+
* if (err.name === 'AbortError') {
|
|
970
|
+
* console.log('Polling was cancelled');
|
|
971
|
+
* }
|
|
972
|
+
* }
|
|
973
|
+
* ```
|
|
1111
974
|
*
|
|
1112
975
|
* @example
|
|
1113
976
|
* ```ts
|
|
1114
|
-
*
|
|
1115
|
-
*
|
|
1116
|
-
*
|
|
977
|
+
* // Poll for user action completion
|
|
978
|
+
* const userConfirmed = await poll(async () => {
|
|
979
|
+
* const confirmations = await getPendingConfirmations();
|
|
980
|
+
* return confirmations.length > 0 ? confirmations[0] : null;
|
|
981
|
+
* }, { interval: 5000, timeout: 300000 }); // 5 min timeout
|
|
1117
982
|
* ```
|
|
1118
983
|
*/
|
|
1119
|
-
|
|
984
|
+
declare function poll<T>(cond: () => Promise<T | null | false | undefined>, {
|
|
985
|
+
interval,
|
|
986
|
+
timeout,
|
|
987
|
+
jitter,
|
|
988
|
+
signal
|
|
989
|
+
}?: Partial<{
|
|
990
|
+
interval: number;
|
|
991
|
+
timeout: number;
|
|
992
|
+
signal: AbortSignal;
|
|
993
|
+
jitter: boolean;
|
|
994
|
+
}>): Promise<T>;
|
|
1120
995
|
//#endregion
|
|
1121
|
-
//#region src/
|
|
1122
|
-
type
|
|
1123
|
-
type
|
|
1124
|
-
type
|
|
1125
|
-
type
|
|
996
|
+
//#region src/functions/promise.d.ts
|
|
997
|
+
type TaskType<T> = Promise<T> | (() => Promise<T>);
|
|
998
|
+
type AwaitedTuple<T extends readonly TaskType<any>[] | []> = { -readonly [P in keyof T]: Awaited<T[P] extends (() => Promise<infer R>) ? R : T[P]> };
|
|
999
|
+
type ObjectValues<T extends Record<string, TaskType<any>>> = { [K in keyof T]: Awaited<T[K] extends (() => Promise<infer R>) ? R : T[K]> };
|
|
1000
|
+
type ConcurrenceResult<T> = {
|
|
1001
|
+
results: T;
|
|
1002
|
+
errors: Error[];
|
|
1003
|
+
succeeded: number;
|
|
1004
|
+
failed: number;
|
|
1005
|
+
duration: number;
|
|
1006
|
+
};
|
|
1007
|
+
type ConcurrenceOptions = {
|
|
1008
|
+
concurrency?: number;
|
|
1009
|
+
timeout?: number;
|
|
1010
|
+
signal?: AbortSignal;
|
|
1011
|
+
retry?: number;
|
|
1012
|
+
retryDelay?: number;
|
|
1013
|
+
throwOnFirstError?: boolean;
|
|
1014
|
+
ignoreErrors?: boolean;
|
|
1015
|
+
};
|
|
1016
|
+
declare function withConcurrency<T extends readonly TaskType<any>[] | []>(tasks: T, options?: ConcurrenceOptions): Promise<ConcurrenceResult<AwaitedTuple<T>>>;
|
|
1017
|
+
declare function withConcurrency<T extends Record<string, TaskType<any>>>(tasks: T, options?: ConcurrenceOptions): Promise<ConcurrenceResult<ObjectValues<T>>>;
|
|
1018
|
+
//#endregion
|
|
1019
|
+
//#region src/functions/schedule.d.ts
|
|
1126
1020
|
/**
|
|
1127
|
-
*
|
|
1128
|
-
*
|
|
1129
|
-
* Truth table for 3 arguments:
|
|
1130
|
-
*
|
|
1131
|
-
* A B C = AND
|
|
1132
|
-
* 1 1 1 = 1
|
|
1133
|
-
* 1 1 0 = 0
|
|
1134
|
-
* 1 0 1 = 0
|
|
1135
|
-
* 1 0 0 = 0
|
|
1136
|
-
* 0 1 1 = 0
|
|
1137
|
-
* 0 1 0 = 0
|
|
1138
|
-
* 0 0 1 = 0
|
|
1139
|
-
* 0 0 0 = 0
|
|
1140
|
-
*
|
|
1141
|
-
* @template T - Tuple of boolean-like types (1/0)
|
|
1021
|
+
* A task function that can be synchronous or asynchronous.
|
|
1142
1022
|
*/
|
|
1143
|
-
type
|
|
1023
|
+
type Task = () => Promise<void> | void;
|
|
1144
1024
|
/**
|
|
1145
|
-
*
|
|
1146
|
-
*
|
|
1147
|
-
* Truth table for 3 arguments:
|
|
1148
|
-
*
|
|
1149
|
-
* A B C = OR
|
|
1150
|
-
* 1 1 1 = 1
|
|
1151
|
-
* 1 1 0 = 1
|
|
1152
|
-
* 1 0 1 = 1
|
|
1153
|
-
* 1 0 0 = 1
|
|
1154
|
-
* 0 1 1 = 1
|
|
1155
|
-
* 0 1 0 = 1
|
|
1156
|
-
* 0 0 1 = 1
|
|
1157
|
-
* 0 0 0 = 0
|
|
1158
|
-
*
|
|
1159
|
-
* @template T - Tuple of boolean-like types (1/0)
|
|
1025
|
+
* Options for configuring the schedule function.
|
|
1160
1026
|
*/
|
|
1161
|
-
|
|
1027
|
+
interface ScheduleOpts {
|
|
1028
|
+
/** Number of retry attempts on failure. Defaults to 0. */
|
|
1029
|
+
retry?: number;
|
|
1030
|
+
/** Delay in milliseconds between retries. Defaults to 0. */
|
|
1031
|
+
delay?: number;
|
|
1032
|
+
/** Maximum time in milliseconds to wait for the task to complete. */
|
|
1033
|
+
timeout?: number;
|
|
1034
|
+
}
|
|
1162
1035
|
/**
|
|
1163
|
-
*
|
|
1164
|
-
*
|
|
1165
|
-
* Truth table for 3 arguments:
|
|
1166
|
-
*
|
|
1167
|
-
* A B C = XOR
|
|
1168
|
-
* 1 1 1 = 1
|
|
1169
|
-
* 1 1 0 = 0
|
|
1170
|
-
* 1 0 1 = 0
|
|
1171
|
-
* 1 0 0 = 1
|
|
1172
|
-
* 0 1 1 = 0
|
|
1173
|
-
* 0 1 0 = 1
|
|
1174
|
-
* 0 0 1 = 1
|
|
1175
|
-
* 0 0 0 = 0
|
|
1036
|
+
* Runs a function asynchronously in the background without blocking the main thread.
|
|
1176
1037
|
*
|
|
1177
|
-
*
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
/**
|
|
1181
|
-
* Computes a type-level XNOR (All or None true) for a tuple of types.
|
|
1038
|
+
* Executes the task immediately using setTimeout, with optional retry logic on failure.
|
|
1039
|
+
* Useful for non-critical operations like analytics, logging, or background processing.
|
|
1040
|
+
* Logs execution time and retry attempts to the console.
|
|
1182
1041
|
*
|
|
1183
|
-
*
|
|
1042
|
+
* @param task - The function to execute asynchronously
|
|
1043
|
+
* @param options - Configuration options for retries and timing
|
|
1184
1044
|
*
|
|
1185
|
-
*
|
|
1186
|
-
*
|
|
1187
|
-
*
|
|
1188
|
-
*
|
|
1189
|
-
*
|
|
1190
|
-
*
|
|
1191
|
-
* 0 1 0 = 0
|
|
1192
|
-
* 0 0 1 = 0
|
|
1193
|
-
* 0 0 0 = 1
|
|
1045
|
+
* @example
|
|
1046
|
+
* ```ts
|
|
1047
|
+
* // Simple background task
|
|
1048
|
+
* schedule(() => {
|
|
1049
|
+
* console.log('Background work done');
|
|
1050
|
+
* });
|
|
1194
1051
|
*
|
|
1195
|
-
*
|
|
1052
|
+
* // Task with retry on failure
|
|
1053
|
+
* schedule(
|
|
1054
|
+
* () => sendAnalytics(),
|
|
1055
|
+
* { retry: 3, delay: 1000 }
|
|
1056
|
+
* );
|
|
1057
|
+
* ```
|
|
1196
1058
|
*/
|
|
1197
|
-
|
|
1059
|
+
declare function schedule(task: Task, options?: ScheduleOpts): void;
|
|
1060
|
+
//#endregion
|
|
1061
|
+
//#region src/functions/shield.d.ts
|
|
1198
1062
|
/**
|
|
1199
|
-
*
|
|
1200
|
-
*
|
|
1201
|
-
* Truth table for 3 arguments:
|
|
1202
|
-
*
|
|
1203
|
-
* A B C = NOT
|
|
1204
|
-
* 1 1 1 = 0
|
|
1205
|
-
* 1 1 0 = 0
|
|
1206
|
-
* 1 0 1 = 0
|
|
1207
|
-
* 1 0 0 = 0
|
|
1208
|
-
* 0 1 1 = 0
|
|
1209
|
-
* 0 1 0 = 0
|
|
1210
|
-
* 0 0 1 = 0
|
|
1211
|
-
* 0 0 0 = 1
|
|
1063
|
+
* A helper to run sync or async operations safely without try/catch.
|
|
1212
1064
|
*
|
|
1213
|
-
*
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
/**
|
|
1217
|
-
* Computes a type-level NAND for a tuple of types.
|
|
1065
|
+
* Returns a tuple `[error, data]`:
|
|
1066
|
+
* - `error`: the thrown error (if any), otherwise `null`
|
|
1067
|
+
* - `data`: the resolved value (if successful), otherwise `null`
|
|
1218
1068
|
*
|
|
1219
|
-
*
|
|
1069
|
+
* @example
|
|
1070
|
+
* ```ts
|
|
1071
|
+
* // Synchronous error handling
|
|
1072
|
+
* const [err, value] = shield(() => {
|
|
1073
|
+
* if (Math.random() > 0.5) throw new Error('Random failure');
|
|
1074
|
+
* return 'success';
|
|
1075
|
+
* });
|
|
1076
|
+
* if (err) {
|
|
1077
|
+
* console.error('Operation failed:', err);
|
|
1078
|
+
* } else {
|
|
1079
|
+
* console.log('Result:', value);
|
|
1080
|
+
* }
|
|
1220
1081
|
*
|
|
1221
|
-
*
|
|
1222
|
-
*
|
|
1223
|
-
*
|
|
1224
|
-
*
|
|
1225
|
-
*
|
|
1226
|
-
*
|
|
1227
|
-
*
|
|
1228
|
-
*
|
|
1229
|
-
*
|
|
1082
|
+
* // Asynchronous error handling
|
|
1083
|
+
* const [asyncErr, result] = await shield(async () => {
|
|
1084
|
+
* const response = await fetch('/api/data');
|
|
1085
|
+
* if (!response.ok) throw new Error('API error');
|
|
1086
|
+
* return response.json();
|
|
1087
|
+
* });
|
|
1088
|
+
* if (asyncErr) {
|
|
1089
|
+
* console.error('API call failed:', asyncErr);
|
|
1090
|
+
* } else {
|
|
1091
|
+
* processData(result);
|
|
1092
|
+
* }
|
|
1230
1093
|
*
|
|
1231
|
-
*
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
/**
|
|
1235
|
-
* Computes a type-level NOR for a tuple of types.
|
|
1094
|
+
* // API calls with fallbacks
|
|
1095
|
+
* const [fetchErr, data] = await shield(fetchUserData(userId));
|
|
1096
|
+
* const userData = fetchErr ? getCachedUserData(userId) : data;
|
|
1236
1097
|
*
|
|
1237
|
-
*
|
|
1098
|
+
* // File operations
|
|
1099
|
+
* const [fileErr, content] = shield(() => readFileSync('config.json'));
|
|
1100
|
+
* if (fileErr) {
|
|
1101
|
+
* console.warn('Could not read config, using defaults');
|
|
1102
|
+
* return defaultConfig;
|
|
1103
|
+
* }
|
|
1104
|
+
* ```
|
|
1238
1105
|
*
|
|
1239
|
-
*
|
|
1240
|
-
*
|
|
1241
|
-
*
|
|
1242
|
-
*
|
|
1243
|
-
*
|
|
1244
|
-
*
|
|
1245
|
-
*
|
|
1246
|
-
*
|
|
1247
|
-
*
|
|
1106
|
+
* @example
|
|
1107
|
+
* ```ts
|
|
1108
|
+
* // In async functions
|
|
1109
|
+
* async function safeApiCall() {
|
|
1110
|
+
* const [err, result] = await shield(callExternalAPI());
|
|
1111
|
+
* if (err) {
|
|
1112
|
+
* await logError(err);
|
|
1113
|
+
* return null;
|
|
1114
|
+
* }
|
|
1115
|
+
* return result;
|
|
1116
|
+
* }
|
|
1248
1117
|
*
|
|
1249
|
-
*
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
*
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
* Represents all falsy values in JavaScript.
|
|
1118
|
+
* // In event handlers
|
|
1119
|
+
* function handleSubmit(formData) {
|
|
1120
|
+
* const [validationErr, validatedData] = shield(() => validateForm(formData));
|
|
1121
|
+
* if (validationErr) {
|
|
1122
|
+
* showValidationError(validationErr);
|
|
1123
|
+
* return;
|
|
1124
|
+
* }
|
|
1125
|
+
* submitData(validatedData);
|
|
1126
|
+
* }
|
|
1127
|
+
* ```
|
|
1260
1128
|
*/
|
|
1261
|
-
|
|
1129
|
+
declare function shield<T, E = Error>(operation: Promise<T>): Promise<[E | null, T | null]>;
|
|
1130
|
+
declare function shield<T, E = Error>(operation: () => T): [E | null, T | null];
|
|
1131
|
+
//#endregion
|
|
1132
|
+
//#region src/functions/utils-core.d.ts
|
|
1262
1133
|
/**
|
|
1263
|
-
*
|
|
1134
|
+
* Converts various case styles (camelCase, PascalCase, kebab-case, snake_case) into readable normal case.
|
|
1264
1135
|
*
|
|
1265
|
-
*
|
|
1266
|
-
*
|
|
1136
|
+
* Transforms technical naming conventions into human-readable titles by:
|
|
1137
|
+
* - Adding spaces between words
|
|
1138
|
+
* - Capitalizing the first letter of each word
|
|
1139
|
+
* - Handling common separators (-, _, camelCase boundaries)
|
|
1140
|
+
*
|
|
1141
|
+
* @param inputString - The string to convert (supports camelCase, PascalCase, kebab-case, snake_case).
|
|
1142
|
+
* @returns The converted string in normal case (title case).
|
|
1267
1143
|
*
|
|
1268
1144
|
* @example
|
|
1269
|
-
*
|
|
1270
|
-
*
|
|
1271
|
-
*
|
|
1145
|
+
* ```ts
|
|
1146
|
+
* convertToNormalCase('camelCase') // 'Camel Case'
|
|
1147
|
+
* convertToNormalCase('kebab-case') // 'Kebab Case'
|
|
1148
|
+
* convertToNormalCase('snake_case') // 'Snake Case'
|
|
1149
|
+
* convertToNormalCase('PascalCase') // 'Pascal Case'
|
|
1150
|
+
* ```
|
|
1272
1151
|
*/
|
|
1273
|
-
declare
|
|
1152
|
+
declare function convertToNormalCase(inputString: string): string;
|
|
1274
1153
|
/**
|
|
1275
|
-
*
|
|
1276
|
-
*
|
|
1277
|
-
* @param
|
|
1278
|
-
* @returns
|
|
1279
|
-
*
|
|
1154
|
+
* Converts a string to a URL-friendly slug by trimming, converting to lowercase,
|
|
1155
|
+
* replacing diacritics, removing invalid characters, and replacing spaces with hyphens.
|
|
1156
|
+
* @param {string} [str] - The input string to convert.
|
|
1157
|
+
* @returns {string} The generated slug.
|
|
1280
1158
|
* @example
|
|
1281
|
-
*
|
|
1282
|
-
*
|
|
1283
|
-
* }
|
|
1159
|
+
* convertToSlug("Hello World!"); // "hello-world"
|
|
1160
|
+
* convertToSlug("Déjà Vu"); // "deja-vu"
|
|
1284
1161
|
*/
|
|
1285
|
-
declare const
|
|
1162
|
+
declare const convertToSlug: (str: string) => string;
|
|
1286
1163
|
/**
|
|
1287
|
-
*
|
|
1164
|
+
* Pauses execution for the specified time.
|
|
1288
1165
|
*
|
|
1289
|
-
*
|
|
1290
|
-
* @returns True if the value is a boolean, false otherwise
|
|
1166
|
+
* `signal` allows cancelling the sleep via AbortSignal.
|
|
1291
1167
|
*
|
|
1292
|
-
* @
|
|
1293
|
-
*
|
|
1294
|
-
*
|
|
1295
|
-
* }
|
|
1168
|
+
* @param time - Time in milliseconds to sleep (default is 1000ms)
|
|
1169
|
+
* @param signal - Optional AbortSignal to cancel the sleep early
|
|
1170
|
+
* @returns - A Promise that resolves after the specified time or when aborted
|
|
1296
1171
|
*/
|
|
1297
|
-
declare const
|
|
1172
|
+
declare const sleep: (time?: number, signal?: AbortSignal) => Promise<void>;
|
|
1173
|
+
type DebouncedFunction<F$1 extends (...args: any[]) => any> = {
|
|
1174
|
+
(...args: Parameters<F$1>): ReturnType<F$1> | undefined;
|
|
1175
|
+
readonly isPending: boolean;
|
|
1176
|
+
};
|
|
1298
1177
|
/**
|
|
1299
|
-
*
|
|
1178
|
+
* Creates a debounced function that delays invoking the provided function until
|
|
1179
|
+
* after the specified `wait` time has elapsed since the last invocation.
|
|
1300
1180
|
*
|
|
1301
|
-
*
|
|
1302
|
-
*
|
|
1181
|
+
* If the `immediate` option is set to `true`, the function will be invoked immediately
|
|
1182
|
+
* on the leading edge of the wait interval. Subsequent calls during the wait interval
|
|
1183
|
+
* will reset the timer but not invoke the function until the interval elapses again.
|
|
1184
|
+
*
|
|
1185
|
+
* The returned function includes the `isPending` property to check if the debounce
|
|
1186
|
+
* timer is currently active.
|
|
1187
|
+
*
|
|
1188
|
+
* @typeParam F - The type of the function to debounce.
|
|
1189
|
+
*
|
|
1190
|
+
* @param function_ - The function to debounce.
|
|
1191
|
+
* @param wait - The number of milliseconds to delay (default is 100ms).
|
|
1192
|
+
* @param options - An optional object with the following properties:
|
|
1193
|
+
* - `immediate` (boolean): If `true`, invokes the function on the leading edge
|
|
1194
|
+
* of the wait interval instead of the trailing edge.
|
|
1195
|
+
*
|
|
1196
|
+
* @returns A debounced version of the provided function, enhanced with the `isPending` property.
|
|
1197
|
+
*
|
|
1198
|
+
* @throws {TypeError} If the first parameter is not a function.
|
|
1199
|
+
* @throws {RangeError} If the `wait` parameter is negative.
|
|
1303
1200
|
*
|
|
1304
1201
|
* @example
|
|
1305
|
-
*
|
|
1306
|
-
*
|
|
1202
|
+
* ```ts
|
|
1203
|
+
* // Basic debouncing
|
|
1204
|
+
* const log = debounce((message: string) => console.log(message), 200);
|
|
1205
|
+
* log('Hello'); // Logs "Hello" after 200ms if no other call is made.
|
|
1206
|
+
* console.log(log.isPending); // true if the timer is active.
|
|
1207
|
+
*
|
|
1208
|
+
* // Immediate execution
|
|
1209
|
+
* const save = debounce(() => saveToServer(), 500, { immediate: true });
|
|
1210
|
+
* save(); // Executes immediately, then waits 500ms for subsequent calls
|
|
1211
|
+
*
|
|
1212
|
+
* // Check pending state
|
|
1213
|
+
* const debouncedSearch = debounce(searchAPI, 300);
|
|
1214
|
+
* debouncedSearch('query');
|
|
1215
|
+
* if (debouncedSearch.isPending) {
|
|
1216
|
+
* showLoadingIndicator();
|
|
1307
1217
|
* }
|
|
1218
|
+
* ```
|
|
1308
1219
|
*/
|
|
1309
|
-
declare
|
|
1220
|
+
declare function debounce<F$1 extends (...args: any[]) => any>(function_: F$1, wait?: number, options?: {
|
|
1221
|
+
immediate: boolean;
|
|
1222
|
+
}): DebouncedFunction<F$1>;
|
|
1223
|
+
type ThrottledFunction<F$1 extends (...args: any[]) => any> = {
|
|
1224
|
+
(...args: Parameters<F$1>): ReturnType<F$1> | undefined;
|
|
1225
|
+
readonly isPending: boolean;
|
|
1226
|
+
};
|
|
1310
1227
|
/**
|
|
1311
|
-
*
|
|
1228
|
+
* Creates a throttled function that invokes the provided function at most once
|
|
1229
|
+
* every `wait` milliseconds.
|
|
1312
1230
|
*
|
|
1313
|
-
*
|
|
1231
|
+
* If the `leading` option is set to `true`, the function will be invoked immediately
|
|
1232
|
+
* on the leading edge of the throttle interval. If the `trailing` option is set to `true`,
|
|
1233
|
+
* the function will also be invoked at the end of the throttle interval if additional
|
|
1234
|
+
* calls were made during the interval.
|
|
1314
1235
|
*
|
|
1315
|
-
*
|
|
1316
|
-
*
|
|
1236
|
+
* The returned function includes the `isPending` property to check if the throttle
|
|
1237
|
+
* timer is currently active.
|
|
1317
1238
|
*
|
|
1318
|
-
* @
|
|
1319
|
-
* isFiniteNumber(42); // true
|
|
1320
|
-
* isFiniteNumber(NaN); // false
|
|
1321
|
-
* isFiniteNumber(Infinity); // false
|
|
1322
|
-
*/
|
|
1323
|
-
declare const isFiniteNumber: (val: unknown) => val is number;
|
|
1324
|
-
/**
|
|
1325
|
-
* Type guard that checks if a value is an array.
|
|
1239
|
+
* @typeParam F - The type of the function to throttle.
|
|
1326
1240
|
*
|
|
1327
|
-
* @param
|
|
1328
|
-
* @
|
|
1241
|
+
* @param function_ - The function to throttle.
|
|
1242
|
+
* @param wait - The number of milliseconds to wait between invocations (default is 100ms).
|
|
1243
|
+
* @param options - An optional object with the following properties:
|
|
1244
|
+
* - `leading` (boolean): If `true`, invokes the function on the leading edge of the interval.
|
|
1245
|
+
* - `trailing` (boolean): If `true`, invokes the function on the trailing edge of the interval.
|
|
1246
|
+
*
|
|
1247
|
+
* @returns A throttled version of the provided function, enhanced with the `isPending` property.
|
|
1248
|
+
*
|
|
1249
|
+
* @throws {TypeError} If the first parameter is not a function.
|
|
1250
|
+
* @throws {RangeError} If the `wait` parameter is negative.
|
|
1329
1251
|
*
|
|
1330
1252
|
* @example
|
|
1331
|
-
*
|
|
1332
|
-
*
|
|
1333
|
-
*
|
|
1253
|
+
* ```ts
|
|
1254
|
+
* // Basic throttling (leading edge by default)
|
|
1255
|
+
* const log = throttle((message: string) => console.log(message), 200);
|
|
1256
|
+
* log('Hello'); // Logs "Hello" immediately
|
|
1257
|
+
* log('World'); // Ignored for 200ms
|
|
1258
|
+
* console.log(log.isPending); // true if within throttle window
|
|
1259
|
+
*
|
|
1260
|
+
* // Trailing edge only
|
|
1261
|
+
* const trailingLog = throttle(() => console.log('trailing'), 200, {
|
|
1262
|
+
* leading: false,
|
|
1263
|
+
* trailing: true
|
|
1264
|
+
* });
|
|
1265
|
+
* trailingLog(); // No immediate execution
|
|
1266
|
+
* // After 200ms: logs "trailing"
|
|
1267
|
+
*
|
|
1268
|
+
* // Both edges
|
|
1269
|
+
* const bothLog = throttle(() => console.log('both'), 200, {
|
|
1270
|
+
* leading: true,
|
|
1271
|
+
* trailing: true
|
|
1272
|
+
* });
|
|
1273
|
+
* bothLog(); // Immediate execution
|
|
1274
|
+
* // After 200ms: executes again if called during window
|
|
1275
|
+
* ```
|
|
1334
1276
|
*/
|
|
1335
|
-
declare
|
|
1277
|
+
declare function throttle<F$1 extends (...args: any[]) => any>(function_: F$1, wait?: number, options?: {
|
|
1278
|
+
leading?: boolean;
|
|
1279
|
+
trailing?: boolean;
|
|
1280
|
+
}): ThrottledFunction<F$1>;
|
|
1336
1281
|
/**
|
|
1337
|
-
*
|
|
1282
|
+
* Formats a string by replacing each '%s' placeholder with the corresponding argument.
|
|
1338
1283
|
*
|
|
1339
|
-
*
|
|
1340
|
-
*
|
|
1284
|
+
* Mimics the basic behavior of C's printf for %s substitution. Supports both
|
|
1285
|
+
* variadic arguments and array-based argument passing. Extra placeholders
|
|
1286
|
+
* are left as-is, missing arguments result in empty strings.
|
|
1287
|
+
*
|
|
1288
|
+
* @param format - The format string containing '%s' placeholders.
|
|
1289
|
+
* @param args - The values to substitute, either as separate arguments or a single array.
|
|
1290
|
+
* @returns The formatted string with placeholders replaced by arguments.
|
|
1341
1291
|
*
|
|
1342
1292
|
* @example
|
|
1343
|
-
*
|
|
1344
|
-
*
|
|
1345
|
-
*
|
|
1293
|
+
* ```ts
|
|
1294
|
+
* // Basic usage with separate arguments
|
|
1295
|
+
* printf("%s love %s", "I", "Bangladesh") // "I love Bangladesh"
|
|
1296
|
+
*
|
|
1297
|
+
* // Using array of arguments
|
|
1298
|
+
* printf("%s love %s", ["I", "Bangladesh"]) // "I love Bangladesh"
|
|
1299
|
+
*
|
|
1300
|
+
* // Extra placeholders remain unchanged
|
|
1301
|
+
* printf("%s %s %s", "Hello", "World") // "Hello World %s"
|
|
1302
|
+
*
|
|
1303
|
+
* // Missing arguments become empty strings
|
|
1304
|
+
* printf("%s and %s", "this") // "this and "
|
|
1305
|
+
*
|
|
1306
|
+
* // Multiple occurrences
|
|
1307
|
+
* printf("%s %s %s", "repeat", "repeat", "repeat") // "repeat repeat repeat"
|
|
1308
|
+
* ```
|
|
1346
1309
|
*/
|
|
1347
|
-
declare
|
|
1310
|
+
declare function printf(format: string, ...args: unknown[]): string;
|
|
1348
1311
|
/**
|
|
1349
|
-
*
|
|
1312
|
+
* Escapes a string for use in a regular expression.
|
|
1350
1313
|
*
|
|
1351
|
-
* @param
|
|
1352
|
-
* @returns
|
|
1314
|
+
* @param str - The string to escape
|
|
1315
|
+
* @returns - The escaped string safe for use in RegExp constructor
|
|
1353
1316
|
*
|
|
1354
1317
|
* @example
|
|
1355
|
-
*
|
|
1356
|
-
*
|
|
1357
|
-
*
|
|
1318
|
+
* ```ts
|
|
1319
|
+
* const escapedString = escapeRegExp('Hello, world!');
|
|
1320
|
+
* // escapedString === 'Hello\\, world!'
|
|
1321
|
+
*
|
|
1322
|
+
* const regex = new RegExp(escapeRegExp(userInput));
|
|
1323
|
+
* ```
|
|
1358
1324
|
*/
|
|
1359
|
-
declare
|
|
1325
|
+
declare function escapeRegExp(str: string): string;
|
|
1360
1326
|
/**
|
|
1361
|
-
*
|
|
1327
|
+
* Normalizes a string by:
|
|
1328
|
+
* - Applying Unicode normalization (NFC)
|
|
1329
|
+
* - Optionally removing diacritic marks (accents)
|
|
1330
|
+
* - Optionally trimming leading/trailing non-alphanumeric characters
|
|
1331
|
+
* - Optionally converting to lowercase
|
|
1362
1332
|
*
|
|
1363
|
-
* @param
|
|
1364
|
-
* @
|
|
1333
|
+
* @param str - The string to normalize
|
|
1334
|
+
* @param options - Normalization options
|
|
1335
|
+
* @param options.lowercase - Whether to convert the result to lowercase (default: true)
|
|
1336
|
+
* @param options.removeAccents - Whether to remove diacritic marks like accents (default: true)
|
|
1337
|
+
* @param options.removeNonAlphanumeric - Whether to trim non-alphanumeric characters from the edges (default: true)
|
|
1338
|
+
* @returns The normalized string
|
|
1365
1339
|
*
|
|
1366
1340
|
* @example
|
|
1367
|
-
*
|
|
1368
|
-
*
|
|
1369
|
-
*
|
|
1341
|
+
* ```ts
|
|
1342
|
+
* normalizeText('Café') // 'cafe'
|
|
1343
|
+
* normalizeText(' Hello! ') // 'hello'
|
|
1344
|
+
* normalizeText('José', { removeAccents: false }) // 'josé'
|
|
1345
|
+
* ```
|
|
1370
1346
|
*/
|
|
1371
|
-
declare function
|
|
1347
|
+
declare function normalizeText(str?: string | null, options?: {
|
|
1348
|
+
lowercase?: boolean;
|
|
1349
|
+
removeAccents?: boolean;
|
|
1350
|
+
removeNonAlphanumeric?: boolean;
|
|
1351
|
+
}): string;
|
|
1372
1352
|
//#endregion
|
|
1373
1353
|
export { AND, AllOrNone, BUFFER, ConcurrenceOptions, ConcurrenceResult, DeepMergeOptions, DeepPartial, DeepReadonly, DeepRequired, Diff, Falsy, IMPLIES, Intersection, Keys, KeysOfType, Maybe, Merge, 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, convertToNormalCase, convertToSlug, debounce, deepmerge, escapeRegExp, extendProps, getObjectValue, hydrate, isArray, isBoolean, isFalsy, isFiniteNumber, isFunction, isNullish, isPlainObject, isPrimitive, isString, normalizeText, poll, printf, schedule, shield, sleep, throttle, withConcurrency };
|