@ts-utilities/core 1.1.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -16
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +945 -911
- package/dist/index.d.ts +945 -911
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -142,1232 +142,1266 @@ 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
|
-
*
|
|
770
|
-
*
|
|
771
|
-
* This type traverses through nested objects and arrays, making all properties required.
|
|
772
|
-
* Functions and primitives are left unchanged.
|
|
576
|
+
* Computes a type-level AND (all must true) for a tuple of types.
|
|
773
577
|
*
|
|
774
|
-
*
|
|
775
|
-
* @returns A type with all properties required recursively
|
|
578
|
+
* Truth table for 3 arguments:
|
|
776
579
|
*
|
|
777
|
-
*
|
|
778
|
-
*
|
|
779
|
-
*
|
|
780
|
-
*
|
|
781
|
-
*
|
|
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
|
|
782
589
|
*
|
|
783
|
-
*
|
|
784
|
-
* // {
|
|
785
|
-
* // server: { host: string; port: number };
|
|
786
|
-
* // }
|
|
787
|
-
* ```
|
|
590
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
788
591
|
*/
|
|
789
|
-
type
|
|
592
|
+
type AND<T extends any[]> = T extends [infer F, ...infer R] ? R extends any[] ? F & AND<R> : F : unknown;
|
|
790
593
|
/**
|
|
791
|
-
*
|
|
594
|
+
* Computes a type-level OR (At least one) for a tuple of types.
|
|
792
595
|
*
|
|
793
|
-
*
|
|
794
|
-
* @template K - The keys to make required
|
|
795
|
-
* @returns An object type with specified properties required
|
|
596
|
+
* Truth table for 3 arguments:
|
|
796
597
|
*
|
|
797
|
-
*
|
|
798
|
-
*
|
|
799
|
-
*
|
|
800
|
-
*
|
|
801
|
-
*
|
|
802
|
-
*
|
|
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
|
|
607
|
+
*
|
|
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
|
-
*
|
|
630
|
+
* Computes a type-level XNOR (All or None true) for a tuple of types.
|
|
822
631
|
*
|
|
823
|
-
*
|
|
824
|
-
* @returns A type where all properties can be null
|
|
632
|
+
* Truth table for 3 arguments:
|
|
825
633
|
*
|
|
826
|
-
*
|
|
827
|
-
*
|
|
828
|
-
*
|
|
829
|
-
*
|
|
830
|
-
*
|
|
831
|
-
*
|
|
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
|
|
643
|
+
*
|
|
644
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
832
645
|
*/
|
|
833
|
-
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;
|
|
834
647
|
/**
|
|
835
|
-
*
|
|
648
|
+
* Computes a type-level NOT for a tuple of types.
|
|
836
649
|
*
|
|
837
|
-
*
|
|
838
|
-
* @returns A type where all properties can be undefined
|
|
650
|
+
* Truth table for 3 arguments:
|
|
839
651
|
*
|
|
840
|
-
*
|
|
841
|
-
*
|
|
842
|
-
*
|
|
843
|
-
*
|
|
844
|
-
*
|
|
845
|
-
*
|
|
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)
|
|
846
663
|
*/
|
|
847
|
-
type
|
|
664
|
+
type NOT<T> = { [P in keyof T]?: never };
|
|
848
665
|
/**
|
|
849
|
-
*
|
|
666
|
+
* Computes a type-level NAND for a tuple of types.
|
|
850
667
|
*
|
|
851
|
-
*
|
|
852
|
-
* @returns A type where all properties can be null or undefined
|
|
668
|
+
* Truth table for 3 arguments:
|
|
853
669
|
*
|
|
854
|
-
*
|
|
855
|
-
*
|
|
856
|
-
*
|
|
857
|
-
*
|
|
858
|
-
*
|
|
859
|
-
*
|
|
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)
|
|
860
681
|
*/
|
|
861
|
-
type
|
|
682
|
+
type NAND<T extends any[]> = NOT<AND<T>>;
|
|
862
683
|
/**
|
|
863
|
-
*
|
|
684
|
+
* Computes a type-level NOR for a tuple of types.
|
|
864
685
|
*
|
|
865
|
-
*
|
|
686
|
+
* Truth table for 3 arguments:
|
|
866
687
|
*
|
|
867
|
-
*
|
|
868
|
-
*
|
|
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
|
|
869
697
|
*
|
|
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
|
-
* ```
|
|
698
|
+
* @template T - Tuple of boolean-like types (1/0)
|
|
876
699
|
*/
|
|
877
|
-
type
|
|
700
|
+
type NOR<T extends any[]> = NOT<OR<T>>;
|
|
701
|
+
//#endregion
|
|
702
|
+
//#region src/types/guards.d.ts
|
|
878
703
|
/**
|
|
879
|
-
*
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
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.
|
|
883
713
|
*
|
|
884
|
-
* @
|
|
885
|
-
* @returns
|
|
714
|
+
* @param val - The value to check
|
|
715
|
+
* @returns True if the value is falsy, false otherwise
|
|
886
716
|
*
|
|
887
717
|
* @example
|
|
888
|
-
*
|
|
889
|
-
*
|
|
890
|
-
*
|
|
891
|
-
* features: string[];
|
|
892
|
-
* };
|
|
893
|
-
*
|
|
894
|
-
* type ReadonlyConfig = DeepReadonly<Config>;
|
|
895
|
-
* // {
|
|
896
|
-
* // readonly server: { readonly host: string; readonly port: number };
|
|
897
|
-
* // readonly features: readonly string[];
|
|
898
|
-
* // }
|
|
899
|
-
* ```
|
|
718
|
+
* if (isFalsy(value)) {
|
|
719
|
+
* console.log('Value is falsy');
|
|
720
|
+
* }
|
|
900
721
|
*/
|
|
901
|
-
|
|
722
|
+
declare const isFalsy: (val: unknown) => val is Falsy;
|
|
902
723
|
/**
|
|
903
|
-
*
|
|
724
|
+
* Type guard that checks if a value is null or undefined.
|
|
904
725
|
*
|
|
905
|
-
* @
|
|
906
|
-
* @returns
|
|
726
|
+
* @param val - The value to check
|
|
727
|
+
* @returns True if the value is null or undefined, false otherwise
|
|
907
728
|
*
|
|
908
729
|
* @example
|
|
909
|
-
*
|
|
910
|
-
*
|
|
911
|
-
*
|
|
912
|
-
* // { name: string; profile: { age: number } }
|
|
913
|
-
* ```
|
|
730
|
+
* if (isNullish(value)) {
|
|
731
|
+
* console.log('Value is null or undefined');
|
|
732
|
+
* }
|
|
914
733
|
*/
|
|
915
|
-
|
|
734
|
+
declare const isNullish: (val: unknown) => val is null | undefined;
|
|
916
735
|
/**
|
|
917
|
-
*
|
|
736
|
+
* Type guard that checks if a value is a boolean.
|
|
918
737
|
*
|
|
919
|
-
* @
|
|
920
|
-
* @
|
|
921
|
-
* @returns A union of keys whose values match the specified type
|
|
738
|
+
* @param val - The value to check
|
|
739
|
+
* @returns True if the value is a boolean, false otherwise
|
|
922
740
|
*
|
|
923
741
|
* @example
|
|
924
|
-
*
|
|
925
|
-
*
|
|
926
|
-
*
|
|
927
|
-
* type NumberKeys = KeysOfType<User, number>; // 'age'
|
|
928
|
-
* ```
|
|
742
|
+
* if (isBoolean(value)) {
|
|
743
|
+
* console.log('Value is a boolean');
|
|
744
|
+
* }
|
|
929
745
|
*/
|
|
930
|
-
|
|
746
|
+
declare const isBoolean: (val: unknown) => val is boolean;
|
|
931
747
|
/**
|
|
932
|
-
*
|
|
748
|
+
* Type guard that checks if a value is a string.
|
|
933
749
|
*
|
|
934
|
-
* @
|
|
935
|
-
* @
|
|
936
|
-
* @returns An object type without properties of the specified value type
|
|
750
|
+
* @param val - The value to check
|
|
751
|
+
* @returns True if the value is a string, false otherwise
|
|
937
752
|
*
|
|
938
753
|
* @example
|
|
939
|
-
*
|
|
940
|
-
*
|
|
941
|
-
*
|
|
942
|
-
* ```
|
|
754
|
+
* if (isString(value)) {
|
|
755
|
+
* console.log('Value is a string');
|
|
756
|
+
* }
|
|
943
757
|
*/
|
|
944
|
-
|
|
758
|
+
declare const isString: (val: unknown) => val is string;
|
|
945
759
|
/**
|
|
946
|
-
*
|
|
760
|
+
* Type guard that checks whether a value is a finite number.
|
|
947
761
|
*
|
|
948
|
-
*
|
|
949
|
-
*
|
|
950
|
-
* @
|
|
762
|
+
* This excludes `NaN`, `Infinity`, and `-Infinity`.
|
|
763
|
+
*
|
|
764
|
+
* @param val - The value to check
|
|
765
|
+
* @returns `true` if the value is a finite number
|
|
951
766
|
*
|
|
952
767
|
* @example
|
|
953
|
-
*
|
|
954
|
-
*
|
|
955
|
-
*
|
|
956
|
-
* // { name: string; age?: number; email?: string }
|
|
957
|
-
* ```
|
|
768
|
+
* isFiniteNumber(42); // true
|
|
769
|
+
* isFiniteNumber(NaN); // false
|
|
770
|
+
* isFiniteNumber(Infinity); // false
|
|
958
771
|
*/
|
|
959
|
-
|
|
772
|
+
declare const isFiniteNumber: (val: unknown) => val is number;
|
|
960
773
|
/**
|
|
961
|
-
*
|
|
774
|
+
* Type guard that checks if a value is an array.
|
|
962
775
|
*
|
|
963
|
-
*
|
|
964
|
-
*
|
|
965
|
-
* @template T - First object type
|
|
966
|
-
* @template U - Second object type
|
|
967
|
-
* @returns Properties unique to T or U
|
|
776
|
+
* @param val - The value to check
|
|
777
|
+
* @returns True if the value is an array, false otherwise
|
|
968
778
|
*
|
|
969
779
|
* @example
|
|
970
|
-
*
|
|
971
|
-
*
|
|
972
|
-
*
|
|
973
|
-
* type DiffAB = Diff<A, B>; // { x: number; z: boolean }
|
|
974
|
-
* ```
|
|
780
|
+
* if (isArray(value)) {
|
|
781
|
+
* console.log('Value is an array');
|
|
782
|
+
* }
|
|
975
783
|
*/
|
|
976
|
-
|
|
784
|
+
declare const isArray: (val: unknown) => val is unknown[];
|
|
977
785
|
/**
|
|
978
|
-
*
|
|
786
|
+
* Type guard that checks if a value is a function.
|
|
979
787
|
*
|
|
980
|
-
* @
|
|
981
|
-
* @
|
|
982
|
-
* @returns Properties that exist in both T and U
|
|
788
|
+
* @param val - The value to check
|
|
789
|
+
* @returns True if the value is a function, false otherwise
|
|
983
790
|
*
|
|
984
791
|
* @example
|
|
985
|
-
*
|
|
986
|
-
*
|
|
987
|
-
*
|
|
988
|
-
* type IntersectionAB = Intersection<A, B>; // { y: string }
|
|
989
|
-
* ```
|
|
792
|
+
* if (isFunction(value)) {
|
|
793
|
+
* console.log('Value is a function');
|
|
794
|
+
* }
|
|
990
795
|
*/
|
|
991
|
-
|
|
796
|
+
declare const isFunction: (val: unknown) => val is Function;
|
|
992
797
|
/**
|
|
993
|
-
*
|
|
798
|
+
* Type guard that checks if a value is a primitive type.
|
|
994
799
|
*
|
|
995
|
-
* @
|
|
996
|
-
* @
|
|
997
|
-
* @returns A merged object type with properties from both
|
|
800
|
+
* @param val - The value to check
|
|
801
|
+
* @returns True if the value is a primitive, false otherwise
|
|
998
802
|
*
|
|
999
803
|
* @example
|
|
1000
|
-
*
|
|
1001
|
-
*
|
|
1002
|
-
*
|
|
1003
|
-
* type Merged = Merge<A, B>; // { x: number; y: boolean; z: string }
|
|
1004
|
-
* ```
|
|
804
|
+
* if (isPrimitive(value)) {
|
|
805
|
+
* console.log('Value is a primitive type');
|
|
806
|
+
* }
|
|
1005
807
|
*/
|
|
1006
|
-
|
|
808
|
+
declare const isPrimitive: (val: unknown) => val is Primitive;
|
|
1007
809
|
/**
|
|
1008
|
-
*
|
|
810
|
+
* Type guard that checks if a value is a plain object (not an array, function, etc.).
|
|
1009
811
|
*
|
|
1010
|
-
* @
|
|
1011
|
-
* @
|
|
1012
|
-
* @returns T without properties that exist in U
|
|
812
|
+
* @param value - The value to check
|
|
813
|
+
* @returns True if the value is a plain object, false otherwise
|
|
1013
814
|
*
|
|
1014
815
|
* @example
|
|
1015
|
-
*
|
|
1016
|
-
*
|
|
1017
|
-
*
|
|
1018
|
-
* type Subtracted = Substract<A, B>; // { x: number; z: boolean }
|
|
1019
|
-
* ```
|
|
816
|
+
* if (isPlainObject(value)) {
|
|
817
|
+
* console.log('Value is a plain object');
|
|
818
|
+
* }
|
|
1020
819
|
*/
|
|
1021
|
-
|
|
820
|
+
declare function isPlainObject(value: unknown): value is Record<string, any>;
|
|
821
|
+
//#endregion
|
|
822
|
+
//#region src/functions/object.d.ts
|
|
1022
823
|
/**
|
|
1023
|
-
*
|
|
1024
|
-
*
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
*
|
|
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
|
|
1029
843
|
*
|
|
1030
844
|
* @example
|
|
1031
|
-
*
|
|
1032
|
-
*
|
|
1033
|
-
*
|
|
1034
|
-
* // { host: string; port: number } | {}
|
|
1035
|
-
* ```
|
|
845
|
+
* // use as const for better type safety for arrays
|
|
846
|
+
* extract({a: [{b: 1}]} as const, 'a.0.b', 2) // 1
|
|
847
|
+
* extract({a: {b: 1}}, 'a.b', 2) // 1
|
|
1036
848
|
*/
|
|
1037
|
-
|
|
849
|
+
declare function extract<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;
|
|
1038
850
|
/**
|
|
1039
|
-
*
|
|
851
|
+
* Get a nested value from an object using dot notation path
|
|
852
|
+
* @template T - Object type
|
|
853
|
+
* @template S - Valid path string type constrained by object structure
|
|
854
|
+
* @param obj - Source object
|
|
855
|
+
* @param path - Dot-separated path string (constrained to valid paths)
|
|
856
|
+
* @returns Value at path or undefined
|
|
1040
857
|
*
|
|
1041
|
-
*
|
|
858
|
+
* @example
|
|
859
|
+
* extract({a: [{b: 1}]}, 'a.0.b') // 1
|
|
860
|
+
*/
|
|
861
|
+
declare function extract<const T extends object, S$1 extends NestedKeyOf<T>>(obj: T, path: S$1): GetValue<T, SplitPath<S$1>> | undefined;
|
|
862
|
+
/**
|
|
863
|
+
* Get multiple nested values from an object using dot notation paths
|
|
864
|
+
* @template T - Object type
|
|
865
|
+
* @template S - Array of path strings
|
|
866
|
+
* @template D - Default value type
|
|
867
|
+
* @param obj - Source object
|
|
868
|
+
* @param paths - Array of dot-separated path strings
|
|
869
|
+
* @param defaultValue - Fallback value if any path not found
|
|
870
|
+
* @returns Array of values at paths or default values
|
|
1042
871
|
*
|
|
1043
|
-
* @
|
|
1044
|
-
*
|
|
872
|
+
* @example
|
|
873
|
+
* extract({a: [{b: 1}, {b: 2}]}, ['a.0.b', 'a.1.b'], 0) // [1, 2]
|
|
874
|
+
*/
|
|
875
|
+
declare function extract<const T extends object, const S$1 extends readonly string[], D>(obj: T, paths: S$1, defaultValue: D): { readonly [K in keyof S$1]: GetValue<T, SplitPath<S$1[K] & string>> | D };
|
|
876
|
+
/**
|
|
877
|
+
* Get multiple nested values from an object using dot notation paths
|
|
878
|
+
* @template T - Object type
|
|
879
|
+
* @template S - Array of path strings
|
|
880
|
+
* @param obj - Source object
|
|
881
|
+
* @param paths - Array of dot-separated path strings
|
|
882
|
+
* @returns Array of values at paths or undefined
|
|
1045
883
|
*
|
|
1046
884
|
* @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
|
-
* ```
|
|
885
|
+
* extract({a: [{b: 1}, {b: 2}]}, ['a.0.b', 'a.1.b']) // [1, 2]
|
|
1052
886
|
*/
|
|
1053
|
-
|
|
887
|
+
declare function extract<const T extends object, const S$1 extends readonly string[]>(obj: T, paths: S$1): { readonly [K in keyof S$1]: GetValue<T, SplitPath<S$1[K] & string>> | undefined };
|
|
1054
888
|
/**
|
|
1055
|
-
*
|
|
889
|
+
* Get multiple nested values from an object using dot notation paths mapped to keys
|
|
890
|
+
* @template T - Object type
|
|
891
|
+
* @template S - Record mapping keys to path strings
|
|
892
|
+
* @template D - Default value type
|
|
893
|
+
* @param obj - Source object
|
|
894
|
+
* @param paths - Record with keys as output keys and values as dot-separated path strings
|
|
895
|
+
* @param defaultValue - Fallback value if any path not found
|
|
896
|
+
* @returns Object with the same keys as paths, values at paths or default values
|
|
1056
897
|
*
|
|
1057
|
-
* @
|
|
1058
|
-
*
|
|
898
|
+
* @example
|
|
899
|
+
* extract({a: [{b: 1}, {b: 2}]}, {first: 'a.0.b', second: 'a.1.b'}, 0) // {first: 1, second: 2}
|
|
900
|
+
*/
|
|
901
|
+
declare function extract<const T extends object, const S$1 extends Record<string, string>, D>(obj: T, paths: S$1, defaultValue: D): { readonly [K in keyof S$1]: GetValue<T, SplitPath<S$1[K]>> | D };
|
|
902
|
+
/**
|
|
903
|
+
* Get multiple nested values from an object using dot notation paths mapped to keys
|
|
904
|
+
* @template T - Object type
|
|
905
|
+
* @template S - Record mapping keys to path strings
|
|
906
|
+
* @param obj - Source object
|
|
907
|
+
* @param paths - Record with keys as output keys and values as dot-separated path strings
|
|
908
|
+
* @returns Object with the same keys as paths, values at paths or undefined
|
|
1059
909
|
*
|
|
1060
910
|
* @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
|
-
* ```
|
|
911
|
+
* extract({a: [{b: 1}, {b: 2}]}, {first: 'a.0.b', second: 'a.1.b'}) // {first: 1, second: 2}
|
|
1066
912
|
*/
|
|
1067
|
-
|
|
913
|
+
declare function extract<const T extends object, const S$1 extends Record<string, string>>(obj: T, paths: S$1): { readonly [K in keyof S$1]: GetValue<T, SplitPath<S$1[K]>> | undefined };
|
|
1068
914
|
/**
|
|
1069
|
-
*
|
|
915
|
+
* Extend an object or function with additional properties while
|
|
916
|
+
* preserving the original type information.
|
|
1070
917
|
*
|
|
1071
|
-
*
|
|
918
|
+
* Works with both plain objects and callable functions since
|
|
919
|
+
* functions in JavaScript are objects too. Also handles nullable types.
|
|
1072
920
|
*
|
|
1073
|
-
* @template T
|
|
1074
|
-
* @
|
|
921
|
+
* @template T The base object or function type (can be null/undefined)
|
|
922
|
+
* @template P The additional properties type
|
|
923
|
+
*
|
|
924
|
+
* @param base - The object or function to extend (can be null/undefined)
|
|
925
|
+
* @param props - An object containing properties to attach
|
|
926
|
+
*
|
|
927
|
+
* @returns The same object/function, augmented with the given properties, or the original value if null/undefined
|
|
1075
928
|
*
|
|
1076
929
|
* @example
|
|
1077
930
|
* ```ts
|
|
1078
|
-
*
|
|
1079
|
-
*
|
|
931
|
+
* // Extend a plain object
|
|
932
|
+
* const config = extendProps({ apiUrl: '/api' }, { timeout: 5000 });
|
|
933
|
+
* // config has both apiUrl and timeout properties
|
|
934
|
+
*
|
|
935
|
+
* // Extend a function with metadata
|
|
936
|
+
* const fetchData = (url: string) => fetch(url).then(r => r.json());
|
|
937
|
+
* const enhancedFetch = extendProps(fetchData, {
|
|
938
|
+
* description: 'Data fetching utility',
|
|
939
|
+
* version: '1.0'
|
|
940
|
+
* });
|
|
941
|
+
* // enhancedFetch is callable and has description/version properties
|
|
942
|
+
*
|
|
943
|
+
* // Create plugin system
|
|
944
|
+
* const basePlugin = { name: 'base', enabled: true };
|
|
945
|
+
* const authPlugin = extendProps(basePlugin, {
|
|
946
|
+
* authenticate: (token: string) => validateToken(token)
|
|
947
|
+
* });
|
|
948
|
+
*
|
|
949
|
+
* // Build configuration objects
|
|
950
|
+
* const defaultSettings = { theme: 'light', lang: 'en' };
|
|
951
|
+
* const userSettings = extendProps(defaultSettings, {
|
|
952
|
+
* theme: 'dark',
|
|
953
|
+
* notifications: true
|
|
954
|
+
* });
|
|
955
|
+
*
|
|
956
|
+
* // Handle nullable types (e.g., Supabase Session | null)
|
|
957
|
+
* const session: Session | null = getSession();
|
|
958
|
+
* const extendedSession = extendProps(session, { customProp: 'value' });
|
|
959
|
+
* // extendedSession is (Session & { customProp: string }) | null
|
|
1080
960
|
* ```
|
|
1081
961
|
*/
|
|
1082
|
-
|
|
962
|
+
declare function extendProps<T, P$1 extends object>(base: T, props: P$1): T extends null | undefined ? T : T & P$1;
|
|
963
|
+
//#endregion
|
|
964
|
+
//#region src/functions/poll.d.ts
|
|
1083
965
|
/**
|
|
1084
|
-
*
|
|
966
|
+
* Repeatedly polls an async `cond` function UNTIL it returns a TRUTHY value,
|
|
967
|
+
* or until the operation times out or is aborted.
|
|
1085
968
|
*
|
|
1086
|
-
*
|
|
1087
|
-
*
|
|
1088
|
-
* @
|
|
969
|
+
* Designed for waiting on async jobs, external state, or delayed availability.
|
|
970
|
+
*
|
|
971
|
+
* @template T The type of the successful result.
|
|
972
|
+
*
|
|
973
|
+
* @param cond
|
|
974
|
+
* A function returning a Promise that resolves to:
|
|
975
|
+
* - a truthy value `T` → stop polling and return it
|
|
976
|
+
* - falsy/null/undefined → continue polling
|
|
977
|
+
*
|
|
978
|
+
* @param options
|
|
979
|
+
* Configuration options:
|
|
980
|
+
* - `interval` (number) — Time between polls in ms (default: 5000 ms)
|
|
981
|
+
* - `timeout` (number) — Max total duration before failing (default: 5 min)
|
|
982
|
+
* - `jitter` (boolean) — Add small random offset (±10%) to intervals to avoid sync bursts (default: true)
|
|
983
|
+
* - `signal` (AbortSignal) — Optional abort signal to cancel polling
|
|
984
|
+
*
|
|
985
|
+
* @returns
|
|
986
|
+
* Resolves with the truthy value `T` when successful.
|
|
987
|
+
* Throws `AbortError` if aborted
|
|
1089
988
|
*
|
|
1090
989
|
* @example
|
|
1091
990
|
* ```ts
|
|
1092
|
-
*
|
|
1093
|
-
*
|
|
1094
|
-
*
|
|
1095
|
-
*
|
|
1096
|
-
* };
|
|
991
|
+
* // Poll for job completion
|
|
992
|
+
* const job = await poll(async () => {
|
|
993
|
+
* const status = await getJobStatus();
|
|
994
|
+
* return status === 'done' ? status : null;
|
|
995
|
+
* }, { interval: 3000, timeout: 60000 });
|
|
996
|
+
* ```
|
|
1097
997
|
*
|
|
1098
|
-
*
|
|
1099
|
-
*
|
|
998
|
+
* @example
|
|
999
|
+
* ```ts
|
|
1000
|
+
* // Wait for API endpoint to be ready
|
|
1001
|
+
* const apiReady = await poll(async () => {
|
|
1002
|
+
* try {
|
|
1003
|
+
* await fetch('/api/health');
|
|
1004
|
+
* return true;
|
|
1005
|
+
* } catch {
|
|
1006
|
+
* return null;
|
|
1007
|
+
* }
|
|
1008
|
+
* }, { interval: 1000, timeout: 30000 });
|
|
1100
1009
|
* ```
|
|
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
1010
|
*
|
|
1106
|
-
*
|
|
1011
|
+
* @example
|
|
1012
|
+
* ```ts
|
|
1013
|
+
* // Poll with abort signal for cancellation
|
|
1014
|
+
* const controller = new AbortController();
|
|
1015
|
+
* setTimeout(() => controller.abort(), 10000); // Cancel after 10s
|
|
1107
1016
|
*
|
|
1108
|
-
*
|
|
1109
|
-
*
|
|
1110
|
-
*
|
|
1017
|
+
* try {
|
|
1018
|
+
* const result = await poll(
|
|
1019
|
+
* () => checkExternalService(),
|
|
1020
|
+
* { interval: 2000, signal: controller.signal }
|
|
1021
|
+
* );
|
|
1022
|
+
* } catch (err) {
|
|
1023
|
+
* if (err.name === 'AbortError') {
|
|
1024
|
+
* console.log('Polling was cancelled');
|
|
1025
|
+
* }
|
|
1026
|
+
* }
|
|
1027
|
+
* ```
|
|
1111
1028
|
*
|
|
1112
1029
|
* @example
|
|
1113
1030
|
* ```ts
|
|
1114
|
-
*
|
|
1115
|
-
*
|
|
1116
|
-
*
|
|
1031
|
+
* // Poll for user action completion
|
|
1032
|
+
* const userConfirmed = await poll(async () => {
|
|
1033
|
+
* const confirmations = await getPendingConfirmations();
|
|
1034
|
+
* return confirmations.length > 0 ? confirmations[0] : null;
|
|
1035
|
+
* }, { interval: 5000, timeout: 300000 }); // 5 min timeout
|
|
1117
1036
|
* ```
|
|
1118
1037
|
*/
|
|
1119
|
-
|
|
1038
|
+
declare function poll<T>(cond: () => Promise<T | null | false | undefined>, {
|
|
1039
|
+
interval,
|
|
1040
|
+
timeout,
|
|
1041
|
+
jitter,
|
|
1042
|
+
signal
|
|
1043
|
+
}?: Partial<{
|
|
1044
|
+
interval: number;
|
|
1045
|
+
timeout: number;
|
|
1046
|
+
signal: AbortSignal;
|
|
1047
|
+
jitter: boolean;
|
|
1048
|
+
}>): Promise<T>;
|
|
1120
1049
|
//#endregion
|
|
1121
|
-
//#region src/
|
|
1122
|
-
type
|
|
1123
|
-
type
|
|
1124
|
-
type
|
|
1125
|
-
type
|
|
1050
|
+
//#region src/functions/promise.d.ts
|
|
1051
|
+
type TaskType<T> = Promise<T> | (() => Promise<T>);
|
|
1052
|
+
type AwaitedTuple<T extends readonly TaskType<any>[] | []> = { -readonly [P in keyof T]: Awaited<T[P] extends (() => Promise<infer R>) ? R : T[P]> };
|
|
1053
|
+
type ObjectValues<T extends Record<string, TaskType<any>>> = { [K in keyof T]: Awaited<T[K] extends (() => Promise<infer R>) ? R : T[K]> };
|
|
1054
|
+
type ConcurrenceResult<T> = {
|
|
1055
|
+
results: T;
|
|
1056
|
+
errors: Error[];
|
|
1057
|
+
succeeded: number;
|
|
1058
|
+
failed: number;
|
|
1059
|
+
duration: number;
|
|
1060
|
+
};
|
|
1061
|
+
type ConcurrenceOptions = {
|
|
1062
|
+
concurrency?: number;
|
|
1063
|
+
timeout?: number;
|
|
1064
|
+
signal?: AbortSignal;
|
|
1065
|
+
retry?: number;
|
|
1066
|
+
retryDelay?: number;
|
|
1067
|
+
throwOnFirstError?: boolean;
|
|
1068
|
+
ignoreErrors?: boolean;
|
|
1069
|
+
};
|
|
1070
|
+
declare function withConcurrency<T extends readonly TaskType<any>[] | []>(tasks: T, options?: ConcurrenceOptions): Promise<ConcurrenceResult<AwaitedTuple<T>>>;
|
|
1071
|
+
declare function withConcurrency<T extends Record<string, TaskType<any>>>(tasks: T, options?: ConcurrenceOptions): Promise<ConcurrenceResult<ObjectValues<T>>>;
|
|
1072
|
+
//#endregion
|
|
1073
|
+
//#region src/functions/schedule.d.ts
|
|
1126
1074
|
/**
|
|
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)
|
|
1075
|
+
* A task function that can be synchronous or asynchronous.
|
|
1142
1076
|
*/
|
|
1143
|
-
type
|
|
1077
|
+
type Task = () => Promise<void> | void;
|
|
1144
1078
|
/**
|
|
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)
|
|
1079
|
+
* Options for configuring the schedule function.
|
|
1160
1080
|
*/
|
|
1161
|
-
|
|
1081
|
+
interface ScheduleOpts {
|
|
1082
|
+
/** Number of retry attempts on failure. Defaults to 0. */
|
|
1083
|
+
retry?: number;
|
|
1084
|
+
/** Delay in milliseconds between retries. Defaults to 0. */
|
|
1085
|
+
delay?: number;
|
|
1086
|
+
/** Maximum time in milliseconds to wait for the task to complete. */
|
|
1087
|
+
timeout?: number;
|
|
1088
|
+
}
|
|
1162
1089
|
/**
|
|
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
|
|
1090
|
+
* Runs a function asynchronously in the background without blocking the main thread.
|
|
1176
1091
|
*
|
|
1177
|
-
*
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
/**
|
|
1181
|
-
* Computes a type-level XNOR (All or None true) for a tuple of types.
|
|
1092
|
+
* Executes the task immediately using setTimeout, with optional retry logic on failure.
|
|
1093
|
+
* Useful for non-critical operations like analytics, logging, or background processing.
|
|
1094
|
+
* Logs execution time and retry attempts to the console.
|
|
1182
1095
|
*
|
|
1183
|
-
*
|
|
1096
|
+
* @param task - The function to execute asynchronously
|
|
1097
|
+
* @param options - Configuration options for retries and timing
|
|
1184
1098
|
*
|
|
1185
|
-
*
|
|
1186
|
-
*
|
|
1187
|
-
*
|
|
1188
|
-
*
|
|
1189
|
-
*
|
|
1190
|
-
*
|
|
1191
|
-
* 0 1 0 = 0
|
|
1192
|
-
* 0 0 1 = 0
|
|
1193
|
-
* 0 0 0 = 1
|
|
1099
|
+
* @example
|
|
1100
|
+
* ```ts
|
|
1101
|
+
* // Simple background task
|
|
1102
|
+
* schedule(() => {
|
|
1103
|
+
* console.log('Background work done');
|
|
1104
|
+
* });
|
|
1194
1105
|
*
|
|
1195
|
-
*
|
|
1106
|
+
* // Task with retry on failure
|
|
1107
|
+
* schedule(
|
|
1108
|
+
* () => sendAnalytics(),
|
|
1109
|
+
* { retry: 3, delay: 1000 }
|
|
1110
|
+
* );
|
|
1111
|
+
* ```
|
|
1196
1112
|
*/
|
|
1197
|
-
|
|
1113
|
+
declare function schedule(task: Task, options?: ScheduleOpts): void;
|
|
1114
|
+
//#endregion
|
|
1115
|
+
//#region src/functions/shield.d.ts
|
|
1198
1116
|
/**
|
|
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
|
|
1117
|
+
* A helper to run sync or async operations safely without try/catch.
|
|
1212
1118
|
*
|
|
1213
|
-
*
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
/**
|
|
1217
|
-
* Computes a type-level NAND for a tuple of types.
|
|
1119
|
+
* Returns a tuple `[error, data]`:
|
|
1120
|
+
* - `error`: the thrown error (if any), otherwise `null`
|
|
1121
|
+
* - `data`: the resolved value (if successful), otherwise `null`
|
|
1218
1122
|
*
|
|
1219
|
-
*
|
|
1123
|
+
* @example
|
|
1124
|
+
* ```ts
|
|
1125
|
+
* // Synchronous error handling
|
|
1126
|
+
* const [err, value] = shield(() => {
|
|
1127
|
+
* if (Math.random() > 0.5) throw new Error('Random failure');
|
|
1128
|
+
* return 'success';
|
|
1129
|
+
* });
|
|
1130
|
+
* if (err) {
|
|
1131
|
+
* console.error('Operation failed:', err);
|
|
1132
|
+
* } else {
|
|
1133
|
+
* console.log('Result:', value);
|
|
1134
|
+
* }
|
|
1220
1135
|
*
|
|
1221
|
-
*
|
|
1222
|
-
*
|
|
1223
|
-
*
|
|
1224
|
-
*
|
|
1225
|
-
*
|
|
1226
|
-
*
|
|
1227
|
-
*
|
|
1228
|
-
*
|
|
1229
|
-
*
|
|
1136
|
+
* // Asynchronous error handling
|
|
1137
|
+
* const [asyncErr, result] = await shield(async () => {
|
|
1138
|
+
* const response = await fetch('/api/data');
|
|
1139
|
+
* if (!response.ok) throw new Error('API error');
|
|
1140
|
+
* return response.json();
|
|
1141
|
+
* });
|
|
1142
|
+
* if (asyncErr) {
|
|
1143
|
+
* console.error('API call failed:', asyncErr);
|
|
1144
|
+
* } else {
|
|
1145
|
+
* processData(result);
|
|
1146
|
+
* }
|
|
1230
1147
|
*
|
|
1231
|
-
*
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
/**
|
|
1235
|
-
* Computes a type-level NOR for a tuple of types.
|
|
1148
|
+
* // API calls with fallbacks
|
|
1149
|
+
* const [fetchErr, data] = await shield(fetchUserData(userId));
|
|
1150
|
+
* const userData = fetchErr ? getCachedUserData(userId) : data;
|
|
1236
1151
|
*
|
|
1237
|
-
*
|
|
1152
|
+
* // File operations
|
|
1153
|
+
* const [fileErr, content] = shield(() => readFileSync('config.json'));
|
|
1154
|
+
* if (fileErr) {
|
|
1155
|
+
* console.warn('Could not read config, using defaults');
|
|
1156
|
+
* return defaultConfig;
|
|
1157
|
+
* }
|
|
1158
|
+
* ```
|
|
1238
1159
|
*
|
|
1239
|
-
*
|
|
1240
|
-
*
|
|
1241
|
-
*
|
|
1242
|
-
*
|
|
1243
|
-
*
|
|
1244
|
-
*
|
|
1245
|
-
*
|
|
1246
|
-
*
|
|
1247
|
-
*
|
|
1160
|
+
* @example
|
|
1161
|
+
* ```ts
|
|
1162
|
+
* // In async functions
|
|
1163
|
+
* async function safeApiCall() {
|
|
1164
|
+
* const [err, result] = await shield(callExternalAPI());
|
|
1165
|
+
* if (err) {
|
|
1166
|
+
* await logError(err);
|
|
1167
|
+
* return null;
|
|
1168
|
+
* }
|
|
1169
|
+
* return result;
|
|
1170
|
+
* }
|
|
1248
1171
|
*
|
|
1249
|
-
*
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
*
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
* Represents all falsy values in JavaScript.
|
|
1172
|
+
* // In event handlers
|
|
1173
|
+
* function handleSubmit(formData) {
|
|
1174
|
+
* const [validationErr, validatedData] = shield(() => validateForm(formData));
|
|
1175
|
+
* if (validationErr) {
|
|
1176
|
+
* showValidationError(validationErr);
|
|
1177
|
+
* return;
|
|
1178
|
+
* }
|
|
1179
|
+
* submitData(validatedData);
|
|
1180
|
+
* }
|
|
1181
|
+
* ```
|
|
1260
1182
|
*/
|
|
1261
|
-
|
|
1183
|
+
declare function shield<T, E = Error>(operation: Promise<T>): Promise<[E | null, T | null]>;
|
|
1184
|
+
declare function shield<T, E = Error>(operation: () => T): [E | null, T | null];
|
|
1185
|
+
//#endregion
|
|
1186
|
+
//#region src/functions/utils-core.d.ts
|
|
1262
1187
|
/**
|
|
1263
|
-
*
|
|
1188
|
+
* Converts various case styles (camelCase, PascalCase, kebab-case, snake_case) into readable normal case.
|
|
1264
1189
|
*
|
|
1265
|
-
*
|
|
1266
|
-
*
|
|
1190
|
+
* Transforms technical naming conventions into human-readable titles by:
|
|
1191
|
+
* - Adding spaces between words
|
|
1192
|
+
* - Capitalizing the first letter of each word
|
|
1193
|
+
* - Handling common separators (-, _, camelCase boundaries)
|
|
1194
|
+
*
|
|
1195
|
+
* @param inputString - The string to convert (supports camelCase, PascalCase, kebab-case, snake_case).
|
|
1196
|
+
* @returns The converted string in normal case (title case).
|
|
1267
1197
|
*
|
|
1268
1198
|
* @example
|
|
1269
|
-
*
|
|
1270
|
-
*
|
|
1271
|
-
*
|
|
1199
|
+
* ```ts
|
|
1200
|
+
* convertToNormalCase('camelCase') // 'Camel Case'
|
|
1201
|
+
* convertToNormalCase('kebab-case') // 'Kebab Case'
|
|
1202
|
+
* convertToNormalCase('snake_case') // 'Snake Case'
|
|
1203
|
+
* convertToNormalCase('PascalCase') // 'Pascal Case'
|
|
1204
|
+
* ```
|
|
1272
1205
|
*/
|
|
1273
|
-
declare
|
|
1206
|
+
declare function convertToNormalCase(inputString: string): string;
|
|
1274
1207
|
/**
|
|
1275
|
-
*
|
|
1276
|
-
*
|
|
1277
|
-
* @param
|
|
1278
|
-
* @returns
|
|
1279
|
-
*
|
|
1208
|
+
* Converts a string to a URL-friendly slug by trimming, converting to lowercase,
|
|
1209
|
+
* replacing diacritics, removing invalid characters, and replacing spaces with hyphens.
|
|
1210
|
+
* @param {string} [str] - The input string to convert.
|
|
1211
|
+
* @returns {string} The generated slug.
|
|
1280
1212
|
* @example
|
|
1281
|
-
*
|
|
1282
|
-
*
|
|
1283
|
-
* }
|
|
1213
|
+
* convertToSlug("Hello World!"); // "hello-world"
|
|
1214
|
+
* convertToSlug("Déjà Vu"); // "deja-vu"
|
|
1284
1215
|
*/
|
|
1285
|
-
declare const
|
|
1216
|
+
declare const convertToSlug: (str: string) => string;
|
|
1286
1217
|
/**
|
|
1287
|
-
*
|
|
1218
|
+
* Pauses execution for the specified time.
|
|
1288
1219
|
*
|
|
1289
|
-
*
|
|
1290
|
-
* @returns True if the value is a boolean, false otherwise
|
|
1220
|
+
* `signal` allows cancelling the sleep via AbortSignal.
|
|
1291
1221
|
*
|
|
1292
|
-
* @
|
|
1293
|
-
*
|
|
1294
|
-
*
|
|
1295
|
-
* }
|
|
1222
|
+
* @param time - Time in milliseconds to sleep (default is 1000ms)
|
|
1223
|
+
* @param signal - Optional AbortSignal to cancel the sleep early
|
|
1224
|
+
* @returns - A Promise that resolves after the specified time or when aborted
|
|
1296
1225
|
*/
|
|
1297
|
-
declare const
|
|
1226
|
+
declare const sleep: (time?: number, signal?: AbortSignal) => Promise<void>;
|
|
1227
|
+
type DebouncedFunction<F$1 extends (...args: any[]) => any> = {
|
|
1228
|
+
(...args: Parameters<F$1>): ReturnType<F$1> | undefined;
|
|
1229
|
+
readonly isPending: boolean;
|
|
1230
|
+
};
|
|
1298
1231
|
/**
|
|
1299
|
-
*
|
|
1232
|
+
* Creates a debounced function that delays invoking the provided function until
|
|
1233
|
+
* after the specified `wait` time has elapsed since the last invocation.
|
|
1300
1234
|
*
|
|
1301
|
-
*
|
|
1302
|
-
*
|
|
1235
|
+
* If the `immediate` option is set to `true`, the function will be invoked immediately
|
|
1236
|
+
* on the leading edge of the wait interval. Subsequent calls during the wait interval
|
|
1237
|
+
* will reset the timer but not invoke the function until the interval elapses again.
|
|
1238
|
+
*
|
|
1239
|
+
* The returned function includes the `isPending` property to check if the debounce
|
|
1240
|
+
* timer is currently active.
|
|
1241
|
+
*
|
|
1242
|
+
* @typeParam F - The type of the function to debounce.
|
|
1243
|
+
*
|
|
1244
|
+
* @param function_ - The function to debounce.
|
|
1245
|
+
* @param wait - The number of milliseconds to delay (default is 100ms).
|
|
1246
|
+
* @param options - An optional object with the following properties:
|
|
1247
|
+
* - `immediate` (boolean): If `true`, invokes the function on the leading edge
|
|
1248
|
+
* of the wait interval instead of the trailing edge.
|
|
1249
|
+
*
|
|
1250
|
+
* @returns A debounced version of the provided function, enhanced with the `isPending` property.
|
|
1251
|
+
*
|
|
1252
|
+
* @throws {TypeError} If the first parameter is not a function.
|
|
1253
|
+
* @throws {RangeError} If the `wait` parameter is negative.
|
|
1303
1254
|
*
|
|
1304
1255
|
* @example
|
|
1305
|
-
*
|
|
1306
|
-
*
|
|
1256
|
+
* ```ts
|
|
1257
|
+
* // Basic debouncing
|
|
1258
|
+
* const log = debounce((message: string) => console.log(message), 200);
|
|
1259
|
+
* log('Hello'); // Logs "Hello" after 200ms if no other call is made.
|
|
1260
|
+
* console.log(log.isPending); // true if the timer is active.
|
|
1261
|
+
*
|
|
1262
|
+
* // Immediate execution
|
|
1263
|
+
* const save = debounce(() => saveToServer(), 500, { immediate: true });
|
|
1264
|
+
* save(); // Executes immediately, then waits 500ms for subsequent calls
|
|
1265
|
+
*
|
|
1266
|
+
* // Check pending state
|
|
1267
|
+
* const debouncedSearch = debounce(searchAPI, 300);
|
|
1268
|
+
* debouncedSearch('query');
|
|
1269
|
+
* if (debouncedSearch.isPending) {
|
|
1270
|
+
* showLoadingIndicator();
|
|
1307
1271
|
* }
|
|
1272
|
+
* ```
|
|
1308
1273
|
*/
|
|
1309
|
-
declare
|
|
1274
|
+
declare function debounce<F$1 extends (...args: any[]) => any>(function_: F$1, wait?: number, options?: {
|
|
1275
|
+
immediate: boolean;
|
|
1276
|
+
}): DebouncedFunction<F$1>;
|
|
1277
|
+
type ThrottledFunction<F$1 extends (...args: any[]) => any> = {
|
|
1278
|
+
(...args: Parameters<F$1>): ReturnType<F$1> | undefined;
|
|
1279
|
+
readonly isPending: boolean;
|
|
1280
|
+
};
|
|
1310
1281
|
/**
|
|
1311
|
-
*
|
|
1282
|
+
* Creates a throttled function that invokes the provided function at most once
|
|
1283
|
+
* every `wait` milliseconds.
|
|
1312
1284
|
*
|
|
1313
|
-
*
|
|
1285
|
+
* If the `leading` option is set to `true`, the function will be invoked immediately
|
|
1286
|
+
* on the leading edge of the throttle interval. If the `trailing` option is set to `true`,
|
|
1287
|
+
* the function will also be invoked at the end of the throttle interval if additional
|
|
1288
|
+
* calls were made during the interval.
|
|
1314
1289
|
*
|
|
1315
|
-
*
|
|
1316
|
-
*
|
|
1290
|
+
* The returned function includes the `isPending` property to check if the throttle
|
|
1291
|
+
* timer is currently active.
|
|
1317
1292
|
*
|
|
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.
|
|
1293
|
+
* @typeParam F - The type of the function to throttle.
|
|
1326
1294
|
*
|
|
1327
|
-
* @param
|
|
1328
|
-
* @
|
|
1295
|
+
* @param function_ - The function to throttle.
|
|
1296
|
+
* @param wait - The number of milliseconds to wait between invocations (default is 100ms).
|
|
1297
|
+
* @param options - An optional object with the following properties:
|
|
1298
|
+
* - `leading` (boolean): If `true`, invokes the function on the leading edge of the interval.
|
|
1299
|
+
* - `trailing` (boolean): If `true`, invokes the function on the trailing edge of the interval.
|
|
1300
|
+
*
|
|
1301
|
+
* @returns A throttled version of the provided function, enhanced with the `isPending` property.
|
|
1302
|
+
*
|
|
1303
|
+
* @throws {TypeError} If the first parameter is not a function.
|
|
1304
|
+
* @throws {RangeError} If the `wait` parameter is negative.
|
|
1329
1305
|
*
|
|
1330
1306
|
* @example
|
|
1331
|
-
*
|
|
1332
|
-
*
|
|
1333
|
-
*
|
|
1307
|
+
* ```ts
|
|
1308
|
+
* // Basic throttling (leading edge by default)
|
|
1309
|
+
* const log = throttle((message: string) => console.log(message), 200);
|
|
1310
|
+
* log('Hello'); // Logs "Hello" immediately
|
|
1311
|
+
* log('World'); // Ignored for 200ms
|
|
1312
|
+
* console.log(log.isPending); // true if within throttle window
|
|
1313
|
+
*
|
|
1314
|
+
* // Trailing edge only
|
|
1315
|
+
* const trailingLog = throttle(() => console.log('trailing'), 200, {
|
|
1316
|
+
* leading: false,
|
|
1317
|
+
* trailing: true
|
|
1318
|
+
* });
|
|
1319
|
+
* trailingLog(); // No immediate execution
|
|
1320
|
+
* // After 200ms: logs "trailing"
|
|
1321
|
+
*
|
|
1322
|
+
* // Both edges
|
|
1323
|
+
* const bothLog = throttle(() => console.log('both'), 200, {
|
|
1324
|
+
* leading: true,
|
|
1325
|
+
* trailing: true
|
|
1326
|
+
* });
|
|
1327
|
+
* bothLog(); // Immediate execution
|
|
1328
|
+
* // After 200ms: executes again if called during window
|
|
1329
|
+
* ```
|
|
1334
1330
|
*/
|
|
1335
|
-
declare
|
|
1331
|
+
declare function throttle<F$1 extends (...args: any[]) => any>(function_: F$1, wait?: number, options?: {
|
|
1332
|
+
leading?: boolean;
|
|
1333
|
+
trailing?: boolean;
|
|
1334
|
+
}): ThrottledFunction<F$1>;
|
|
1336
1335
|
/**
|
|
1337
|
-
*
|
|
1336
|
+
* Formats a string by replacing each '%s' placeholder with the corresponding argument.
|
|
1338
1337
|
*
|
|
1339
|
-
*
|
|
1340
|
-
*
|
|
1338
|
+
* Mimics the basic behavior of C's printf for %s substitution. Supports both
|
|
1339
|
+
* variadic arguments and array-based argument passing. Extra placeholders
|
|
1340
|
+
* are left as-is, missing arguments result in empty strings.
|
|
1341
|
+
*
|
|
1342
|
+
* @param format - The format string containing '%s' placeholders.
|
|
1343
|
+
* @param args - The values to substitute, either as separate arguments or a single array.
|
|
1344
|
+
* @returns The formatted string with placeholders replaced by arguments.
|
|
1341
1345
|
*
|
|
1342
1346
|
* @example
|
|
1343
|
-
*
|
|
1344
|
-
*
|
|
1345
|
-
*
|
|
1347
|
+
* ```ts
|
|
1348
|
+
* // Basic usage with separate arguments
|
|
1349
|
+
* printf("%s love %s", "I", "Bangladesh") // "I love Bangladesh"
|
|
1350
|
+
*
|
|
1351
|
+
* // Using array of arguments
|
|
1352
|
+
* printf("%s love %s", ["I", "Bangladesh"]) // "I love Bangladesh"
|
|
1353
|
+
*
|
|
1354
|
+
* // Extra placeholders remain unchanged
|
|
1355
|
+
* printf("%s %s %s", "Hello", "World") // "Hello World %s"
|
|
1356
|
+
*
|
|
1357
|
+
* // Missing arguments become empty strings
|
|
1358
|
+
* printf("%s and %s", "this") // "this and "
|
|
1359
|
+
*
|
|
1360
|
+
* // Multiple occurrences
|
|
1361
|
+
* printf("%s %s %s", "repeat", "repeat", "repeat") // "repeat repeat repeat"
|
|
1362
|
+
* ```
|
|
1346
1363
|
*/
|
|
1347
|
-
declare
|
|
1364
|
+
declare function printf(format: string, ...args: unknown[]): string;
|
|
1348
1365
|
/**
|
|
1349
|
-
*
|
|
1366
|
+
* Escapes a string for use in a regular expression.
|
|
1350
1367
|
*
|
|
1351
|
-
* @param
|
|
1352
|
-
* @returns
|
|
1368
|
+
* @param str - The string to escape
|
|
1369
|
+
* @returns - The escaped string safe for use in RegExp constructor
|
|
1353
1370
|
*
|
|
1354
1371
|
* @example
|
|
1355
|
-
*
|
|
1356
|
-
*
|
|
1357
|
-
*
|
|
1372
|
+
* ```ts
|
|
1373
|
+
* const escapedString = escapeRegExp('Hello, world!');
|
|
1374
|
+
* // escapedString === 'Hello\\, world!'
|
|
1375
|
+
*
|
|
1376
|
+
* const regex = new RegExp(escapeRegExp(userInput));
|
|
1377
|
+
* ```
|
|
1358
1378
|
*/
|
|
1359
|
-
declare
|
|
1379
|
+
declare function escapeRegExp(str: string): string;
|
|
1360
1380
|
/**
|
|
1361
|
-
*
|
|
1381
|
+
* Normalizes a string by:
|
|
1382
|
+
* - Applying Unicode normalization (NFC)
|
|
1383
|
+
* - Optionally removing diacritic marks (accents)
|
|
1384
|
+
* - Optionally trimming leading/trailing non-alphanumeric characters
|
|
1385
|
+
* - Optionally converting to lowercase
|
|
1362
1386
|
*
|
|
1363
|
-
* @param
|
|
1364
|
-
* @
|
|
1387
|
+
* @param str - The string to normalize
|
|
1388
|
+
* @param options - Normalization options
|
|
1389
|
+
* @param options.lowercase - Whether to convert the result to lowercase (default: true)
|
|
1390
|
+
* @param options.removeAccents - Whether to remove diacritic marks like accents (default: true)
|
|
1391
|
+
* @param options.removeNonAlphanumeric - Whether to trim non-alphanumeric characters from the edges (default: true)
|
|
1392
|
+
* @returns The normalized string
|
|
1365
1393
|
*
|
|
1366
1394
|
* @example
|
|
1367
|
-
*
|
|
1368
|
-
*
|
|
1369
|
-
*
|
|
1395
|
+
* ```ts
|
|
1396
|
+
* normalizeText('Café') // 'cafe'
|
|
1397
|
+
* normalizeText(' Hello! ') // 'hello'
|
|
1398
|
+
* normalizeText('José', { removeAccents: false }) // 'josé'
|
|
1399
|
+
* ```
|
|
1370
1400
|
*/
|
|
1371
|
-
declare function
|
|
1401
|
+
declare function normalizeText(str?: string | null, options?: {
|
|
1402
|
+
lowercase?: boolean;
|
|
1403
|
+
removeAccents?: boolean;
|
|
1404
|
+
removeNonAlphanumeric?: boolean;
|
|
1405
|
+
}): string;
|
|
1372
1406
|
//#endregion
|
|
1373
|
-
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,
|
|
1407
|
+
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, extract, hydrate, isArray, isBoolean, isFalsy, isFiniteNumber, isFunction, isNullish, isPlainObject, isPrimitive, isString, normalizeText, poll, printf, schedule, shield, sleep, throttle, withConcurrency };
|