-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoerce.ts
1164 lines (1078 loc) · 31.9 KB
/
coerce.ts
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//#region Coerce
// -----------------------------------------------------------------------------
const SymbolGuardTest = Symbol('GuardTest');
const SymbolOtherwise = Symbol('Otherwise');
/**
* Chain a set of coercing functions.
* @example
* ```ts
* import { to, string } from '@resolute/std/coerce';
* const mustBeString = to(string);
* mustBeString('foo'); // 'foo'
* mustBeString(1); // throws TypeError
* ```
* @param coercers unary guards, sanitizers, mutators
* @returns pipeline of coercers
*/
export const to: Coerce = (...coercers: UnaryFunction[]) => {
const coercerLengthMinusOne = coercers.length - 1;
const lastCoercer = coercers[coercerLengthMinusOne];
if (not(own(SymbolOtherwise))(lastCoercer)) {
return pipe(...coercers);
}
return <T>(value: T) => {
const { value: otherwise } = lastCoercer;
const revised = coercers.slice(0, coercerLengthMinusOne);
try {
return pipe(...revised)(value);
} catch (error) {
if (is(instance(Error))(otherwise)) {
throw otherwise;
}
return otherwise;
}
};
};
/**
* Alias `to`
* @see to
*/
export const coerce = /* @__PURE__ */ to;
/**
* Provide a backup value to be used when coercion fails. If `value` is an
* `instanceof Error`, then that error will be `throw`n. Any other `value` will
* be returned as-is.
* @example
* ```ts
* import { or, to, string } from '@resolute/std/coerce';
* to(string, or(null))('foo'); // 'foo'
* to(string, or(null))(1); // 1
* ```
* @param value backup value if any coercers fail
* @returns backup value to be used in `to` chain
*/
export const or = <Y>(value: NonFunction<Y>) => ({
value,
[SymbolOtherwise]: true,
});
/**
* Type guard test. Use with any type guard or mutating function that `throw`s
* on failure (almost all functions here do). The `is` function will catch the
* error and return `false`, otherwise it will return `true`.
* @example
* ```ts
* import { is, string } from '@resolute/std/coerce';
* is(string)('foo'); // true
* is(string)(12345); // false
* ```
* @param coercer any type guard or mutating function
* @returns boolean
*/
export const is = <A extends UnaryFunction>(coercer: A) => {
const guard = (value: unknown): value is ReturnType<A> => {
try {
coercer(value);
return true;
} catch {
return false;
}
};
(guard as Is<A>)[SymbolGuardTest] = <T extends ReturnType<A>>(value: T) => {
coercer(value);
return value;
};
return guard as Is<A>;
};
/**
* Negate type guard test. Use with any type guard or mutating function that
* `throw`s on failure (almost all functions here do). The `not` function will
* catch the error and return `true`, otherwise it will return `false`.
* @example
* ```ts
* import { not, string } from '@resolute/std/coerce';
* not(string)('foo'); // false
* not(string)(12345); // true
* ```
* @param coercer any type guard or mutating function
* @returns
*/
export const not = <A extends UnaryFunction>(coercer: A) => {
const guard = <T>(value: T): value is T extends ReturnType<A> ? never : T => !is(coercer)(value);
(guard as Not<A>)[SymbolGuardTest] = <T>(value: T) => {
try {
coercer(value);
} catch {
return value as T extends ReturnType<A> ? never : T;
}
throw exception(value, `something else`);
};
return guard as Not<A>;
};
/**
* Construct uniform `TypeError`s
* @param actual value
* @param expected value
* @returns TypeError
*/
const exception = (actual: any, expected: string) =>
new TypeError(`Expected “${actual}” to be ${expected}.`);
/**
* Create a pipe of unary functions
*/
const pipe: To = (...fns: UnaryFunction[]) =>
fns.reduce.bind(fns.map(guardInPipe), (value, fn) => fn(value)) as UnaryFunction;
/**
* Since type guard functions return boolean, we need to wrap them when they are
* used within `pipe` so that the value is passed along instead of a boolean.
* @param coercer type guard (result of `is` or `not`) function or a coerce function
* @returns
*/
const guardInPipe = <T extends Is<R> | UnaryFunction, R extends UnaryFunction>(
coercer: T,
) => {
if (is(own(SymbolGuardTest))(coercer)) {
return coercer[SymbolGuardTest];
}
return coercer;
};
//#endregion
//#region Guards
// -----------------------------------------------------------------------------
/**
* `string` Guard
* @param value unknown
* @returns value
* @throws if value is not `string`
*/
export const string = (value: string): string => {
if (typeof value === 'string') {
return value;
}
throw exception(value, 'a string');
};
/**
* `number` Guard
* @param value unknown
* @returns value
* @throws if value is not `number`
*/
export const number = (value: number): number => {
if (typeof value === 'number' && Number.isFinite(value)) {
return value;
}
throw exception(value, 'a finite number');
};
/**
* `bigint` Guard
* @param value unknown
* @returns value
* @throws if value is not `bigint`
*/
export const bigint = (value: bigint): bigint => {
if (typeof value === 'bigint') {
return value;
}
throw exception(value, 'a bigint');
};
/**
* `Date` Guard
* @param value unknown
* @returns value
* @throws if value is not `Date`
*/
export const date = (value: Date): Date => {
try {
const valid = to(object, instance(Date))(value);
to(finite, nonzero)(valid.valueOf());
return valid;
} catch {
throw exception(value, 'a date');
}
};
/**
* `Array` Guard
* @param value unknown
* @returns value
* @throws if value is not `Array`
*/
export const array = <T extends readonly any[]>(value: T): T => {
if (Array.isArray(value)) {
return value;
}
throw exception(value, 'an array');
};
/**
* `Iterable` Guard
* @param value unknown
* @returns value
* @throws if value is not `Iterable`
*/
export const iterable = <T extends Iterable<any>>(value: T): T => {
if (Symbol.iterator in value) {
return value;
}
throw exception(value, 'iterable');
};
/**
* Not `undefined` | `null` Guard
* @param value unknown
* @returns value
* @throws if value is `undefined` or `null`
*/
export const defined = <T>(value: T): NonNullable<T> => {
if (typeof value !== 'undefined' && value !== null) {
return value as NonNullable<T>;
}
throw exception(value, 'defined');
};
/**
* `object` Guard
* @param value unknown
* @returns value
* @throws if value is not an `object`
*/
export const object = <T>(value: T): NonNullable<T> => {
if (typeof value === 'object' && value !== null) {
return value;
}
throw exception(value, 'an object');
};
/**
* `function` Guard
* @param value unknown
* @returns value
* @throws if value is not an `function`
*/
export const func = <T extends Function>(value: T): T => {
if (typeof value === 'function') {
return value;
}
throw exception(value, 'a function');
};
/**
* `instanceof …` Guard
* @param value unknown
* @returns value
* @throws if value is not `instanceof …`
*/
export const instance = <T extends Constructor>(constructor: T) => (value: unknown) => {
if (is(func)(constructor) && value instanceof constructor) {
return value as InstanceType<T>;
}
throw exception(value, `an instance of ${constructor}`);
};
export const own = <K extends PropertyKey>(property: K) => <T extends {}>(value: T) => {
if (Object.hasOwn(value, property)) {
return value as
& T
// Test the intersection of `T` and `{ [property]: unknown }`
& Extract<T, { [_ in K]: unknown }> extends never
// When it fails, this will throw, so if it passes, then the result is at a minimum `{ [property]: unknown }`
? { [_ in K]: unknown }
// Successful path is a union of `T` and `{ [property]: unknown }`
: Extract<T, { [_ in K]: unknown }>;
}
throw exception(value, `an object with own “${String(property)}” property`);
};
//#endregion
//#region Validators
// -----------------------------------------------------------------------------
/**
* Alias `number`
* @param value number
* @returns value
* @throws if value is not finite
* @see number
*/
export const finite = /* @__PURE__ */ number;
/**
* Number is 0
* @param value number
* @returns value
* @throws if value is not 0
*/
export const zero = (value: number) => {
if (value === 0) {
return value as 0;
}
throw exception(value, '0');
};
/**
* Alias `not(zero)`
* @see zero
*/
export const nonzero = /* @__PURE__ */ to(not(zero)) as <T extends number>(
input: T,
) => T extends 0 ? never : T;
/**
* Number > 0
* @param value number
* @returns value
* @throws if value <= 0
*/
export const positive = /* @__PURE__ */ to(
nonzero,
(value: number) => {
if (value > 0) {
return value;
}
throw exception(value, 'a positive number');
},
) as (input: number) => number;
/**
* Number < 0
* @param value number
* @returns value
* @throws if value >= 0
*/
export const negative = (value: number) =>
to(
nonzero,
not(positive),
or(exception(value, 'a negative number')),
)(value);
/**
* Date is in the future
* @param value date
* @returns value
* @throws if date is in the past
*/
export const future = (value: Date) => {
if (value.valueOf() > Date.now()) {
return value;
}
throw exception(value, 'in the future');
};
/**
* Date is in the past
* @param value date
* @returns value
* @throws if date is in the future
*/
export const past = (value: Date) => {
if (value.valueOf() < Date.now()) {
return value;
}
throw exception(value, 'in the past');
};
/**
* Length of string or array is exactly `size`
* @param size length
* @returns validator
*/
export const length = <N extends number>(size: N) =>
/**
* Length of string or array is exactly `size`
* @param value `string` or `array`
* @returns value
* @throws if value is not of length `size`
*/
<T extends { length: number }>(value: T) => {
if (value?.length === size) {
return value as T & { length: N };
}
throw exception(value, `of length: ${size}`);
};
/**
* Alias `to(not(length(0)))`
* @see length
*/
// `not` kind of messes up the types here. Explicit type definition used:
// export const nonempty = <T>(value: T) => to(not(length(0)))(value) as T & { length: number };
export const nonempty = /* @__PURE__ */ to(not(length(0))) as <T extends { length: number }>(
value: T,
) => T;
/**
* `value` is within `list` (a.k.a. Enum)
*/
export const within = <T>(list: readonly T[]) =>
/**
* @param value member of `list`
* @returns value
* @throws if value is not a member of `list`
*/
<V>(value: V) => {
if (list.indexOf(value as unknown as T) >= 0) {
return value as V & T;
}
throw exception(value, `one of ${list}`);
};
/**
* Validate against the Luhn algorithm (adapted from
* https://github.com/bendrucker/fast-luhn).
* @param value string of digits
* @returns value
* @throws if value does not pass Luhn algorithm
*/
export const luhn = (value: string) => {
let { length } = value;
let bit = 1;
let sum = 0;
while (length) {
const int = parseInt(value.charAt(--length), 10);
bit ^= 1;
sum += bit ? [0, 2, 4, 6, 8, 1, 3, 5, 7, 9][int] : int;
}
if (sum % 10 === 0) {
return value;
}
throw exception(value, 'able to pass the Luhn test');
};
//#endregion
//#region Mutators
// -----------------------------------------------------------------------------
/**
* `string` Mutation
* @param value string | number | bigint
* @returns string
* @throws if value cannot be mutated to `string`
*/
export const stringify = <T extends string | number | bigint>(value: T) => {
if (is(finite)(value) || is(bigint)(value)) {
return value.toString();
}
return string(value as string);
};
/**
* `number` Mutation
* @param value string | number | bigint
* @returns number
* @throws if value cannot be mutated to `number`
*/
export const numeric = <T extends string | number | bigint>(value: T) => {
if (is(bigint)(value)) {
if (value > Number.MIN_SAFE_INTEGER && value < Number.MAX_SAFE_INTEGER) {
return Number(value);
}
throw exception(value, 'between SAFE_INTEGER bounds');
}
if (is(finite)(value)) {
return value;
}
return to(
stringify,
(value: string) => value.replace(/[^0-9oex.-]/g, ''),
nonempty,
(value: string) => finite(Number(value)),
or(exception(value, 'numeric')),
)(value);
};
/**
* `Date` Mutator
* @param value number | string | Date
* @returns Date
* @throws if value cannot be mutated to `Date`
*/
export const dateify = <T extends number | string | Date>(value: T) =>
to(date, or(exception(value, 'a date')))(new Date(value));
/**
* `boolean` Mutator
* @param truthy returned when value evaluates true
* @param falsy returned when value evaluates false (false, '0', 'false', …)
* @param nully returned when value evaluates null (null, '', 'null')
* @param undefy returned when value evaluates undefined (undefined, 'undefined', …)
* @returns boolean
*/
export const boolean = <Truthy = true, Falsy = false, Nully = Falsy, Undefy = Nully>(
...args:
| readonly []
| readonly [truthy: Truthy]
| readonly [truthy: Truthy, falsy: Falsy]
| readonly [truthy: Truthy, falsy: Falsy, nully: Nully]
| readonly [truthy: Truthy, falsy: Falsy, nully: Nully, undefy: Undefy]
) => {
// This verbose nastiness handles cases where `undefined` is passed as any of
// the truthy, falsy, nully, undefy parameters.
const truthy = (args.length < 1 ? true : args[0]) as Truthy;
const falsy = (args.length < 2 ? false : args[1]) as Falsy;
const nully = (args.length < 3 ? falsy : args[2]) as Nully;
const undefy = (args.length < 4 ? nully : args[3]) as Undefy;
return (value: unknown) => {
switch (typeof value) {
case 'undefined':
return undefy;
case 'string':
{
const trimmed = value.trim();
if (trimmed === '' || trimmed === 'null') {
return nully;
}
if (trimmed === '0' || trimmed === 'false') {
return falsy;
}
}
break;
case 'number':
if (value === 0 || !Number.isFinite(value)) {
return falsy;
}
break;
default:
if (value === null) {
return nully;
}
break;
}
return value ? truthy : falsy;
};
};
/**
* `Array` Mutator. Transform any iterable into an array [...value], except for
* `string`s. A `string` is wrapped as a single member array (`[value]`).
* @example
* ```ts
* import { arrayify } from '@resolute/std/coerce';
* arrayify('123'); // ['123']
* arrayify(new Set([1,2,3])); // [1,2,3]
* ```
* @param value any iterable (Map, Set, …)
* @returns array
*/
export const arrayify = <T>(value: T): IterableOrNot<T>[] => {
// a `string` _is_ Iterable, but we do not want to return an array of
// characters
if (is(array)(value)) {
return value as IterableOrNot<T>[];
}
if (!is(string)(value) && is(iterable)(value)) {
return [...value] as IterableOrNot<T>[];
}
return [value] as IterableOrNot<T>[];
};
/**
* Mutate iterables (except strings) and objects to an array of entries.
* @param value iterable | object (Map, Set, Headers, URLSearchParams, …)
* @returns array
* @throws if value cannot be transformed into an array of entries
*/
export const entries: Entries = <T extends Iterable<any>>(value: T) => {
if (is(iterable)(value)) {
return arrayify(value);
}
if (is(object)(value)) {
return Object.entries(value);
}
throw exception(value, `transformable to entries`);
};
/**
* Mutate to tuple pairs. Same as `entries`, but limits the entries array to a
* length of 2 for each member.
* @see entries
* @param value iterable | object
* @returns array
* @throws if value cannot be transformed into an array of entries
*/
export const pairs = <T extends Iterable<[K, V]>, K, V>(value: T) =>
entries(value)
.map(limit(2) as (value: [K, V]) => [K, V])
.filter(is(length(2)));
/**
* Gracefully wrap errors or strings with an Error wrapper. Prevents
* double-wrapping errors.
* @param wrapper Error constructor used to wrap
* @returns wrapper
* @throws if value is not a string or instanceof Error
*/
export const wrapError: WrapError = (wrapper?: ErrorConstructor) => (value: Error | string) => {
const wrap = wrapper ?? Error;
if (is(string)(value)) {
return new wrap(value);
}
if (is(instance(wrap))(value)) {
return value;
}
if (is(instance(Error))(value)) {
return new wrap((value as Error).message);
}
throw exception(value, `string, Error, or ${wrap.name}`);
};
/**
* Round to integer
* @param value number
* @returns number
* @throws if value is not a number
*/
export const integer = (value: number) => Math.round(value);
/**
* Remove dangerous characters from string
* @param value string
* @returns string
* @throws if value is not a string
*/
export const safe = (value: string) => value.replace(/[\\|";/?<>()*[\]{}=`\t\r\n]/g, '');
/**
* Replace leading and trailing whitespace from a string
* @warning Does _not_ remove half-space and other UTF SPACE-like characters. Chain
* `spaces` before `trim` in order to remove these special SPACE characters from
* the string.
* @param value string
* @returns string
* @throws if value is not a string
*/
export const trim = (value: string) => value.trim();
/**
* Replace all SPACE-like characters with a regular SPACE. Replace continuous
* multiple SPACE characters with a single SPACE.
* @see https://jkorpela.fi/chars/spaces.html
* @param value string
* @returns string
* @throws if value is not a string
*/
export const spaces = (value: string) =>
value
.replace(
/[\u00A0\u1680\u180E\u2000-\u200B\u202F\u205F\u3000\uFEFF]/g,
' ',
)
.replace(/\s+/g, ' ');
/**
* Replace ' and " with ‘’ and “” respectively.
* @param value string
* @returns string
* @throws if value is not a string
*/
export const quotes = (value: string) =>
([
// triple prime
[/'''/g, '‴'],
// beginning "
[/(\W|^)"(\w)/g, '$1“$2'],
// ending "
[/(“[^"]*)"([^"]*$|[^“"]*“)/g, '$1”$2'],
// remaining " at end of word
[/([^0-9])"/g, '$1”'],
// double prime as two single quotes
[/''/g, '″'],
// beginning '
[/(\W|^)'(\S)/g, '$1‘$2'],
// conjunction possession
[/([a-z])'([a-z])/gi, '$1’$2'],
// abbrev. years like '93
[
/(‘)([0-9]{2}[^’]*)(‘([^0-9]|$)|$|’[a-z])/gi,
'’$2$3',
],
// ending '
[/((‘[^']*)|[a-z])'([^0-9]|$)/gi, '$1’$3'],
// backwards apostrophe
[
/(\B|^)‘(?=([^‘’]*’\b)*([^‘’]*\B\W[‘’]\b|[^‘’]*$))/gi,
'$1’',
],
// double prime
[/"/g, '″'],
// prime
[/'/g, '\u2032'],
// -- → —
[/--/g, '—'],
// .. → ellipsis
[/\.\.+/g, '…'],
] as const).reduce(
(subject, [search, replacement]) => subject.replace(search, replacement),
value,
);
/**
* Capitalize the first letter of a string
* @param value string
* @returns string
* @throws if value is not a string
*/
export const ucfirst = (value: string) => value.charAt(0).toUpperCase() + value.slice(1);
export const ucFirst = ucfirst;
const hasBothUpperAndLower = (value: string) => /[A-Z]/.test(value) && /[a-z]/.test(value);
const properNameCapitalizer = (mixedCase: boolean) => (match: string) => {
const lowerCaseMatch = match.toLowerCase();
// single letters should be uppercase (middle initials, etc.)
if (match.length === 1) {
return match.toUpperCase();
}
if (match.length <= 3) {
// suffixes that should be all uppercase
if (['ii', 'iii', 'iv', 'v'].indexOf(lowerCaseMatch) > -1) {
return match.toUpperCase();
}
// compound names that should be lowercase
if (['dit', 'de', 'von'].indexOf(lowerCaseMatch) > -1) {
return lowerCaseMatch;
}
if (mixedCase) {
return match;
}
}
return (
ucfirst(lowerCaseMatch)
// McXx, MacXx, O’Xx, D’Xx
.replace(
/^(ma?c|[od]’)(\S{2,}$)/i,
(_m, p1, p2) => ucfirst(p1) + ucfirst(p2),
)
);
};
/**
* Fix capitalization of proper nouns: names, addresses
* @param value string
* @returns string
* @throws if value is not a string
*/
export const proper = (value: string) =>
value
// restrict character set for proper names and addresses
.replace(/[^A-Za-z0-9\u00C0-\u00FF’ ,-]/g, ' ')
// remove double spacing possibly introduced from previous replace
.replace(/ +/g, ' ')
// remove leading/trailing spaces possibly introduced from previous replace
.trim()
.replace(/([^ ,-]+)/g, properNameCapitalizer(hasBothUpperAndLower(value)));
/**
* Format email addresses
* @param value string
* @returns string
* @throws if value does not resemble an email
*/
export const email = /* @__PURE__ */ to(
(value: string) => value.toLowerCase().replace(/\s+/g, ''),
nonempty,
(value: string) => {
if (/[a-z0-9]@[a-z0-9]/.test(value)) {
return value;
}
throw exception(value, 'a valid email address');
},
);
/**
* Strip all non-digit characters from string
* @param value string
* @returns string with only [0-9] digits
* @throws if value is not a string
*/
export const digits = (value: string) => value.replace(/[^\d]/g, '');
/**
* Returns only the digits of a phone number without any formatting
* @param value string
* @returns string of 10+ digits
* @throws if value is not a string of 10+ digits (excluding leading 0 or 1)
*/
export const phone = (value: string) => {
const onlyDigits = digits(value).replace(/^[01]+/, '');
if (onlyDigits.length >= 10) {
return onlyDigits;
}
throw exception(value, 'a valid US phone number');
};
/**
* Requires the input phone number to be exactly 10-digits (no extension)
* @param value string
* @returns string of exactly 10 digits
* @throws if value is not a string of exactly 10 digits (excluding leading 0 or 1)
*/
export const phone10 = (value: string) => {
const valid = phone(value);
if (valid.length === 10) {
return valid;
}
throw exception(value, 'a valid US 10-digit phone number');
};
/**
* Format phone number as “(NNN) NNN-NNNN ext N…”
* @param value string
* @returns string of formatted phone number
* @throws if value is not a string of at least 10 digits (excluding leading 0 or 1)
*/
export const prettyPhone = (value: string) => {
const valid = phone(value);
if (valid.length === 10) {
return valid.replace(/(\d{3})(\d{3})(\d{4})/, '($1) $2-$3');
}
return valid.replace(/(\d{3})(\d{3})(\d{4})(\d+)/, '($1) $2-$3 ext $4');
};
/**
* 5-digit US postal code
* @param value string
* @returns string of 5-digit zip code
* @throws if value does not contain 5 digits
*/
export const postalCodeUs5 = (value: string) =>
to(
digits,
limit(5),
string,
length(5),
or(exception(value, 'a valid US postal code')),
)(value);
/**
* Limit the value of a `number`, characters in a `string`, or items in an
* `array`
* @param max number
* @returns function to limit given input
*/
export const limit = <N extends number>(max: N) =>
/**
* Limit the value of a `number`, characters in a `string`, or items in an
* `array`
* @param value
* @returns limited to `max`
* @throws if value is not a number, string, or array
*/
<T>(value: T) => {
if (is(number)(value)) {
return Math.min(value, max) as Limit<N, T>;
}
if (is(string)(value)) {
return value.slice(0, max) as Limit<N, T>;
}
if (is(array)(value)) {
return value.slice(0, max) as Limit<N, T>;
}
throw exception(value, `able to be limited to ${max}`);
};
/**
* Split a string into an array. Optionally define a separator RegExp. Default
* separator is comma, newline, space, tab.
* @param separator default: `/[,\r\n\s]+/g` commas, newlines, spaces, tabs
* @param limit optionally limit the number of items in result
*/
export const split = (separator = /[,\r\n\s]+/g, limit?: number) =>
/**
* Split a string by given `separator` (default: comma, newline, space, tab).
* Remove empty strings from returned array.
* @example
* ```ts
* import { split } from '@resolute/std/coerce';
* split()('a,b,,,c d e foo') // ['a', 'b', 'c', 'd', 'e', 'foo']
* ```
* @param value string
* @returns array of strings split by `separator`
* @throws if value is not a string
*/
(value: string) =>
value.split(separator, limit)
.map(spaces) // remove irregular spaces
.map(trim)
.filter(is(nonempty));
//#endregion
//#region Types
// -----------------------------------------------------------------------------
export type Constructor = new (...args: any[]) => any;
export type IterableOrNot<T> = T extends Iterable<infer U> ? U : T;
export type NonFunction<T> = T extends (Function | ErrorConstructor | (new (...args: any[]) => any))
? never
: T;
export interface WrapError {
(): (value: Error | string) => Error;
<T extends Constructor>(value: T): (value: Error | string) => InstanceType<T>;
}
export interface Entries {
<T>(value: Iterable<T>): T[];
<K extends string | number | symbol, V>(value: Record<K, V>): [K, V][];
<T extends Iterable<I> | Record<K, V>, I, K extends string | number | symbol, V>(
value: T,
): I[] | [K, V][];
}
export type TupleSplit<T, N extends number, O extends readonly any[] = readonly []> =
O['length'] extends N ? [O, T]
: T extends readonly [infer F, ...infer R] ? TupleSplit<readonly [...R], N, readonly [...O, F]>
: [O, T];
export type TakeFirst<T extends readonly any[], N extends number> = TupleSplit<T, N>[0];
export type SkipFirst<T extends readonly any[], N extends number> = TupleSplit<T, N>[1];
export type TupleSlice<T extends readonly any[], S extends number, E extends number> = SkipFirst<
TakeFirst<T, E>,
S
>;
export type Limit<N extends number, T> = T extends number ? number
: T extends (string | any[]) ? T & { length: N }
: T extends readonly any[] ? TakeFirst<T, N>
: never;
export type UnaryFunction = (value: any) => any;
export type UnaryReturnType<T extends UnaryFunction> = T extends Is<infer R> ? ReturnType<R>
: (T extends Not<infer S> ? ReturnType<S> : ReturnType<T>);
export type UnaryExtends<A extends UnaryFunction, B extends UnaryFunction> =
UnaryReturnType<A> extends UnaryReturnType<B> ? UnaryReturnType<A>
: UnaryReturnType<B>;
export interface Is<A extends UnaryFunction> {
<T>(value: T): value is Extract<T, ReturnType<A>>;
[SymbolGuardTest]: <T extends ReturnType<A>>(value: T) => T;
}
export interface Not<A extends UnaryFunction> {
<T>(value: T): value is T extends ReturnType<A> ? never : T;
[SymbolGuardTest]: <T>(value: T) => T extends ReturnType<A> ? never : T;
}
export interface Otherwise<T> {
value: T;
[SymbolOtherwise]: boolean;
}
export interface ToResult<I, O> {
<T>(value: T): T extends I ? (T extends O ? T : O) : (unknown extends T ? O : never);
}
export interface OrResult<I, O, Y> {
<T>(value: T):
| (T extends I ? (T extends O ? T : O) : (unknown extends T ? O : never))
| Exclude<Y, Error>;
}
export interface To {
(): <I>(value: I) => I;
<A extends UnaryFunction>(
a: A,
): ToResult<Parameters<A>[0], UnaryReturnType<A>>;
<A extends UnaryFunction, B extends (value: UnaryReturnType<A>) => any>(
a: A,