-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.ts
124 lines (112 loc) · 3.24 KB
/
index.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
// This is the order the formats will be run in
import { inferDatetime, JSONDateTimeFormat } from "./datetime";
import { inferTimestamp, JSONTimestampFormat } from "./timestamp";
import { inferEmail, JSONEmailFormat } from "./email";
import { inferCurrency, JSONCurrencyFormat } from "./currency";
import { inferCountry, JSONCountryFormat } from "./country";
import { inferTld, JSONTLDFormat } from "./tld";
import { inferIpAddress, JSONIPAddressFormat } from "./ipAddress";
import { inferLanguage, JSONLanguageFormat } from "./language";
import { inferPhoneNumber, JSONPhoneNumberFormat } from "./phoneNumber";
import { inferUri, JSONURIFormat } from "./uri";
import { inferUuid, JSONUUIDFormat } from "./uuid";
import { inferFilesize, JSONFilesizeFormat } from "./filesize";
import { inferHostname, JSONHostnameFormat } from "./hostname";
import { inferJson, JSONJSONFormat } from "./json";
import { inferJsonPointer, JSONJSONPointerFormat } from "./jsonPointer";
import { inferEmoji, JSONEmojiFormat } from "./emoji";
import { inferSemver, JSONSemverFormat } from "./semver";
import { inferFirestoreTimestamp, JSONFirestoreTimestampFormat } from "./firestoreTimestamp";
import { inferJWT, JSONJWTStringFormat } from "./jwt";
import { inferColor, JSONColorFormat } from "./color";
import { inferCreditCard, JSONCreditCardFormat } from "./creditCard";
import { inferBase64, JSONBase64Format } from "./base64";
export {
JSONHostnameFormat,
JSONUUIDFormat,
JSONURIFormat,
JSONPhoneNumberFormat,
JSONLanguageFormat,
JSONIPAddressFormat,
JSONTLDFormat,
JSONCountryFormat,
JSONCurrencyFormat,
JSONEmailFormat,
JSONTimestampFormat,
JSONDateTimeFormat,
JSONFilesizeFormat,
JSONJSONFormat,
JSONJSONPointerFormat,
JSONEmojiFormat,
JSONSemverFormat,
JSONJWTStringFormat,
JSONColorFormat,
};
export type JSONStringFormat =
| JSONHostnameFormat
| JSONUUIDFormat
| JSONURIFormat
| JSONPhoneNumberFormat
| JSONLanguageFormat
| JSONIPAddressFormat
| JSONTLDFormat
| JSONCountryFormat
| JSONCurrencyFormat
| JSONEmailFormat
| JSONTimestampFormat
| JSONDateTimeFormat
| JSONFilesizeFormat
| JSONJSONFormat
| JSONJSONPointerFormat
| JSONEmojiFormat
| JSONSemverFormat
| JSONJWTStringFormat
| JSONColorFormat
| JSONCreditCardFormat
| JSONBase64Format;
const formats = [
inferUri,
inferTld,
inferHostname,
inferEmail,
inferDatetime,
inferIpAddress,
inferPhoneNumber,
inferCurrency,
inferCountry,
inferLanguage,
inferUuid,
inferFilesize,
inferTimestamp,
inferJson,
inferJsonPointer,
inferEmoji,
inferSemver,
inferJWT,
inferColor,
inferCreditCard,
inferBase64,
];
export function inferFormat(value: string): JSONStringFormat | undefined {
if (value.trim() === "") {
return undefined;
}
for (const [, format] of Object.entries(formats)) {
const result = format(value);
if (result) {
return result;
}
}
return undefined;
}
export type JSONObjectFormat = JSONFirestoreTimestampFormat;
const objectFormats = [inferFirestoreTimestamp];
export function inferObjectFormat(value: object): JSONObjectFormat | undefined {
for (const [, format] of Object.entries(objectFormats)) {
const result = format(value);
if (result) {
return result;
}
}
return undefined;
}