-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstringFormats.test.ts
885 lines (825 loc) · 20.7 KB
/
stringFormats.test.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
import { inferType } from "../src";
describe("rfc3339", () => {
test.each([
"2019-01-01T00:00:00.000Z",
"2019-01-01 00:00:00.000Z",
"2019-10-12T14:20:50.52+07:00",
"2019-10-12 14:20:50.52-12:00",
"2016-05-25T09:08:34.123",
"1983-10-14T13:30Z",
"2016-05-25T09",
"2016-05-25T09:24",
"2016-05-25T09:24:15",
"2016-05-25T09:24:15,123",
"2016-05-25T09:24:15.1239999",
"2016-W21-3T09:24:15.123Z", // Accepts ISO week date
"2016-200T09:24:15.123", // Accepts ordinal date
])("%p should be inferred as an rfc3339 datetime", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "datetime",
parts: "datetime",
variant: "rfc3339",
},
});
});
test.each([
"2007-12-03T10:15:30+01:00[Europe/Paris]",
"2016-05-25T09:08:34.123[America/North_Dakota/Beulah]",
"2016-W21-3T09:24:15.123Z[America/Phoenix]",
"1983-10-14T13:30Z[Europe/Isle_of_Man]",
"2022-02-28T11:06:00.092121729+08:00[Asia/Shanghai]",
])("%p should be inferred as an rfc3339 datetime with timezone extension", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "datetime",
parts: "datetime",
variant: "rfc3339",
extensions: ["timezone"],
},
});
});
test.each([
"2007-12-03T10:15:30+01:00[Europe/Paris][u-ca=hebrew]",
"2016-05-25T09:08:34.123[America/North_Dakota/Beulah][u-ca=iso8601]",
"2016-W21-3T09:24:15.123Z[America/Phoenix][u-ca=buddhist]",
"1983-10-14T13:30Z[Europe/Isle_of_Man][u-ca=japanese]",
])(
"%p should be inferred as an rfc3339 datetime with timezone and calendar extension",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "datetime",
parts: "datetime",
variant: "rfc3339",
extensions: ["timezone", "calendar"],
},
});
},
);
test.each(["2016-05", "2016-05-25", "+002016-05-25", "2016-W21", "2016-W21-3", "2016-200"])(
"%p should be inferred as an rfc3339 date",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "datetime",
parts: "date",
variant: "rfc3339",
},
});
},
);
test.each([
"2016-05[Europe/Paris][u-ca=hebrew]",
"2016-05-25[Europe/Isle_of_Man][u-ca=japanese]",
"+002016-05-25[America/Phoenix][u-ca=buddhist]",
"2016-W21[America/Phoenix][u-ca=buddhist]",
"2016-W21-3[America/Phoenix][u-ca=buddhist]",
"2016-200[America/Phoenix][u-ca=buddhist]",
])("%p should be inferred as an rfc3339 date with timezone and calendar extensions", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "datetime",
parts: "date",
variant: "rfc3339",
extensions: ["timezone", "calendar"],
},
});
});
test.each(["09:24:15.123Z", "09:24:15", "09:24"])(
"%p should be inferred as an rfc3339 time",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "datetime",
parts: "time",
variant: "rfc3339",
},
});
},
);
test.each([
"09:24:15.123Z[Europe/Isle_of_Man][u-ca=japanese]",
"09:24:15[America/Phoenix][u-ca=buddhist]",
"09:24[Europe/Paris][u-ca=hebrew]",
])("%p should be inferred as an rfc3339 time with timezone and calendar extensions", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "datetime",
parts: "time",
variant: "rfc3339",
extensions: ["timezone", "calendar"],
},
});
});
});
describe("rfc2822", () => {
test.each([
"Tue, 01 Nov 2016 13:23:12 +0630",
"Sun, 12 Apr 2015 05:06:07 GMT",
"Tue, 01 Nov 2016 01:23:45 +0000",
"Tue, 01 Nov 16 04:23:45 Z",
"01 Nov 2016 05:23:45 z",
"Mon, 02 Jan 2017 06:00:00 -0800",
"Mon, 02 Jan 2017 06:00:00 +0800",
"Mon, 02 Jan 2017 06:00:00 +0330",
"Mon, 02 Jan 2017 06:00:00 -0330",
"Mon, 02 Jan 2017 06:00:00 PST",
"Mon, 02 Jan 2017 06:00:00 PDT",
])("%p should be inferred as an rfc2822 datetime", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "datetime",
parts: "datetime",
variant: "rfc2822",
},
});
});
});
describe("timestamps", () => {
test.each(["1596597629980", "1640273437757"])(
"%p should be inferred as an timestamp",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "timestamp",
variant: "millisecondsSinceEpoch",
},
});
},
);
test.each(["1640273389", "1608737603"])("%p should be inferred as an timestamp", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "timestamp",
variant: "secondsSinceEpoch",
},
});
});
test.each(["1596597839946364285"])("%p should be inferred as an timestamp", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "timestamp",
variant: "nanosecondsSinceEpoch",
},
});
});
});
describe("email addresses", () => {
test.each([
])("%p should be inferred as an email", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "email",
variant: "rfc5321",
},
});
});
test.each([
"Example Name <[email protected]>",
"John <[email protected]>",
"Example S. Name <[email protected]>",
])("%p should be inferred as an email", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "email",
variant: "rfc5322",
},
});
});
});
describe("currencies", () => {
test.each(["USD", "XPF", "BTC", "usd", "eur", "btc"])(
"%p should be inferred as a currency code",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "currency",
variant: "iso4217",
},
});
},
);
test.each(["LTC", "ETH", "XRP"])("%p should be inferred as a crypto currency", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "currency",
variant: "crypto",
},
});
});
test.each(["Euro", "United States dollar", "US Dollar"])(
"%p should be inferred as a currency in english",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "currency",
variant: "english",
},
});
},
);
test.each(["$", "£", "€", "¥"])("%p should be inferred as a currency symbol", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "currency",
variant: "symbol",
},
});
});
});
describe("country", () => {
test.each(["USA", "UZB", "SGP", "NFK", "MMR", "KEN", "FJI"])(
"%p should be inferred as a iso3166-3 country code",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "country",
variant: "iso3166-3",
},
});
},
);
test.each(["US", "GB", "JP", "NZ", "SV", "DE", "IE"])(
"%p should be inferred as a iso3166-2 country code",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "country",
variant: "iso3166-2",
},
});
},
);
});
describe("tlds", () => {
test.each([
".com",
".co.uk",
".biz",
".com.mx",
".org.au",
".cf",
".nz.basketball",
".accountants",
".immobilien",
])("%p should be inferred as a tld", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "tld",
},
});
});
});
describe("ip address", () => {
test.each(["192.168.0.1", "172.16.0.0"])("%p should be inferred as an ipv4 address", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "ip",
variant: "v4",
},
});
});
test.each(["2001:db8:1234::1", "2001:0:ce49:7601:e866:efff:62c3:fffe"])(
"%p should be inferred as an ipv6 address",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "ip",
variant: "v6",
},
});
},
);
});
describe("language", () => {
test.each(["en", "ab", "aa", "es"])(
"%p should be inferred as a language iso693-1 code",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "language",
variant: "iso693-1",
},
});
},
);
test.each(["eng", "eus", "ind", "zul"])(
"%p should be inferred as a language iso693-2 code",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "language",
variant: "iso693-2",
},
});
},
);
test.each(["Arabic", "Welsh", "Russian", "Swahili"])(
"%p should be inferred as a language in english",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "language",
variant: "english",
},
});
},
);
test.each(["eesti, eesti keel", "Afaan Oromoo", "dansk", "Español"])(
"%p should be inferred as a native language",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "language",
variant: "native",
},
});
},
);
});
describe("phone numbers", () => {
test.each([
"+1 684-633-5115",
"+49 30 83050",
"+355 4 224 7285",
"+1 323-867-5309",
"+1 (684) 633-5115",
])("%p should be inferred as an internation phone number", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "phoneNumber",
variant: "e.164",
},
});
});
});
describe("uris", () => {
test.each([
"https://www.example.com/foo#bar",
"file://host/path",
"https://goole.com?hello=world",
"ipfs://bafybeihzwwtl65z6ftqf5avmg3iv45umxm4hyz6wty4l4pd5eplnz256xa",
])("%p should be inferred as a rfc3986 URI", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "uri",
},
});
});
test("It should correctly infer the contentType of the uri", () => {
expect(inferType("https://www.example.com/foo.json")).toEqual({
name: "string",
value: "https://www.example.com/foo.json",
format: {
name: "uri",
contentType: "application/json",
},
});
expect(inferType("https://www.example.com/foo.md")).toEqual({
name: "string",
value: "https://www.example.com/foo.md",
format: {
name: "uri",
contentType: "text/markdown",
},
});
expect(inferType("https://www.example.com/foo.jpg")).toEqual({
name: "string",
value: "https://www.example.com/foo.jpg",
format: {
name: "uri",
contentType: "image/jpeg",
},
});
expect(inferType("https://www.example.com/foo.png")).toEqual({
name: "string",
value: "https://www.example.com/foo.png",
format: {
name: "uri",
contentType: "image/png",
},
});
expect(
inferType("ipfs://bafybeihzwwtl65z6ftqf5avmg3iv45umxm4hyz6wty4l4pd5eplnz256xa/500.png"),
).toEqual({
name: "string",
value: "ipfs://bafybeihzwwtl65z6ftqf5avmg3iv45umxm4hyz6wty4l4pd5eplnz256xa/500.png",
format: {
name: "uri",
contentType: "image/png",
},
});
});
});
describe("uuids", () => {
test.each(["4677658f-8865-47db-afb0-908e25246348"])(
"%p should be inferred as a v4 uuid",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "uuid",
variant: "v4",
},
});
},
);
test.each(["cfa649f0-650b-11ec-acb3-03462fc79f5d"])(
"%p should be inferred as a v1 uuid",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "uuid",
variant: "v1",
},
});
},
);
test.each(["bde4a7b9-5793-5a1f-b378-211205b15898"])(
"%p should be inferred as a v5 uuid",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "uuid",
variant: "v5",
},
});
},
);
});
describe("hostnames", () => {
test.each(["localhost", "example.com", "foo.example.com", "exa-mple.co.uk", "example.com."])(
"%p should be inferred as a rfc1123 hostname",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "hostname",
variant: "rfc1123",
},
});
},
);
test.each(["exa_mple.com"])("%p should be inferred as a rfc5890 hostname", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "hostname",
variant: "rfc5890",
},
});
});
test.each([`${"example".repeat(36)}.com`, "Screen_Shot_2021-08-16_at_11_12_27_AM.png"])(
"%p should NOT be inferred as a hostname",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
});
},
);
});
describe("file sizes", () => {
test.each(["544B", "1.0MB", "377K", "1.87GB", "8MiB", "1 KB", "25kB"])(
"%p should be inferred as a file size",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "filesize",
variant: "human",
},
});
},
);
});
describe("json", () => {
test.each(['{ "foo": 1 }', '{"name":"string","format":{"name":"filesize","variant":"human"}}'])(
"%p should be inferred as a json string",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "json",
variant: "ecma262",
},
});
},
);
test.each(["{ foo: 1, }", `{name:'string',format:{name: 0xdecaf, variant:.8675309}}`])(
"%p should be inferred as a json string",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "json",
variant: "json5",
},
});
},
);
});
describe("jsonPointer", () => {
test.each(["/foo/bar~0/baz~1/%a", "/foo//bar", "/foo/bar/", "/foo/0", "/foo/-/bar"])(
"%p should be inferred as an absolute jsonPointer",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "jsonPointer",
variant: "rfc6901",
},
});
},
);
});
describe("emoji", () => {
test.each(["😄", "🤪"])("%p should be inferred as an emoji", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "emoji",
},
});
});
test.each(["Hello 😄", "👩👩👧👧 Lots of people"])("%p should NOT be inferred as an emoji", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
});
});
});
describe("semver", () => {
test.each(["1.2.3", "1.11.0", "0.0.1", "1.0.0-alpha.1", "1.0.0+20130313144700"])(
"%p should be inferred as a semver",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "semver",
},
});
},
);
});
describe("jwt", () => {
test.each([
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.sruoLZNJ59anK67z25t80L62OXDerSiAhWerW-usZLQ",
])("%p should be inferred as a JWT", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "jwt",
},
});
});
});
describe("colors", () => {
test.each(["#D47DB9", "#14C3FF", "#ff9933", "#fff"])(
"%p should be inferred as a color hex",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "color",
variant: "hex",
},
});
},
);
test.each([
"rgb(255, 255, 255)",
"rgb(255, 255, 255,.5)",
"rgba(255, 255, 255,.5)",
"rgb(255 255 255)",
"rgb(255 255 255 / .5)",
])("%p should be inferred as a color rgb", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "color",
variant: "rgb",
},
});
});
test.each([
"hsl(100, 100%, 50%)",
"hsl(235, 100%, 50%, .5)",
"hsl(235 100% 50%)",
"hsl(235 100% 50% / .5)",
])("%p should be inferred as a color rgb", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "color",
variant: "hsl",
},
});
});
});
describe("credit cards", () => {
test.each([
"4916986744094249",
"4556355098906363",
"4929712410821052",
"4485428259658366",
"4485 4282 5965 8366",
])("%p should be inferred as a visa card", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "creditcard",
variant: "visa",
},
});
});
test.each(["375092442988287", "346135683645540", "343285261458387", "371163810364163"])(
"%p should be inferred as an amex card",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "creditcard",
variant: "amex",
},
});
},
);
test.each(["6011150635208157", "6011640556085105", "6011938437037885", "6011145666460750"])(
"%p should be inferred as a discover card",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "creditcard",
variant: "discover",
},
});
},
);
test.each(["5291160983813402", "5277689457510316", "5308066989503486", "5573850948088160"])(
"%p should be inferred as a mastercard card",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "creditcard",
variant: "mastercard",
},
});
},
);
test.each(["38223928053796", "30054894306803", "36059360259778", "30348465263843"])(
"%p should be inferred as a Diners Club card",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "creditcard",
variant: "dinersclub",
},
});
},
);
});
describe("mac", () => {
test.each([
"38:EE:4A:47:3A:36",
"ad:af:bd:71:12:87",
])("%p should be infered as being a MAC address", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "mac",
variant: "EUI-48",
splitter: ":"
},
});
});
test.each([
"38-EE-4A-47-3A-36",
"ad-af-bd-71-12-87",
])("%p should be infered as being a MAC address", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "mac",
variant: "EUI-48",
splitter: "-"
},
});
});
test.each([
"38.EE.4A.47.3A.36",
"ad.af.bd.71.12.87",
])("%p should be infered as being a MAC address", (value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
format: {
name: "mac",
variant: "EUI-48",
splitter: "."
},
});
});
});
describe("without format", () => {
test.each(["46", "2244994945", "1212092628029698048"])(
"%p should be inferred as having no format",
(value) => {
expect(inferType(value)).toEqual({
name: "string",
value,
});
},
);
});