@ts-utilities/core 1.0.12 → 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 +897 -916
- package/dist/index.d.ts +897 -916
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -142,1231 +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 AwaitedTuple<T extends readonly (() => Promise<any>)[] | []> = { -readonly [P in keyof T]: Awaited<ReturnType<T[P]>> };
|
|
347
|
-
type ObjectValues<T extends Record<string, () => Promise<any>>> = { [K in keyof T]: Awaited<ReturnType<T[K]>> };
|
|
348
|
-
type ConcurrenceResult<T> = {
|
|
349
|
-
results: T;
|
|
350
|
-
errors: Error[];
|
|
351
|
-
succeeded: number;
|
|
352
|
-
failed: number;
|
|
353
|
-
duration: number;
|
|
354
|
-
};
|
|
355
|
-
type ConcurrenceOptions = {
|
|
356
|
-
concurrency?: number;
|
|
357
|
-
timeout?: number;
|
|
358
|
-
signal?: AbortSignal;
|
|
359
|
-
retry?: number;
|
|
360
|
-
retryDelay?: number;
|
|
361
|
-
throwOnFirstError?: boolean;
|
|
362
|
-
ignoreErrors?: boolean;
|
|
363
|
-
};
|
|
364
|
-
declare function withConcurrency<T extends readonly (() => Promise<any>)[] | []>(tasks: T, options?: ConcurrenceOptions): Promise<ConcurrenceResult<AwaitedTuple<T>>>;
|
|
365
|
-
declare function withConcurrency<T extends Record<string, () => Promise<any>>>(tasks: T, options?: ConcurrenceOptions): Promise<ConcurrenceResult<ObjectValues<T>>>;
|
|
366
|
-
//#endregion
|
|
367
|
-
//#region src/functions/schedule.d.ts
|
|
368
|
-
/**
|
|
369
|
-
* A task function that can be synchronous or asynchronous.
|
|
370
|
-
*/
|
|
371
|
-
type Task = () => Promise<void> | void;
|
|
372
|
-
/**
|
|
373
|
-
* Options for configuring the schedule function.
|
|
374
|
-
*/
|
|
375
|
-
interface ScheduleOpts {
|
|
376
|
-
/** Number of retry attempts on failure. Defaults to 0. */
|
|
377
|
-
retry?: number;
|
|
378
|
-
/** Delay in milliseconds between retries. Defaults to 0. */
|
|
379
|
-
delay?: number;
|
|
380
|
-
/** Maximum time in milliseconds to wait for the task to complete. */
|
|
381
|
-
timeout?: number;
|
|
382
|
-
}
|
|
320
|
+
type Maybe<T> = T extends object ? { [P in keyof T]?: Nullish<T[P]> } : T | null | undefined;
|
|
383
321
|
/**
|
|
384
|
-
*
|
|
322
|
+
* Makes all properties of an object type readonly recursively.
|
|
385
323
|
*
|
|
386
|
-
*
|
|
387
|
-
*
|
|
388
|
-
* 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.
|
|
389
326
|
*
|
|
390
|
-
* @
|
|
391
|
-
* @
|
|
327
|
+
* @template T - The type to make deeply readonly
|
|
328
|
+
* @returns A type with all properties readonly recursively
|
|
392
329
|
*
|
|
393
330
|
* @example
|
|
394
331
|
* ```ts
|
|
395
|
-
*
|
|
396
|
-
*
|
|
397
|
-
*
|
|
398
|
-
* }
|
|
332
|
+
* type Config = {
|
|
333
|
+
* server: { host: string; port: number };
|
|
334
|
+
* features: string[];
|
|
335
|
+
* };
|
|
399
336
|
*
|
|
400
|
-
*
|
|
401
|
-
*
|
|
402
|
-
*
|
|
403
|
-
*
|
|
404
|
-
*
|
|
337
|
+
* type ReadonlyConfig = DeepReadonly<Config>;
|
|
338
|
+
* // {
|
|
339
|
+
* // readonly server: { readonly host: string; readonly port: number };
|
|
340
|
+
* // readonly features: readonly string[];
|
|
341
|
+
* // }
|
|
405
342
|
* ```
|
|
406
343
|
*/
|
|
407
|
-
|
|
408
|
-
//#endregion
|
|
409
|
-
//#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;
|
|
410
345
|
/**
|
|
411
|
-
*
|
|
346
|
+
* Removes readonly modifier from all properties of an object type recursively.
|
|
412
347
|
*
|
|
413
|
-
*
|
|
414
|
-
*
|
|
415
|
-
* - `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
|
|
416
350
|
*
|
|
417
351
|
* @example
|
|
418
352
|
* ```ts
|
|
419
|
-
*
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
*
|
|
427
|
-
* console.log('Result:', value);
|
|
428
|
-
* }
|
|
429
|
-
*
|
|
430
|
-
* // Asynchronous error handling
|
|
431
|
-
* const [asyncErr, result] = await shield(async () => {
|
|
432
|
-
* const response = await fetch('/api/data');
|
|
433
|
-
* if (!response.ok) throw new Error('API error');
|
|
434
|
-
* return response.json();
|
|
435
|
-
* });
|
|
436
|
-
* if (asyncErr) {
|
|
437
|
-
* console.error('API call failed:', asyncErr);
|
|
438
|
-
* } else {
|
|
439
|
-
* processData(result);
|
|
440
|
-
* }
|
|
353
|
+
* type ReadonlyUser = { readonly name: string; readonly profile: { readonly age: number } };
|
|
354
|
+
* type MutableUser = Mutable<ReadonlyUser>;
|
|
355
|
+
* // { name: string; profile: { age: number } }
|
|
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.
|
|
441
361
|
*
|
|
442
|
-
*
|
|
443
|
-
*
|
|
444
|
-
*
|
|
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
|
|
445
365
|
*
|
|
446
|
-
*
|
|
447
|
-
*
|
|
448
|
-
*
|
|
449
|
-
*
|
|
450
|
-
*
|
|
451
|
-
* }
|
|
366
|
+
* @example
|
|
367
|
+
* ```ts
|
|
368
|
+
* type User = { name: string; age: number; active: boolean };
|
|
369
|
+
* type StringKeys = KeysOfType<User, string>; // 'name'
|
|
370
|
+
* type NumberKeys = KeysOfType<User, number>; // 'age'
|
|
452
371
|
* ```
|
|
372
|
+
*/
|
|
373
|
+
type KeysOfType<T, U$1> = { [K in keyof T]: T[K] extends U$1 ? K : never }[keyof T];
|
|
374
|
+
/**
|
|
375
|
+
* Omits properties from an object type that have values of a specific type.
|
|
376
|
+
*
|
|
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
|
|
453
380
|
*
|
|
454
381
|
* @example
|
|
455
382
|
* ```ts
|
|
456
|
-
*
|
|
457
|
-
*
|
|
458
|
-
*
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
*
|
|
463
|
-
* return result;
|
|
464
|
-
* }
|
|
383
|
+
* type Mixed = { name: string; age: number; active: boolean };
|
|
384
|
+
* type WithoutStrings = OmitByType<Mixed, string>; // { age: number; active: boolean }
|
|
385
|
+
* ```
|
|
386
|
+
*/
|
|
387
|
+
type OmitByType<T, U$1> = { [K in keyof T as T[K] extends U$1 ? never : K]: T[K] };
|
|
388
|
+
/**
|
|
389
|
+
* Makes specified properties required while keeping others as-is.
|
|
465
390
|
*
|
|
466
|
-
*
|
|
467
|
-
*
|
|
468
|
-
*
|
|
469
|
-
*
|
|
470
|
-
*
|
|
471
|
-
*
|
|
472
|
-
*
|
|
473
|
-
*
|
|
474
|
-
* }
|
|
391
|
+
* @template T - The base object type
|
|
392
|
+
* @template K - The keys to make required
|
|
393
|
+
* @returns An object type with specified properties required
|
|
394
|
+
*
|
|
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 }
|
|
475
400
|
* ```
|
|
476
401
|
*/
|
|
477
|
-
|
|
478
|
-
declare function shield<T, E = Error>(operation: () => T): [E | null, T | null];
|
|
479
|
-
//#endregion
|
|
480
|
-
//#region src/functions/utils-core.d.ts
|
|
402
|
+
type RequiredKeys<T, K$1 extends keyof T> = Omit<T, K$1> & Required<Pick<T, K$1>>;
|
|
481
403
|
/**
|
|
482
|
-
*
|
|
404
|
+
* Computes the symmetric difference between two object types.
|
|
483
405
|
*
|
|
484
|
-
*
|
|
485
|
-
* - Adding spaces between words
|
|
486
|
-
* - Capitalizing the first letter of each word
|
|
487
|
-
* - Handling common separators (-, _, camelCase boundaries)
|
|
406
|
+
* Properties that exist in either T or U but not in both.
|
|
488
407
|
*
|
|
489
|
-
* @
|
|
490
|
-
* @
|
|
408
|
+
* @template T - First object type
|
|
409
|
+
* @template U - Second object type
|
|
410
|
+
* @returns Properties unique to T or U
|
|
491
411
|
*
|
|
492
412
|
* @example
|
|
493
413
|
* ```ts
|
|
494
|
-
*
|
|
495
|
-
*
|
|
496
|
-
*
|
|
497
|
-
* convertToNormalCase('PascalCase') // 'Pascal Case'
|
|
414
|
+
* type A = { x: number; y: string };
|
|
415
|
+
* type B = { y: string; z: boolean };
|
|
416
|
+
* type DiffAB = Diff<A, B>; // { x: number; z: boolean }
|
|
498
417
|
* ```
|
|
499
418
|
*/
|
|
500
|
-
|
|
419
|
+
type Diff<T, U$1> = Omit<T, keyof U$1> & Omit<U$1, keyof T>;
|
|
501
420
|
/**
|
|
502
|
-
*
|
|
503
|
-
*
|
|
504
|
-
* @
|
|
505
|
-
* @
|
|
421
|
+
* Computes the intersection of two object types (properties present in both).
|
|
422
|
+
*
|
|
423
|
+
* @template T - First object type
|
|
424
|
+
* @template U - Second object type
|
|
425
|
+
* @returns Properties that exist in both T and U
|
|
426
|
+
*
|
|
506
427
|
* @example
|
|
507
|
-
*
|
|
508
|
-
*
|
|
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
|
+
* ```
|
|
509
433
|
*/
|
|
510
|
-
|
|
434
|
+
type Intersection<T extends object, U$1 extends object> = Pick<T, Extract<keyof T, keyof U$1> & Extract<keyof U$1, keyof T>>;
|
|
511
435
|
/**
|
|
512
|
-
*
|
|
436
|
+
* Merges two object types, combining their properties.
|
|
513
437
|
*
|
|
514
|
-
*
|
|
438
|
+
* @template T - First object type
|
|
439
|
+
* @template U - Second object type
|
|
440
|
+
* @returns A merged object type with properties from both
|
|
515
441
|
*
|
|
516
|
-
* @
|
|
517
|
-
*
|
|
518
|
-
*
|
|
442
|
+
* @example
|
|
443
|
+
* ```ts
|
|
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 }
|
|
447
|
+
* ```
|
|
519
448
|
*/
|
|
520
|
-
|
|
521
|
-
type DebouncedFunction<F$1 extends (...args: any[]) => any> = {
|
|
522
|
-
(...args: Parameters<F$1>): ReturnType<F$1> | undefined;
|
|
523
|
-
readonly isPending: boolean;
|
|
524
|
-
};
|
|
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>;
|
|
525
450
|
/**
|
|
526
|
-
*
|
|
527
|
-
* after the specified `wait` time has elapsed since the last invocation.
|
|
451
|
+
* Subtracts properties of one object type from another.
|
|
528
452
|
*
|
|
529
|
-
*
|
|
530
|
-
*
|
|
531
|
-
*
|
|
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
|
|
532
456
|
*
|
|
533
|
-
*
|
|
534
|
-
*
|
|
457
|
+
* @example
|
|
458
|
+
* ```ts
|
|
459
|
+
* type A = { x: number; y: string; z: boolean };
|
|
460
|
+
* type B = { y: string };
|
|
461
|
+
* type Subtracted = Substract<A, B>; // { x: number; z: boolean }
|
|
462
|
+
* ```
|
|
463
|
+
*/
|
|
464
|
+
type Substract<T extends object, U$1 extends object> = Omit<T, keyof U$1>;
|
|
465
|
+
/**
|
|
466
|
+
* Represents either all properties present or none of them.
|
|
535
467
|
*
|
|
536
|
-
*
|
|
537
|
-
*
|
|
538
|
-
* @param function_ - The function to debounce.
|
|
539
|
-
* @param wait - The number of milliseconds to delay (default is 100ms).
|
|
540
|
-
* @param options - An optional object with the following properties:
|
|
541
|
-
* - `immediate` (boolean): If `true`, invokes the function on the leading edge
|
|
542
|
-
* of the wait interval instead of the trailing edge.
|
|
543
|
-
*
|
|
544
|
-
* @returns A debounced version of the provided function, enhanced with the `isPending` property.
|
|
468
|
+
* Useful for creating mutually exclusive configurations.
|
|
545
469
|
*
|
|
546
|
-
* @
|
|
547
|
-
* @
|
|
470
|
+
* @template T - The object type
|
|
471
|
+
* @returns Either the full object or an empty object with optional properties
|
|
548
472
|
*
|
|
549
473
|
* @example
|
|
550
474
|
* ```ts
|
|
551
|
-
*
|
|
552
|
-
*
|
|
553
|
-
*
|
|
554
|
-
* console.log(log.isPending); // true if the timer is active.
|
|
555
|
-
*
|
|
556
|
-
* // Immediate execution
|
|
557
|
-
* const save = debounce(() => saveToServer(), 500, { immediate: true });
|
|
558
|
-
* save(); // Executes immediately, then waits 500ms for subsequent calls
|
|
559
|
-
*
|
|
560
|
-
* // Check pending state
|
|
561
|
-
* const debouncedSearch = debounce(searchAPI, 300);
|
|
562
|
-
* debouncedSearch('query');
|
|
563
|
-
* if (debouncedSearch.isPending) {
|
|
564
|
-
* showLoadingIndicator();
|
|
565
|
-
* }
|
|
475
|
+
* type Config = { host: string; port: number };
|
|
476
|
+
* type AllOrNoneConfig = AllOrNone<Config>;
|
|
477
|
+
* // { host: string; port: number } | {}
|
|
566
478
|
* ```
|
|
567
479
|
*/
|
|
568
|
-
|
|
569
|
-
immediate: boolean;
|
|
570
|
-
}): DebouncedFunction<F$1>;
|
|
571
|
-
type ThrottledFunction<F$1 extends (...args: any[]) => any> = {
|
|
572
|
-
(...args: Parameters<F$1>): ReturnType<F$1> | undefined;
|
|
573
|
-
readonly isPending: boolean;
|
|
574
|
-
};
|
|
480
|
+
type AllOrNone<T> = T | { [P in keyof T]?: never };
|
|
575
481
|
/**
|
|
576
|
-
*
|
|
577
|
-
* every `wait` milliseconds.
|
|
578
|
-
*
|
|
579
|
-
* If the `leading` option is set to `true`, the function will be invoked immediately
|
|
580
|
-
* on the leading edge of the throttle interval. If the `trailing` option is set to `true`,
|
|
581
|
-
* the function will also be invoked at the end of the throttle interval if additional
|
|
582
|
-
* calls were made during the interval.
|
|
583
|
-
*
|
|
584
|
-
* The returned function includes the `isPending` property to check if the throttle
|
|
585
|
-
* timer is currently active.
|
|
586
|
-
*
|
|
587
|
-
* @typeParam F - The type of the function to throttle.
|
|
588
|
-
*
|
|
589
|
-
* @param function_ - The function to throttle.
|
|
590
|
-
* @param wait - The number of milliseconds to wait between invocations (default is 100ms).
|
|
591
|
-
* @param options - An optional object with the following properties:
|
|
592
|
-
* - `leading` (boolean): If `true`, invokes the function on the leading edge of the interval.
|
|
593
|
-
* - `trailing` (boolean): If `true`, invokes the function on the trailing edge of the interval.
|
|
482
|
+
* Represents exactly one property from an object type being present.
|
|
594
483
|
*
|
|
595
|
-
*
|
|
484
|
+
* Useful for creating discriminated unions or mutually exclusive options.
|
|
596
485
|
*
|
|
597
|
-
* @
|
|
598
|
-
* @
|
|
486
|
+
* @template T - The object type
|
|
487
|
+
* @returns A union where only one property is present at a time
|
|
599
488
|
*
|
|
600
489
|
* @example
|
|
601
490
|
* ```ts
|
|
602
|
-
*
|
|
603
|
-
*
|
|
604
|
-
*
|
|
605
|
-
* log('World'); // Ignored for 200ms
|
|
606
|
-
* console.log(log.isPending); // true if within throttle window
|
|
607
|
-
*
|
|
608
|
-
* // Trailing edge only
|
|
609
|
-
* const trailingLog = throttle(() => console.log('trailing'), 200, {
|
|
610
|
-
* leading: false,
|
|
611
|
-
* trailing: true
|
|
612
|
-
* });
|
|
613
|
-
* trailingLog(); // No immediate execution
|
|
614
|
-
* // After 200ms: logs "trailing"
|
|
615
|
-
*
|
|
616
|
-
* // Both edges
|
|
617
|
-
* const bothLog = throttle(() => console.log('both'), 200, {
|
|
618
|
-
* leading: true,
|
|
619
|
-
* trailing: true
|
|
620
|
-
* });
|
|
621
|
-
* bothLog(); // Immediate execution
|
|
622
|
-
* // After 200ms: executes again if called during window
|
|
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 }
|
|
623
494
|
* ```
|
|
624
495
|
*/
|
|
625
|
-
|
|
626
|
-
leading?: boolean;
|
|
627
|
-
trailing?: boolean;
|
|
628
|
-
}): ThrottledFunction<F$1>;
|
|
496
|
+
type OneOf<T> = { [K in keyof T]: Pick<T, K> }[keyof T];
|
|
629
497
|
/**
|
|
630
|
-
*
|
|
631
|
-
*
|
|
632
|
-
* Mimics the basic behavior of C's printf for %s substitution. Supports both
|
|
633
|
-
* variadic arguments and array-based argument passing. Extra placeholders
|
|
634
|
-
* are left as-is, missing arguments result in empty strings.
|
|
498
|
+
* Represents exactly two properties from an object type being present.
|
|
635
499
|
*
|
|
636
|
-
* @
|
|
637
|
-
* @
|
|
638
|
-
* @returns The formatted string with placeholders replaced by arguments.
|
|
500
|
+
* @template T - The object type
|
|
501
|
+
* @returns A union where exactly two properties are present at a time
|
|
639
502
|
*
|
|
640
503
|
* @example
|
|
641
504
|
* ```ts
|
|
642
|
-
*
|
|
643
|
-
*
|
|
644
|
-
*
|
|
645
|
-
* // Using array of arguments
|
|
646
|
-
* printf("%s love %s", ["I", "Bangladesh"]) // "I love Bangladesh"
|
|
647
|
-
*
|
|
648
|
-
* // Extra placeholders remain unchanged
|
|
649
|
-
* printf("%s %s %s", "Hello", "World") // "Hello World %s"
|
|
650
|
-
*
|
|
651
|
-
* // Missing arguments become empty strings
|
|
652
|
-
* printf("%s and %s", "this") // "this and "
|
|
653
|
-
*
|
|
654
|
-
* // Multiple occurrences
|
|
655
|
-
* printf("%s %s %s", "repeat", "repeat", "repeat") // "repeat repeat repeat"
|
|
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 }
|
|
656
508
|
* ```
|
|
657
509
|
*/
|
|
658
|
-
|
|
510
|
+
type TwoOf<T> = { [K in keyof T]: { [L in Exclude<keyof T, K>]: Pick<T, K | L> }[Exclude<keyof T, K>] }[keyof T];
|
|
659
511
|
/**
|
|
660
|
-
*
|
|
512
|
+
* Prettifies a complex type by expanding it for better readability in tooltips.
|
|
661
513
|
*
|
|
662
|
-
*
|
|
663
|
-
*
|
|
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
|
|
664
518
|
*
|
|
665
519
|
* @example
|
|
666
520
|
* ```ts
|
|
667
|
-
*
|
|
668
|
-
* //
|
|
669
|
-
*
|
|
670
|
-
* const regex = new RegExp(escapeRegExp(userInput));
|
|
521
|
+
* type Complex = { a: string } & { b: number };
|
|
522
|
+
* type PrettyComplex = Prettify<Complex>; // Shows as { a: string; b: number }
|
|
671
523
|
* ```
|
|
672
524
|
*/
|
|
673
|
-
|
|
525
|
+
type Prettify<T> = T extends infer U ? U extends object ? { [K in keyof U]: U[K] } & {} : U : never;
|
|
674
526
|
/**
|
|
675
|
-
*
|
|
676
|
-
* - Applying Unicode normalization (NFC)
|
|
677
|
-
* - Optionally removing diacritic marks (accents)
|
|
678
|
-
* - Optionally trimming leading/trailing non-alphanumeric characters
|
|
679
|
-
* - Optionally converting to lowercase
|
|
527
|
+
* Extracts all nested keys of an object type as dot-separated strings.
|
|
680
528
|
*
|
|
681
|
-
* @
|
|
682
|
-
* @
|
|
683
|
-
* @
|
|
684
|
-
* @param options.removeAccents - Whether to remove diacritic marks like accents (default: true)
|
|
685
|
-
* @param options.removeNonAlphanumeric - Whether to trim non-alphanumeric characters from the edges (default: true)
|
|
686
|
-
* @returns The normalized string
|
|
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
|
|
687
532
|
*
|
|
688
533
|
* @example
|
|
689
534
|
* ```ts
|
|
690
|
-
*
|
|
691
|
-
*
|
|
692
|
-
*
|
|
535
|
+
* interface User {
|
|
536
|
+
* name: string;
|
|
537
|
+
* profile: { age: number; address: { city: string } };
|
|
538
|
+
* tags: string[];
|
|
539
|
+
* }
|
|
540
|
+
*
|
|
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`
|
|
693
548
|
* ```
|
|
694
549
|
*/
|
|
695
|
-
|
|
696
|
-
lowercase?: boolean;
|
|
697
|
-
removeAccents?: boolean;
|
|
698
|
-
removeNonAlphanumeric?: boolean;
|
|
699
|
-
}): string;
|
|
700
|
-
//#endregion
|
|
701
|
-
//#region src/types/utilities.d.ts
|
|
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];
|
|
702
551
|
/**
|
|
703
|
-
*
|
|
704
|
-
|
|
705
|
-
*
|
|
706
|
-
|
|
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
|
|
707
559
|
*
|
|
560
|
+
|
|
708
561
|
* @example
|
|
709
562
|
* ```ts
|
|
710
|
-
* type
|
|
711
|
-
* type
|
|
563
|
+
* type A = { x: number; y: string };
|
|
564
|
+
* type B = { y: string };
|
|
565
|
+
* type Result = Without<A, B>; // { x?: never }
|
|
712
566
|
* ```
|
|
713
567
|
*/
|
|
714
|
-
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>);
|
|
715
575
|
/**
|
|
716
|
-
*
|
|
576
|
+
* Computes a type-level AND (all must true) for a tuple of types.
|
|
717
577
|
*
|
|
718
|
-
*
|
|
719
|
-
* @returns A union of all values in the object type
|
|
578
|
+
* Truth table for 3 arguments:
|
|
720
579
|
*
|
|
721
|
-
*
|
|
722
|
-
*
|
|
723
|
-
*
|
|
724
|
-
*
|
|
725
|
-
*
|
|
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
|
|
589
|
+
*
|
|
590
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
726
591
|
*/
|
|
727
|
-
type
|
|
592
|
+
type AND<T extends any[]> = T extends [infer F, ...infer R] ? R extends any[] ? F & AND<R> : F : unknown;
|
|
728
593
|
/**
|
|
729
|
-
*
|
|
730
|
-
*
|
|
731
|
-
* This type traverses through nested objects and arrays, making all properties optional.
|
|
732
|
-
* Functions and primitives are left unchanged.
|
|
594
|
+
* Computes a type-level OR (At least one) for a tuple of types.
|
|
733
595
|
*
|
|
734
|
-
*
|
|
735
|
-
* @returns A type with all properties optional recursively
|
|
596
|
+
* Truth table for 3 arguments:
|
|
736
597
|
*
|
|
737
|
-
*
|
|
738
|
-
*
|
|
739
|
-
*
|
|
740
|
-
*
|
|
741
|
-
*
|
|
742
|
-
*
|
|
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
|
|
743
607
|
*
|
|
744
|
-
*
|
|
745
|
-
* // {
|
|
746
|
-
* // server?: { host?: string; port?: number };
|
|
747
|
-
* // features?: string[];
|
|
748
|
-
* // }
|
|
749
|
-
* ```
|
|
608
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
750
609
|
*/
|
|
751
|
-
type
|
|
610
|
+
type OR<T extends any[]> = T extends [infer F, ...infer R] ? R extends any[] ? F | OR<R> : F : never;
|
|
752
611
|
/**
|
|
753
|
-
*
|
|
612
|
+
* Computes a type-level XOR (only one/odd) for a tuple of types.
|
|
754
613
|
*
|
|
755
|
-
*
|
|
756
|
-
* @template K - The keys to make optional
|
|
757
|
-
* @returns An object type with specified properties optional
|
|
614
|
+
* Truth table for 3 arguments:
|
|
758
615
|
*
|
|
759
|
-
*
|
|
760
|
-
*
|
|
761
|
-
*
|
|
762
|
-
*
|
|
763
|
-
*
|
|
764
|
-
*
|
|
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
|
|
625
|
+
*
|
|
626
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
765
627
|
*/
|
|
766
|
-
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;
|
|
767
629
|
/**
|
|
768
|
-
*
|
|
630
|
+
* Computes a type-level XNOR (All or None true) for a tuple of types.
|
|
769
631
|
*
|
|
770
|
-
*
|
|
771
|
-
* Functions and primitives are left unchanged.
|
|
632
|
+
* Truth table for 3 arguments:
|
|
772
633
|
*
|
|
773
|
-
*
|
|
774
|
-
*
|
|
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
|
|
775
643
|
*
|
|
776
|
-
* @
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
*
|
|
644
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
645
|
+
*/
|
|
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;
|
|
647
|
+
/**
|
|
648
|
+
* Computes a type-level NOT for a tuple of types.
|
|
781
649
|
*
|
|
782
|
-
*
|
|
783
|
-
*
|
|
784
|
-
*
|
|
785
|
-
*
|
|
786
|
-
*
|
|
650
|
+
* Truth table for 3 arguments:
|
|
651
|
+
*
|
|
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)
|
|
787
663
|
*/
|
|
788
|
-
type
|
|
664
|
+
type NOT<T> = { [P in keyof T]?: never };
|
|
789
665
|
/**
|
|
790
|
-
*
|
|
666
|
+
* Computes a type-level NAND for a tuple of types.
|
|
791
667
|
*
|
|
792
|
-
*
|
|
793
|
-
* @template K - The keys to make required
|
|
794
|
-
* @returns An object type with specified properties required
|
|
668
|
+
* Truth table for 3 arguments:
|
|
795
669
|
*
|
|
796
|
-
*
|
|
797
|
-
*
|
|
798
|
-
*
|
|
799
|
-
*
|
|
800
|
-
*
|
|
801
|
-
*
|
|
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
|
|
679
|
+
*
|
|
680
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
802
681
|
*/
|
|
803
|
-
type
|
|
682
|
+
type NAND<T extends any[]> = NOT<AND<T>>;
|
|
804
683
|
/**
|
|
805
|
-
*
|
|
684
|
+
* Computes a type-level NOR for a tuple of types.
|
|
806
685
|
*
|
|
807
|
-
*
|
|
686
|
+
* Truth table for 3 arguments:
|
|
808
687
|
*
|
|
809
|
-
*
|
|
810
|
-
*
|
|
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
|
|
811
697
|
*
|
|
812
|
-
* @
|
|
813
|
-
* ```ts
|
|
814
|
-
* type User = { name: string; age: number };
|
|
815
|
-
* type ExcludedUser = Never<User>; // { name: never; age: never }
|
|
816
|
-
* ```
|
|
698
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
817
699
|
*/
|
|
818
|
-
type
|
|
700
|
+
type NOR<T extends any[]> = NOT<OR<T>>;
|
|
701
|
+
//#endregion
|
|
702
|
+
//#region src/types/guards.d.ts
|
|
819
703
|
/**
|
|
820
|
-
*
|
|
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.
|
|
821
713
|
*
|
|
822
|
-
* @
|
|
823
|
-
* @returns
|
|
714
|
+
* @param val - The value to check
|
|
715
|
+
* @returns True if the value is falsy, false otherwise
|
|
824
716
|
*
|
|
825
717
|
* @example
|
|
826
|
-
*
|
|
827
|
-
*
|
|
828
|
-
*
|
|
829
|
-
* // { name: string | null; profile: { age: number | null } | null }
|
|
830
|
-
* ```
|
|
718
|
+
* if (isFalsy(value)) {
|
|
719
|
+
* console.log('Value is falsy');
|
|
720
|
+
* }
|
|
831
721
|
*/
|
|
832
|
-
|
|
722
|
+
declare const isFalsy: (val: unknown) => val is Falsy;
|
|
833
723
|
/**
|
|
834
|
-
*
|
|
724
|
+
* Type guard that checks if a value is null or undefined.
|
|
835
725
|
*
|
|
836
|
-
* @
|
|
837
|
-
* @returns
|
|
726
|
+
* @param val - The value to check
|
|
727
|
+
* @returns True if the value is null or undefined, false otherwise
|
|
838
728
|
*
|
|
839
729
|
* @example
|
|
840
|
-
*
|
|
841
|
-
*
|
|
842
|
-
*
|
|
843
|
-
* // { name: string | undefined; profile: { age: number | undefined } | undefined }
|
|
844
|
-
* ```
|
|
730
|
+
* if (isNullish(value)) {
|
|
731
|
+
* console.log('Value is null or undefined');
|
|
732
|
+
* }
|
|
845
733
|
*/
|
|
846
|
-
|
|
734
|
+
declare const isNullish: (val: unknown) => val is null | undefined;
|
|
847
735
|
/**
|
|
848
|
-
*
|
|
736
|
+
* Type guard that checks if a value is a boolean.
|
|
849
737
|
*
|
|
850
|
-
* @
|
|
851
|
-
* @returns
|
|
738
|
+
* @param val - The value to check
|
|
739
|
+
* @returns True if the value is a boolean, false otherwise
|
|
852
740
|
*
|
|
853
741
|
* @example
|
|
854
|
-
*
|
|
855
|
-
*
|
|
856
|
-
*
|
|
857
|
-
* // { name: string | null | undefined; profile: { age: number | null | undefined } | null | undefined }
|
|
858
|
-
* ```
|
|
742
|
+
* if (isBoolean(value)) {
|
|
743
|
+
* console.log('Value is a boolean');
|
|
744
|
+
* }
|
|
859
745
|
*/
|
|
860
|
-
|
|
746
|
+
declare const isBoolean: (val: unknown) => val is boolean;
|
|
861
747
|
/**
|
|
862
|
-
*
|
|
863
|
-
*
|
|
864
|
-
* This combines optional properties with nullish values.
|
|
748
|
+
* Type guard that checks if a value is a string.
|
|
865
749
|
*
|
|
866
|
-
* @
|
|
867
|
-
* @returns
|
|
750
|
+
* @param val - The value to check
|
|
751
|
+
* @returns True if the value is a string, false otherwise
|
|
868
752
|
*
|
|
869
753
|
* @example
|
|
870
|
-
*
|
|
871
|
-
*
|
|
872
|
-
*
|
|
873
|
-
* // { name?: string | null | undefined; profile?: { age?: number | null | undefined } | null | undefined }
|
|
874
|
-
* ```
|
|
754
|
+
* if (isString(value)) {
|
|
755
|
+
* console.log('Value is a string');
|
|
756
|
+
* }
|
|
875
757
|
*/
|
|
876
|
-
|
|
758
|
+
declare const isString: (val: unknown) => val is string;
|
|
877
759
|
/**
|
|
878
|
-
*
|
|
760
|
+
* Type guard that checks whether a value is a finite number.
|
|
879
761
|
*
|
|
880
|
-
* This
|
|
881
|
-
* Functions and primitives are left unchanged.
|
|
762
|
+
* This excludes `NaN`, `Infinity`, and `-Infinity`.
|
|
882
763
|
*
|
|
883
|
-
* @
|
|
884
|
-
* @returns
|
|
764
|
+
* @param val - The value to check
|
|
765
|
+
* @returns `true` if the value is a finite number
|
|
885
766
|
*
|
|
886
767
|
* @example
|
|
887
|
-
*
|
|
888
|
-
*
|
|
889
|
-
*
|
|
890
|
-
* features: string[];
|
|
891
|
-
* };
|
|
892
|
-
*
|
|
893
|
-
* type ReadonlyConfig = DeepReadonly<Config>;
|
|
894
|
-
* // {
|
|
895
|
-
* // readonly server: { readonly host: string; readonly port: number };
|
|
896
|
-
* // readonly features: readonly string[];
|
|
897
|
-
* // }
|
|
898
|
-
* ```
|
|
768
|
+
* isFiniteNumber(42); // true
|
|
769
|
+
* isFiniteNumber(NaN); // false
|
|
770
|
+
* isFiniteNumber(Infinity); // false
|
|
899
771
|
*/
|
|
900
|
-
|
|
772
|
+
declare const isFiniteNumber: (val: unknown) => val is number;
|
|
901
773
|
/**
|
|
902
|
-
*
|
|
774
|
+
* Type guard that checks if a value is an array.
|
|
903
775
|
*
|
|
904
|
-
* @
|
|
905
|
-
* @returns
|
|
776
|
+
* @param val - The value to check
|
|
777
|
+
* @returns True if the value is an array, false otherwise
|
|
906
778
|
*
|
|
907
779
|
* @example
|
|
908
|
-
*
|
|
909
|
-
*
|
|
910
|
-
*
|
|
911
|
-
* // { name: string; profile: { age: number } }
|
|
912
|
-
* ```
|
|
780
|
+
* if (isArray(value)) {
|
|
781
|
+
* console.log('Value is an array');
|
|
782
|
+
* }
|
|
913
783
|
*/
|
|
914
|
-
|
|
784
|
+
declare const isArray: (val: unknown) => val is unknown[];
|
|
915
785
|
/**
|
|
916
|
-
*
|
|
786
|
+
* Type guard that checks if a value is a function.
|
|
917
787
|
*
|
|
918
|
-
* @
|
|
919
|
-
* @
|
|
920
|
-
* @returns A union of keys whose values match the specified type
|
|
788
|
+
* @param val - The value to check
|
|
789
|
+
* @returns True if the value is a function, false otherwise
|
|
921
790
|
*
|
|
922
791
|
* @example
|
|
923
|
-
*
|
|
924
|
-
*
|
|
925
|
-
*
|
|
926
|
-
* type NumberKeys = KeysOfType<User, number>; // 'age'
|
|
927
|
-
* ```
|
|
792
|
+
* if (isFunction(value)) {
|
|
793
|
+
* console.log('Value is a function');
|
|
794
|
+
* }
|
|
928
795
|
*/
|
|
929
|
-
|
|
796
|
+
declare const isFunction: (val: unknown) => val is Function;
|
|
930
797
|
/**
|
|
931
|
-
*
|
|
798
|
+
* Type guard that checks if a value is a primitive type.
|
|
932
799
|
*
|
|
933
|
-
* @
|
|
934
|
-
* @
|
|
935
|
-
* @returns An object type without properties of the specified value type
|
|
800
|
+
* @param val - The value to check
|
|
801
|
+
* @returns True if the value is a primitive, false otherwise
|
|
936
802
|
*
|
|
937
803
|
* @example
|
|
938
|
-
*
|
|
939
|
-
*
|
|
940
|
-
*
|
|
941
|
-
* ```
|
|
804
|
+
* if (isPrimitive(value)) {
|
|
805
|
+
* console.log('Value is a primitive type');
|
|
806
|
+
* }
|
|
942
807
|
*/
|
|
943
|
-
|
|
808
|
+
declare const isPrimitive: (val: unknown) => val is Primitive;
|
|
944
809
|
/**
|
|
945
|
-
*
|
|
810
|
+
* Type guard that checks if a value is a plain object (not an array, function, etc.).
|
|
946
811
|
*
|
|
947
|
-
* @
|
|
948
|
-
* @
|
|
949
|
-
* @returns An object type with specified properties required
|
|
812
|
+
* @param value - The value to check
|
|
813
|
+
* @returns True if the value is a plain object, false otherwise
|
|
950
814
|
*
|
|
951
815
|
* @example
|
|
952
|
-
*
|
|
953
|
-
*
|
|
954
|
-
*
|
|
955
|
-
* // { name: string; age?: number; email?: string }
|
|
956
|
-
* ```
|
|
816
|
+
* if (isPlainObject(value)) {
|
|
817
|
+
* console.log('Value is a plain object');
|
|
818
|
+
* }
|
|
957
819
|
*/
|
|
958
|
-
|
|
820
|
+
declare function isPlainObject(value: unknown): value is Record<string, any>;
|
|
821
|
+
//#endregion
|
|
822
|
+
//#region src/functions/object.d.ts
|
|
959
823
|
/**
|
|
960
|
-
*
|
|
961
|
-
*
|
|
962
|
-
* Properties that exist in either T or U but not in both.
|
|
963
|
-
*
|
|
964
|
-
* @template T - First object type
|
|
965
|
-
* @template U - Second object type
|
|
966
|
-
* @returns Properties unique to T or U
|
|
967
|
-
*
|
|
968
|
-
* @example
|
|
969
|
-
* ```ts
|
|
970
|
-
* type A = { x: number; y: string };
|
|
971
|
-
* type B = { y: string; z: boolean };
|
|
972
|
-
* type DiffAB = Diff<A, B>; // { x: number; z: boolean }
|
|
973
|
-
* ```
|
|
824
|
+
* Type representing a path split into segments
|
|
825
|
+
* @template S - The original path string type
|
|
974
826
|
*/
|
|
975
|
-
type
|
|
827
|
+
type SplitPath<S$1 extends string> = S$1 extends `${infer First}.${infer Rest}` ? [First, ...SplitPath<Rest>] : [S$1];
|
|
976
828
|
/**
|
|
977
|
-
*
|
|
978
|
-
*
|
|
979
|
-
* @template
|
|
980
|
-
* @template U - Second object type
|
|
981
|
-
* @returns Properties that exist in both T and U
|
|
982
|
-
*
|
|
983
|
-
* @example
|
|
984
|
-
* ```ts
|
|
985
|
-
* type A = { x: number; y: string };
|
|
986
|
-
* type B = { y: string; z: boolean };
|
|
987
|
-
* type IntersectionAB = Intersection<A, B>; // { y: string }
|
|
988
|
-
* ```
|
|
829
|
+
* Recursive type to resolve nested object types based on path
|
|
830
|
+
* @template T - Current object type
|
|
831
|
+
* @template K - Array of path segments
|
|
989
832
|
*/
|
|
990
|
-
type
|
|
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;
|
|
991
834
|
/**
|
|
992
|
-
*
|
|
993
|
-
*
|
|
994
|
-
* @template
|
|
995
|
-
* @template
|
|
996
|
-
* @
|
|
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
|
|
997
843
|
*
|
|
998
844
|
* @example
|
|
999
|
-
*
|
|
1000
|
-
* type A = { x: number; y: string };
|
|
1001
|
-
* type B = { y: boolean; z: string };
|
|
1002
|
-
* type Merged = Merge<A, B>; // { x: number; y: boolean; z: string }
|
|
1003
|
-
* ```
|
|
845
|
+
* getObjectValue({a: [{b: 1}]}, 'a.0.b', 2) // 1
|
|
1004
846
|
*/
|
|
1005
|
-
|
|
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;
|
|
1006
848
|
/**
|
|
1007
|
-
*
|
|
1008
|
-
*
|
|
1009
|
-
* @template
|
|
1010
|
-
* @
|
|
1011
|
-
* @
|
|
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
|
|
1012
855
|
*
|
|
1013
856
|
* @example
|
|
1014
|
-
*
|
|
1015
|
-
* type A = { x: number; y: string; z: boolean };
|
|
1016
|
-
* type B = { y: string };
|
|
1017
|
-
* type Subtracted = Substract<A, B>; // { x: number; z: boolean }
|
|
1018
|
-
* ```
|
|
857
|
+
* getObjectValue({a: [{b: 1}]}, 'a.0.b') // 1
|
|
1019
858
|
*/
|
|
1020
|
-
|
|
859
|
+
declare function getObjectValue<const T extends object, S$1 extends NestedKeyOf<T>>(obj: T, path: S$1): GetValue<T, SplitPath<S$1>> | undefined;
|
|
1021
860
|
/**
|
|
1022
|
-
*
|
|
861
|
+
* Extend an object or function with additional properties while
|
|
862
|
+
* preserving the original type information.
|
|
1023
863
|
*
|
|
1024
|
-
*
|
|
864
|
+
* Works with both plain objects and callable functions since
|
|
865
|
+
* functions in JavaScript are objects too. Also handles nullable types.
|
|
1025
866
|
*
|
|
1026
|
-
* @template T
|
|
1027
|
-
* @
|
|
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
|
|
1028
874
|
*
|
|
1029
875
|
* @example
|
|
1030
876
|
* ```ts
|
|
1031
|
-
*
|
|
1032
|
-
*
|
|
1033
|
-
* //
|
|
1034
|
-
* ```
|
|
1035
|
-
*/
|
|
1036
|
-
type AllOrNone<T> = T | { [P in keyof T]?: never };
|
|
1037
|
-
/**
|
|
1038
|
-
* Represents exactly one property from an object type being present.
|
|
877
|
+
* // Extend a plain object
|
|
878
|
+
* const config = extendProps({ apiUrl: '/api' }, { timeout: 5000 });
|
|
879
|
+
* // config has both apiUrl and timeout properties
|
|
1039
880
|
*
|
|
1040
|
-
*
|
|
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
|
|
1041
888
|
*
|
|
1042
|
-
*
|
|
1043
|
-
*
|
|
889
|
+
* // Create plugin system
|
|
890
|
+
* const basePlugin = { name: 'base', enabled: true };
|
|
891
|
+
* const authPlugin = extendProps(basePlugin, {
|
|
892
|
+
* authenticate: (token: string) => validateToken(token)
|
|
893
|
+
* });
|
|
1044
894
|
*
|
|
1045
|
-
*
|
|
1046
|
-
*
|
|
1047
|
-
*
|
|
1048
|
-
*
|
|
1049
|
-
*
|
|
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
|
|
1050
906
|
* ```
|
|
1051
907
|
*/
|
|
1052
|
-
|
|
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
|
|
1053
911
|
/**
|
|
1054
|
-
*
|
|
912
|
+
* Repeatedly polls an async `cond` function UNTIL it returns a TRUTHY value,
|
|
913
|
+
* or until the operation times out or is aborted.
|
|
1055
914
|
*
|
|
1056
|
-
*
|
|
1057
|
-
* @returns A union where exactly two properties are present at a time
|
|
915
|
+
* Designed for waiting on async jobs, external state, or delayed availability.
|
|
1058
916
|
*
|
|
1059
|
-
* @
|
|
1060
|
-
* ```ts
|
|
1061
|
-
* type Config = { a: number; b: string; c: boolean };
|
|
1062
|
-
* type TwoConfig = TwoOf<Config>;
|
|
1063
|
-
* // { a: number; b: string } | { a: number; c: boolean } | { b: string; c: boolean }
|
|
1064
|
-
* ```
|
|
1065
|
-
*/
|
|
1066
|
-
type TwoOf<T> = { [K in keyof T]: { [L in Exclude<keyof T, K>]: Pick<T, K | L> }[Exclude<keyof T, K>] }[keyof T];
|
|
1067
|
-
/**
|
|
1068
|
-
* Prettifies a complex type by expanding it for better readability in tooltips.
|
|
917
|
+
* @template T The type of the successful result.
|
|
1069
918
|
*
|
|
1070
|
-
*
|
|
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
|
|
1071
923
|
*
|
|
1072
|
-
* @
|
|
1073
|
-
*
|
|
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
|
|
1074
934
|
*
|
|
1075
935
|
* @example
|
|
1076
936
|
* ```ts
|
|
1077
|
-
*
|
|
1078
|
-
*
|
|
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 });
|
|
1079
942
|
* ```
|
|
1080
|
-
*/
|
|
1081
|
-
type Prettify<T> = T extends infer U ? U extends object ? { [K in keyof U]: U[K] } & {} : U : never;
|
|
1082
|
-
/**
|
|
1083
|
-
* Extracts all nested keys of an object type as dot-separated strings.
|
|
1084
|
-
*
|
|
1085
|
-
* @template ObjectType - The object type to extract nested keys from
|
|
1086
|
-
* @template IgnoreKeys - Keys to ignore during extraction
|
|
1087
|
-
* @returns A union of dot-separated string paths
|
|
1088
943
|
*
|
|
1089
944
|
* @example
|
|
1090
945
|
* ```ts
|
|
1091
|
-
*
|
|
1092
|
-
*
|
|
1093
|
-
*
|
|
1094
|
-
*
|
|
1095
|
-
*
|
|
1096
|
-
*
|
|
1097
|
-
*
|
|
1098
|
-
*
|
|
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 });
|
|
1099
955
|
* ```
|
|
1100
|
-
*/
|
|
1101
|
-
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];
|
|
1102
|
-
/**
|
|
1103
|
-
* Creates a type that excludes properties present in another type.
|
|
1104
956
|
*
|
|
1105
|
-
*
|
|
957
|
+
* @example
|
|
958
|
+
* ```ts
|
|
959
|
+
* // Poll with abort signal for cancellation
|
|
960
|
+
* const controller = new AbortController();
|
|
961
|
+
* setTimeout(() => controller.abort(), 10000); // Cancel after 10s
|
|
1106
962
|
*
|
|
1107
|
-
*
|
|
1108
|
-
*
|
|
1109
|
-
*
|
|
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
|
+
* ```
|
|
1110
974
|
*
|
|
1111
975
|
* @example
|
|
1112
976
|
* ```ts
|
|
1113
|
-
*
|
|
1114
|
-
*
|
|
1115
|
-
*
|
|
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
|
|
1116
982
|
* ```
|
|
1117
983
|
*/
|
|
1118
|
-
|
|
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>;
|
|
1119
995
|
//#endregion
|
|
1120
|
-
//#region src/
|
|
1121
|
-
type
|
|
1122
|
-
type
|
|
1123
|
-
type
|
|
1124
|
-
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
|
|
1125
1020
|
/**
|
|
1126
|
-
*
|
|
1127
|
-
*
|
|
1128
|
-
* Truth table for 3 arguments:
|
|
1129
|
-
*
|
|
1130
|
-
* A B C = AND
|
|
1131
|
-
* 1 1 1 = 1
|
|
1132
|
-
* 1 1 0 = 0
|
|
1133
|
-
* 1 0 1 = 0
|
|
1134
|
-
* 1 0 0 = 0
|
|
1135
|
-
* 0 1 1 = 0
|
|
1136
|
-
* 0 1 0 = 0
|
|
1137
|
-
* 0 0 1 = 0
|
|
1138
|
-
* 0 0 0 = 0
|
|
1139
|
-
*
|
|
1140
|
-
* @template T - Tuple of boolean-like types (1/0)
|
|
1021
|
+
* A task function that can be synchronous or asynchronous.
|
|
1141
1022
|
*/
|
|
1142
|
-
type
|
|
1023
|
+
type Task = () => Promise<void> | void;
|
|
1143
1024
|
/**
|
|
1144
|
-
*
|
|
1145
|
-
*
|
|
1146
|
-
* Truth table for 3 arguments:
|
|
1147
|
-
*
|
|
1148
|
-
* A B C = OR
|
|
1149
|
-
* 1 1 1 = 1
|
|
1150
|
-
* 1 1 0 = 1
|
|
1151
|
-
* 1 0 1 = 1
|
|
1152
|
-
* 1 0 0 = 1
|
|
1153
|
-
* 0 1 1 = 1
|
|
1154
|
-
* 0 1 0 = 1
|
|
1155
|
-
* 0 0 1 = 1
|
|
1156
|
-
* 0 0 0 = 0
|
|
1157
|
-
*
|
|
1158
|
-
* @template T - Tuple of boolean-like types (1/0)
|
|
1025
|
+
* Options for configuring the schedule function.
|
|
1159
1026
|
*/
|
|
1160
|
-
|
|
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
|
+
}
|
|
1161
1035
|
/**
|
|
1162
|
-
*
|
|
1163
|
-
*
|
|
1164
|
-
* Truth table for 3 arguments:
|
|
1165
|
-
*
|
|
1166
|
-
* A B C = XOR
|
|
1167
|
-
* 1 1 1 = 1
|
|
1168
|
-
* 1 1 0 = 0
|
|
1169
|
-
* 1 0 1 = 0
|
|
1170
|
-
* 1 0 0 = 1
|
|
1171
|
-
* 0 1 1 = 0
|
|
1172
|
-
* 0 1 0 = 1
|
|
1173
|
-
* 0 0 1 = 1
|
|
1174
|
-
* 0 0 0 = 0
|
|
1036
|
+
* Runs a function asynchronously in the background without blocking the main thread.
|
|
1175
1037
|
*
|
|
1176
|
-
*
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
/**
|
|
1180
|
-
* 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.
|
|
1181
1041
|
*
|
|
1182
|
-
*
|
|
1042
|
+
* @param task - The function to execute asynchronously
|
|
1043
|
+
* @param options - Configuration options for retries and timing
|
|
1183
1044
|
*
|
|
1184
|
-
*
|
|
1185
|
-
*
|
|
1186
|
-
*
|
|
1187
|
-
*
|
|
1188
|
-
*
|
|
1189
|
-
*
|
|
1190
|
-
* 0 1 0 = 0
|
|
1191
|
-
* 0 0 1 = 0
|
|
1192
|
-
* 0 0 0 = 1
|
|
1045
|
+
* @example
|
|
1046
|
+
* ```ts
|
|
1047
|
+
* // Simple background task
|
|
1048
|
+
* schedule(() => {
|
|
1049
|
+
* console.log('Background work done');
|
|
1050
|
+
* });
|
|
1193
1051
|
*
|
|
1194
|
-
*
|
|
1052
|
+
* // Task with retry on failure
|
|
1053
|
+
* schedule(
|
|
1054
|
+
* () => sendAnalytics(),
|
|
1055
|
+
* { retry: 3, delay: 1000 }
|
|
1056
|
+
* );
|
|
1057
|
+
* ```
|
|
1195
1058
|
*/
|
|
1196
|
-
|
|
1059
|
+
declare function schedule(task: Task, options?: ScheduleOpts): void;
|
|
1060
|
+
//#endregion
|
|
1061
|
+
//#region src/functions/shield.d.ts
|
|
1197
1062
|
/**
|
|
1198
|
-
*
|
|
1199
|
-
*
|
|
1200
|
-
* Truth table for 3 arguments:
|
|
1201
|
-
*
|
|
1202
|
-
* A B C = NOT
|
|
1203
|
-
* 1 1 1 = 0
|
|
1204
|
-
* 1 1 0 = 0
|
|
1205
|
-
* 1 0 1 = 0
|
|
1206
|
-
* 1 0 0 = 0
|
|
1207
|
-
* 0 1 1 = 0
|
|
1208
|
-
* 0 1 0 = 0
|
|
1209
|
-
* 0 0 1 = 0
|
|
1210
|
-
* 0 0 0 = 1
|
|
1063
|
+
* A helper to run sync or async operations safely without try/catch.
|
|
1211
1064
|
*
|
|
1212
|
-
*
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
/**
|
|
1216
|
-
* 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`
|
|
1217
1068
|
*
|
|
1218
|
-
*
|
|
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
|
+
* }
|
|
1219
1081
|
*
|
|
1220
|
-
*
|
|
1221
|
-
*
|
|
1222
|
-
*
|
|
1223
|
-
*
|
|
1224
|
-
*
|
|
1225
|
-
*
|
|
1226
|
-
*
|
|
1227
|
-
*
|
|
1228
|
-
*
|
|
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
|
+
* }
|
|
1229
1093
|
*
|
|
1230
|
-
*
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
/**
|
|
1234
|
-
* 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;
|
|
1235
1097
|
*
|
|
1236
|
-
*
|
|
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
|
+
* ```
|
|
1237
1105
|
*
|
|
1238
|
-
*
|
|
1239
|
-
*
|
|
1240
|
-
*
|
|
1241
|
-
*
|
|
1242
|
-
*
|
|
1243
|
-
*
|
|
1244
|
-
*
|
|
1245
|
-
*
|
|
1246
|
-
*
|
|
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
|
+
* }
|
|
1247
1117
|
*
|
|
1248
|
-
*
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
*
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
* 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
|
+
* ```
|
|
1259
1128
|
*/
|
|
1260
|
-
|
|
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
|
|
1261
1133
|
/**
|
|
1262
|
-
*
|
|
1134
|
+
* Converts various case styles (camelCase, PascalCase, kebab-case, snake_case) into readable normal case.
|
|
1263
1135
|
*
|
|
1264
|
-
*
|
|
1265
|
-
*
|
|
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).
|
|
1266
1143
|
*
|
|
1267
1144
|
* @example
|
|
1268
|
-
*
|
|
1269
|
-
*
|
|
1270
|
-
*
|
|
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
|
+
* ```
|
|
1271
1151
|
*/
|
|
1272
|
-
declare
|
|
1152
|
+
declare function convertToNormalCase(inputString: string): string;
|
|
1273
1153
|
/**
|
|
1274
|
-
*
|
|
1275
|
-
*
|
|
1276
|
-
* @param
|
|
1277
|
-
* @returns
|
|
1278
|
-
*
|
|
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.
|
|
1279
1158
|
* @example
|
|
1280
|
-
*
|
|
1281
|
-
*
|
|
1282
|
-
* }
|
|
1159
|
+
* convertToSlug("Hello World!"); // "hello-world"
|
|
1160
|
+
* convertToSlug("Déjà Vu"); // "deja-vu"
|
|
1283
1161
|
*/
|
|
1284
|
-
declare const
|
|
1162
|
+
declare const convertToSlug: (str: string) => string;
|
|
1285
1163
|
/**
|
|
1286
|
-
*
|
|
1164
|
+
* Pauses execution for the specified time.
|
|
1287
1165
|
*
|
|
1288
|
-
*
|
|
1289
|
-
* @returns True if the value is a boolean, false otherwise
|
|
1166
|
+
* `signal` allows cancelling the sleep via AbortSignal.
|
|
1290
1167
|
*
|
|
1291
|
-
* @
|
|
1292
|
-
*
|
|
1293
|
-
*
|
|
1294
|
-
* }
|
|
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
|
|
1295
1171
|
*/
|
|
1296
|
-
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
|
+
};
|
|
1297
1177
|
/**
|
|
1298
|
-
*
|
|
1178
|
+
* Creates a debounced function that delays invoking the provided function until
|
|
1179
|
+
* after the specified `wait` time has elapsed since the last invocation.
|
|
1299
1180
|
*
|
|
1300
|
-
*
|
|
1301
|
-
*
|
|
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.
|
|
1302
1200
|
*
|
|
1303
1201
|
* @example
|
|
1304
|
-
*
|
|
1305
|
-
*
|
|
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();
|
|
1306
1217
|
* }
|
|
1218
|
+
* ```
|
|
1307
1219
|
*/
|
|
1308
|
-
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
|
+
};
|
|
1309
1227
|
/**
|
|
1310
|
-
*
|
|
1228
|
+
* Creates a throttled function that invokes the provided function at most once
|
|
1229
|
+
* every `wait` milliseconds.
|
|
1311
1230
|
*
|
|
1312
|
-
*
|
|
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.
|
|
1313
1235
|
*
|
|
1314
|
-
*
|
|
1315
|
-
*
|
|
1236
|
+
* The returned function includes the `isPending` property to check if the throttle
|
|
1237
|
+
* timer is currently active.
|
|
1316
1238
|
*
|
|
1317
|
-
* @
|
|
1318
|
-
* isFiniteNumber(42); // true
|
|
1319
|
-
* isFiniteNumber(NaN); // false
|
|
1320
|
-
* isFiniteNumber(Infinity); // false
|
|
1321
|
-
*/
|
|
1322
|
-
declare const isFiniteNumber: (val: unknown) => val is number;
|
|
1323
|
-
/**
|
|
1324
|
-
* Type guard that checks if a value is an array.
|
|
1239
|
+
* @typeParam F - The type of the function to throttle.
|
|
1325
1240
|
*
|
|
1326
|
-
* @param
|
|
1327
|
-
* @
|
|
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.
|
|
1328
1251
|
*
|
|
1329
1252
|
* @example
|
|
1330
|
-
*
|
|
1331
|
-
*
|
|
1332
|
-
*
|
|
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
|
+
* ```
|
|
1333
1276
|
*/
|
|
1334
|
-
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>;
|
|
1335
1281
|
/**
|
|
1336
|
-
*
|
|
1282
|
+
* Formats a string by replacing each '%s' placeholder with the corresponding argument.
|
|
1337
1283
|
*
|
|
1338
|
-
*
|
|
1339
|
-
*
|
|
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.
|
|
1340
1291
|
*
|
|
1341
1292
|
* @example
|
|
1342
|
-
*
|
|
1343
|
-
*
|
|
1344
|
-
*
|
|
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
|
+
* ```
|
|
1345
1309
|
*/
|
|
1346
|
-
declare
|
|
1310
|
+
declare function printf(format: string, ...args: unknown[]): string;
|
|
1347
1311
|
/**
|
|
1348
|
-
*
|
|
1312
|
+
* Escapes a string for use in a regular expression.
|
|
1349
1313
|
*
|
|
1350
|
-
* @param
|
|
1351
|
-
* @returns
|
|
1314
|
+
* @param str - The string to escape
|
|
1315
|
+
* @returns - The escaped string safe for use in RegExp constructor
|
|
1352
1316
|
*
|
|
1353
1317
|
* @example
|
|
1354
|
-
*
|
|
1355
|
-
*
|
|
1356
|
-
*
|
|
1318
|
+
* ```ts
|
|
1319
|
+
* const escapedString = escapeRegExp('Hello, world!');
|
|
1320
|
+
* // escapedString === 'Hello\\, world!'
|
|
1321
|
+
*
|
|
1322
|
+
* const regex = new RegExp(escapeRegExp(userInput));
|
|
1323
|
+
* ```
|
|
1357
1324
|
*/
|
|
1358
|
-
declare
|
|
1325
|
+
declare function escapeRegExp(str: string): string;
|
|
1359
1326
|
/**
|
|
1360
|
-
*
|
|
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
|
|
1361
1332
|
*
|
|
1362
|
-
* @param
|
|
1363
|
-
* @
|
|
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
|
|
1364
1339
|
*
|
|
1365
1340
|
* @example
|
|
1366
|
-
*
|
|
1367
|
-
*
|
|
1368
|
-
*
|
|
1341
|
+
* ```ts
|
|
1342
|
+
* normalizeText('Café') // 'cafe'
|
|
1343
|
+
* normalizeText(' Hello! ') // 'hello'
|
|
1344
|
+
* normalizeText('José', { removeAccents: false }) // 'josé'
|
|
1345
|
+
* ```
|
|
1369
1346
|
*/
|
|
1370
|
-
declare function
|
|
1347
|
+
declare function normalizeText(str?: string | null, options?: {
|
|
1348
|
+
lowercase?: boolean;
|
|
1349
|
+
removeAccents?: boolean;
|
|
1350
|
+
removeNonAlphanumeric?: boolean;
|
|
1351
|
+
}): string;
|
|
1371
1352
|
//#endregion
|
|
1372
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 };
|