-
-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathmisc.ts
More file actions
711 lines (656 loc) · 22.2 KB
/
misc.ts
File metadata and controls
711 lines (656 loc) · 22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/* ===========================================================
MISCELLANEOUS
- LITERALS
- CLASS_TRANSFORM
- CLONE
- PRUNE
- FACTORY FUNCTIONS
==============================================================
LITERALS
----------------------------------------------------------- */
import { NoTransformConfigurationError } from "./transformers/NoTransformConfigurationError";
import { Atomic } from "./typings/Atomic";
import { IValidation } from "./IValidation";
import { Resolved } from "./Resolved";
import { TypeGuardError } from "./TypeGuardError";
/**
* > You must configure the generic argument `T`.
*
* Union literal type to array.
*
* Converts a union literal type to an array of its members.
*
* ```typescript
* literals<"A" | "B" | 1>; // ["A", "B", 1]
* ```
*
* @template T Union literal type
* @return Array of union literal type's members
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function literals(): never;
/**
* Union literal type to array.
*
* Converts a union literal type to an array of its members.
*
* ```typescript
* literals<"A" | "B" | 1>; // ["A", "B", 1]
* ```
*
* @template T Union literal type
* @return Array of union literal type's members
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function literals<T extends Atomic.Type | null>(): T[];
/**
* @internal
*/
export function literals(): never {
NoTransformConfigurationError("misc.literals");
}
/* -----------------------------------------------------------
CLASS_TRANSFORM
----------------------------------------------------------- */
/**
* Transform input into a class instance.
*
* Transforms a primitive input data into an instance following type `T`. This function
* converts plain objects, arrays, and primitive values into the specified class type,
* instantiating any necessary class constructors and applying the correct prototype.
*
* For reference, this `typia.misc.classTransform()` function does not validate the input value
* type. It assumes that the input value can be transformed to the type `T`. If you need
* type validation, it would be better to call {@link assertClassTransform} function instead.
*
* @template T Type of the target class
* @param input A value to be transformed into class instance
* @return Transformed class instance
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function classTransform<T>(input: unknown): T;
/**
* @internal
*/
export function classTransform(): never {
NoTransformConfigurationError("misc.classTransform");
}
/**
* Transform input into a class instance with type assertion.
*
* Transforms a primitive input data into an instance following type `T`, with type assertion.
* This function converts plain objects, arrays, and primitive values into the specified class type,
* instantiating any necessary class constructors and applying the correct prototype.
*
* When `input` value is not compatible with the type `T`, it throws an
* {@link TypeGuardError} or custom error generated by *errorFactory*. Otherwise,
* if there's no problem with the `input` value, transformed class instance would be returned.
*
* @template T Type of the target class
* @param input A value to be transformed into class instance
* @param errorFactory Custom error factory. Default is `TypeGuardError`
* @return Transformed class instance
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function assertClassTransform<T>(
input: unknown,
errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),
): T;
/**
* @internal
*/
export function assertClassTransform(): never {
NoTransformConfigurationError("misc.assertClassTransform");
}
/* -----------------------------------------------------------
CLONE
----------------------------------------------------------- */
/**
* Clone data.
*
* Clones an instance following type `T`. If the target *input* value or its member
* variable contains a class instance having methods, those methods would not be
* cloned.
*
* For reference, this `typia.misc.clone()` function does not validate the input value
* type. It just believes that the input value is following the type `T`. Therefore,
* if you can't ensure the input value type, it would be better to call
* {@link assertClone} function instead.
*
* @template T Type of the input value
* @param input A value to be cloned
* @return Cloned data
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function clone<T>(input: T): Resolved<T>;
/**
* @internal
*/
export function clone(): never {
NoTransformConfigurationError("misc.clone");
}
/**
* Clone data with type assertion.
*
* Clones an instance following type `T`, with type assertion. If the target `input`
* value or its member variable contains a class instance having methods, those
* methods would not be cloned.
*
* In such reason, when `input` value is not matched with the type `T`, it throws an
* {@link TypeGuardError} or custom error generated by *errorFactory*. Otherwise,
* there's no problem on the `input` value, cloned data would be returned.
*
* @template T Type of the input value
* @param input A value to be cloned
* @param errorFactory Custom error factory. Default is `TypeGuardError`
* @return Cloned data
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function assertClone<T>(
input: T,
errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),
): Resolved<T>;
/**
* Clone data with type assertion.
*
* Clones an instance following type `T`, with type assertion. If the target `input`
* value or its member variable contains a class instance having methods, those
* methods would not be cloned.
*
* In such reason, when `input` value is not matched with the type `T`, it throws an
* {@link TypeGuardError} or custom error generated by *errorFactory*. Otherwise,
* there's no problem on the `input` value, cloned data would be returned.
*
* @template T Type of the input value
* @param input A value to be cloned
* @param errorFactory Custom error factory. Default is `TypeGuardError`
* @return Cloned data
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function assertClone<T>(
input: unknown,
errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),
): Resolved<T>;
/**
* @internal
*/
export function assertClone(): never {
NoTransformConfigurationError("misc.assertClone");
}
/**
* Clone data with type checking.
*
* Clones an instance following type `T`, with type checking. If the target `input`
* value or its member variable contains a class instance having methods, those
* methods would not be cloned.
*
* In such reason, when `input` value is not matched with the type `T`, it returns
* `null` value instead. Otherwise, there's no problem on the `input` value, cloned
* data would be returned.
*
* @template T Type of the input value
* @param input A value to be cloned
* @return Cloned data when exact type, otherwise null
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function isClone<T>(input: T): Resolved<T> | null;
/**
* Clone data with type checking.
*
* Clones an instance following type `T`, with type checking. If the target `input`
* value or its member variable contains a class instance having methods, those
* methods would not be cloned.
*
* In such reason, when `input` value is not matched with the type `T`, it returns
* `null` value instead. Otherwise, there's no problem on the `input` value, cloned
* data would be returned.
*
* @template T Type of the input value
* @param input A value to be cloned
* @return Cloned data when exact type, otherwise null
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function isClone<T>(input: unknown): Resolved<T> | null;
/**
* @internal
*/
export function isClone(): never {
NoTransformConfigurationError("misc.isClone");
}
/**
* Clone data with detailed type validation.
*
* Clones an instance following type `T`, with detailed type validation. If the target
* `input` value or its member variable contains a class instance having methods,
* those methods would not be cloned.
*
* In such reason, when `input` value is not matched with the type `T`, it returns
* {@link IValidation.Failure} value. Otherwise, there's no problem on the `input`
* value, cloned data would be stored in `data` property of the output
* {@link IValidation.Success} instance.
*
* @template T Type of the input value
* @param input A value to be cloned
* @returns Validation result with cloned value
*/
export function validateClone<T>(input: T): IValidation<Resolved<T>>;
/**
* Clone data with detailed type validation.
*
* Clones an instance following type `T`, with detailed type validation. If the target
* `input` value or its member variable contains a class instance having methods,
* those methods would not be cloned.
*
* In such reason, when `input` value is not matched with the type `T`, it returns
* {@link IValidation.Failure} value. Otherwise, there's no problem on the `input`
* value, cloned data would be stored in `data` property of the output
* {@link IValidation.Success} instance.
*
* @template T Type of the input value
* @param input A value to be cloned
* @returns Validation result with cloned value
*/
export function validateClone<T>(input: unknown): IValidation<Resolved<T>>;
/**
* @internal
*/
export function validateClone(): never {
NoTransformConfigurationError("misc.validateClone");
}
/* -----------------------------------------------------------
PRUNE
----------------------------------------------------------- */
/**
* Prune, erase superfluous properties.
*
* Remove all superfluous properties from the `input` object, even including nested
* objects. Note that, as all superfluous properties would be deleted, you never can
* read those superfluous properties after calling this `prune()` function.
*
* For reference, this `typia.misc.prune()` function does not validate the input value
* type. It just believes that the input value is following the type `T`. Therefore,
* if you can't ensure the input value type, it would better to call one of below
* functions instead.
*
* - {@link assertPrune}
* - {@link isPrune}
* - {@link validatePrune}
*
* @template T Type of the input value
* @param input Target instance to prune
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function prune<T extends object>(input: T): void;
/**
* @internal
*/
export function prune(): never {
NoTransformConfigurationError("misc.prune");
}
/**
* Prune, erase superfluous properties, with type assertion.
*
* `typia.misc.assertPrune()` is a combination function of {@link assert} and
* {@link prune}. Therefore, it removes all superfluous properties from the `input`
* object including nested objects, with type assertion.
*
* In such reason, when `input` value is not matched with the type `T`, it throws an
* {@link TypeGuardError} or custom error generated by *errorFactory*. Otherwise,
* there's no problem on the `input` value, its all superfluous properties would be
* removed, including nested objects.
*
* @template T Type of the input value
* @param input Target instance to assert and prune
* @param errorFactory Custom error factory. Default is `TypeGuardError`
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function assertPrune<T>(
input: T,
errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),
): T;
/**
* Prune, erase superfluous properties, with type assertion.
*
* `typia.misc.assertPrune()` is a combination function of {@link assert} and
* {@link prune}. Therefore, it removes all superfluous properties from the `input`
* object including nested objects, with type assertion.
*
* In such reason, when `input` value is not matched with the type `T`, it throws an
* {@link TypeGuardError} or custom error generated by *errorFactory*. Otherwise, there's
* no problem on the `input` value, its all superfluous properties would be removed,
* including nested objects.
*
* @template T Type of the input value
* @param input Target instance to assert and prune
* @param errorFactory Custom error factory. Default is `TypeGuardError`
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function assertPrune<T>(
input: unknown,
errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),
): T;
/**
* @internal
*/
export function assertPrune(): unknown {
NoTransformConfigurationError("misc.assertPrune");
}
/**
* Prune, erase superfluous properties, with type checking.
*
* `typia.misc.isPrune()` is a combination function of {@link is} and
* {@link prune}. Therefore, it removes all superfluous properties from the `input`
* object including nested objects, with type checking.
*
* In such reason, when `input` value is not matched with the type `T`, it returns
* `false` value. Otherwise, there's no problem on the `input` value, it returns
* `true` after removing all superfluous properties, including nested objects.
*
* @template T Type of the input value
* @param input Target instance to check and prune
* @returns Whether the parametric value is following the type `T` or not
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function isPrune<T>(input: T): input is T;
/**
* Prune, erase superfluous properties, with type checking.
*
* `typia.misc.isPrune()` is a combination function of {@link is} and
* {@link prune}. Therefore, it removes all superfluous properties from the `input`
* object including nested objects, with type checking.
*
* In such reason, when `input` value is not matched with the type `T`, it returns
* `false` value. Otherwise, there's no problem on the `input` value, it returns
* `true` after removing all superfluous properties, including nested objects.
*
* @template T Type of the input value
* @param input Target instance to check and prune
* @returns Whether the parametric value is following the type `T` or not
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function isPrune<T>(input: unknown): input is T;
/**
* @internal
*/
export function isPrune(): never {
NoTransformConfigurationError("misc.isPrune");
}
/**
* Prune, erase superfluous properties, with type validation.
*
* `typia.misc.validatePrune()` is a combination function of {@link validate} and
* {@link prune}. Therefore, it removes all superfluous properties from the `input`
* object including nested objects, with type validation.
*
* In such reason, when `input` value is not matched with the type `T`, it returns
* {@link IValidation.IFailure} value with detailed error reasons. Otherwise, there's
* no problem on the `input` value, it returns {@link IValidation.ISuccess} value after
* removing all superfluous properties, including nested objects.
*
* @template T Type of the input value
* @param input Target instance to validate and prune
* @returns Validation result
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function validatePrune<T>(input: T): IValidation<T>;
/**
* Prune, erase superfluous properties, with type validation.
*
* `typia.misc.validatePrune()` is a combination function of {@link validate} and
* {@link prune}. Therefore, it removes all superfluous properties from the `input`
* object including nested objects, with type validation.
*
* In such reason, when `input` value is not matched with the type `T`, it returns
* {@link IValidation.IFailure} value with detailed error reasons. Otherwise, there's
* no problem on the `input` value, it returns {@link IValidation.ISuccess} value after
* removing all superfluous properties, including nested objects.
*
* @template T Type of the input value
* @param input Target instance to validate and prune
* @returns Validation result
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function validatePrune<T>(input: unknown): IValidation<T>;
/**
* @internal
*/
export function validatePrune<T>(): IValidation<T> {
NoTransformConfigurationError("misc.validatePrune");
}
/* -----------------------------------------------------------
FACTORY FUNCTIONS
----------------------------------------------------------- */
/**
* Creates a reusable {@link clone} function.
*
* @danger You must configure the generic argument `T`
* @returns Nothing until you configure the generic argument `T`
* @throws compile error
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function createClone(): never;
/**
* Creates a reusable {@link clone} function.
*
* @template T Type of the input value
* @returns A reusable `clone` function
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function createClone<T>(): (input: T) => Resolved<T>;
/**
* @internal
*/
export function createClone(): never {
NoTransformConfigurationError("misc.createClone");
}
/**
* Creates a reusable {@link assertClone} function.
*
* @danger You must configure the generic argument `T`
* @param errorFactory Custom error factory. Default is `TypeGuardError`
* @returns Nothing until you configure the generic argument `T`
* @throws compile error
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function createAssertClone(
errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),
): never;
/**
* Creates a reusable {@link assertClone} function.
*
* @template T Type of the input value
* @param errorFactory Custom error factory. Default is `TypeGuardError`
* @returns A reusable `clone` function
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function createAssertClone<T>(
errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),
): (input: unknown) => Resolved<T>;
/**
* @internal
*/
export function createAssertClone(): never {
NoTransformConfigurationError("misc.createAssertClone");
}
/**
* Creates a reusable {@link isClone} function.
*
* @danger You must configure the generic argument `T`
* @returns Nothing until you configure the generic argument `T`
* @throws compile error
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function createIsClone(): never;
/**
* Creates a reusable {@link isClone} function.
*
* @template T Type of the input value
* @returns A reusable `clone` function
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function createIsClone<T>(): (input: unknown) => Resolved<T> | null;
/**
* @internal
*/
export function createIsClone(): never {
NoTransformConfigurationError("misc.createIsClone");
}
/**
* Creates a reusable {@link validateClone} function.
*
* @danger You must configure the generic argument `T`
* @returns Nothing until you configure the generic argument `T`
* @throws compile error
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function createValidateClone(): never;
/**
* Creates a reusable {@link validateClone} function.
*
* @template T Type of the input value
* @returns A reusable `clone` function
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function createValidateClone<T>(): (
input: unknown,
) => IValidation<Resolved<T>>;
/**
* @internal
*/
export function createValidateClone(): never {
NoTransformConfigurationError("misc.createValidateClone");
}
/**
* Creates a reusable {@link prune} function.
*
* @danger You must configure the generic argument `T`
* @returns Nothing until you configure the generic argument `T`
* @throws compile error
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function createPrune(): never;
/**
* Creates a reusable {@link prune} function.
*
* @template T Type of the input value
* @returns A reusable `prune` function
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function createPrune<T extends object>(): (input: T) => void;
/**
* @internal
*/
export function createPrune<T extends object>(): (input: T) => void {
NoTransformConfigurationError("misc.createPrune");
}
/**
* Creates a reusable {@link assertPrune} function.
*
* @danger You must configure the generic argument `T`
* @param errorFactory Custom error factory. Default is `TypeGuardError`
* @returns Nothing until you configure the generic argument `T`
* @throws compile error
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function createAssertPrune(
errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),
): never;
/**
* Creates a reusable {@link assertPrune} function.
*
* @template T Type of the input value
* @param errorFactory Custom error factory. Default is `TypeGuardError`
* @returns A reusable `isPrune` function
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function createAssertPrune<T extends object>(
errorFactory?: undefined | ((props: TypeGuardError.IProps) => Error),
): (input: T) => T;
/**
* @internal
*/
export function createAssertPrune<T extends object>(): (input: T) => T {
NoTransformConfigurationError("misc.createAssertPrune");
}
/**
* Creates a reusable {@link isPrune} function.
*
* @danger You must configure the generic argument `T`
* @returns Nothing until you configure the generic argument `T`
* @throws compile error
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function createIsPrune(): never;
/**
* Creates a reusable {@link isPrune} function.
*
* @template T Type of the input value
* @returns A reusable `isPrune` function
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function createIsPrune<T extends object>(): (input: T) => input is T;
/**
* @internal
*/
export function createIsPrune<T extends object>(): (input: T) => input is T {
NoTransformConfigurationError("misc.createIsPrune");
}
/**
* Creates a reusable {@link validatePrune} function.
*
* @danger You must configure the generic argument `T`
* @returns Nothing until you configure the generic argument `T`
* @throws compile error
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function createValidatePrune(): never;
/**
* Creates a reusable {@link validatePrune} function.
*
* @template T Type of the input value
* @returns A reusable `validatePrune` function
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function createValidatePrune<T extends object>(): (
input: T,
) => IValidation<T>;
/**
* @internal
*/
export function createValidatePrune<T extends object>(): (
input: T,
) => IValidation<T> {
NoTransformConfigurationError("misc.createValidatePrune");
}