7
7
[ ![ Downloads] ( https://img.shields.io/npm/dm/%40jsonhero%2Fjson-infer-types.svg )] ( https://npmjs.com/@jsonhero/json-infer-types )
8
8
[ ![ Install size] ( https://packagephobia.com/badge?p=%40jsonhero%2Fjson-infer-types )] ( https://packagephobia.com/result?p=@jsonhero/json-infer-types )
9
9
10
+ - [ 🚀 Features] ( #-features )
11
+ - [ 💻 Usage] ( #-usage )
12
+ - [ Objects] ( #objects )
13
+ - [ Arrays] ( #arrays )
14
+ - [ Strings] ( #strings )
15
+ - [ String Formats] ( #string-formats )
16
+ - [ Date/Time strings] ( #datetime-strings )
17
+ - [ URI strings] ( #uri-strings )
18
+ - [ Email address strings] ( #email-address-strings )
19
+ - [ Other formats] ( #other-formats )
20
+
10
21
## 🚀 Features
11
22
12
23
- Written in typescript
13
24
- Infers types of values inside objects and arrays
25
+ - Lightweight with only a few third-party dependencies
14
26
- Includes a large set of formats for strings
15
27
- Dates and times (and timestamps)
28
+ - URIs
16
29
- Email addresses
17
30
- Currencies
18
31
- Countries
19
32
- Top-Level Domains
20
33
- IP Addresses
21
34
- Languages
22
35
- Phone Numbers
23
- - URIs
24
36
- UUIDs
25
37
- Hostnames
38
+ - File sizes
39
+ - Stringified JSON
26
40
27
41
## 💻 Usage
28
42
@@ -50,6 +64,8 @@ inferType(123.456); // => { name: "float" }
50
64
inferType (" hello world" ); // => { name: "string" }
51
65
```
52
66
67
+ ### Objects
68
+
53
69
Objects have an additional ` properties ` property that infers its value types
54
70
55
71
``` js
@@ -61,3 +77,260 @@ Will result in
61
77
``` json
62
78
{ "name" : " object" , "properties" : { "foo" : { "name" : " string" } } }
63
79
```
80
+
81
+ ### Arrays
82
+
83
+ Arrays have an additional ` items ` property that infers the types of its items
84
+
85
+ ``` js
86
+ inferType ([8 , 176 , 3 , 49 , 0 ]); // { name: "array", items: { name: "int" }}
87
+ ```
88
+
89
+ This works for an array of objects as well
90
+
91
+ ``` js
92
+ inferType ([
93
+ { id
: " 1" , email
: " [email protected] " },
94
+ { id
: " 2" , email
: " [email protected] " },
95
+ ]);
96
+ ```
97
+
98
+ Will result in
99
+
100
+ ``` json
101
+ {
102
+ "name" : " array" ,
103
+ "items" : {
104
+ "name" : " object" ,
105
+ "properties" : {
106
+ "id" : { "name" : " string" },
107
+ "email" : {
108
+ "name" : " string" ,
109
+ "format" : {
110
+ "name" : " email" ,
111
+ "variant" : " rfc5321"
112
+ }
113
+ }
114
+ }
115
+ }
116
+ }
117
+ ```
118
+
119
+ If they array has items of different types, ` items ` will be an array of objects representing each unique type found in the array
120
+
121
+ ``` js
122
+ inferType ([1 , " hello world" ]);
123
+ ```
124
+
125
+ Gives the result
126
+
127
+ ``` json
128
+ {
129
+ "name" : " array" ,
130
+ "items" : [
131
+ {
132
+ "name" : " int"
133
+ },
134
+ {
135
+ "name" : " string"
136
+ }
137
+ ]
138
+ }
139
+ ```
140
+
141
+ ### Strings
142
+
143
+ JSON Infer Types will also recognize certain string formats and include that information in the result, for example if the string is a ` URI ` :
144
+
145
+ ``` js
146
+ inferType (" https://www.example.com/foo#bar" );
147
+ ```
148
+
149
+ Will be
150
+
151
+ ``` json
152
+ {
153
+ "name" : " string" ,
154
+ "format" : {
155
+ "name" : " uri"
156
+ }
157
+ }
158
+ ```
159
+
160
+ Some formats have mutliple variants, like IP Address. ` inferType("192.168.0.1") ` will be interpreted as an IPV4 address
161
+
162
+ ``` json
163
+ {
164
+ "name" : " string" ,
165
+ "format" : {
166
+ "name" : " ip" ,
167
+ "variant" : " v4"
168
+ }
169
+ }
170
+ ```
171
+
172
+ And ` inferType("2001:db8:1234::1") ` will be interpreted as an IPV6 address
173
+
174
+ ``` json
175
+ {
176
+ "name" : " string" ,
177
+ "format" : {
178
+ "name" : " ip" ,
179
+ "variant" : " v6"
180
+ }
181
+ }
182
+ ```
183
+
184
+ ## String Formats
185
+
186
+ ### Date/Time strings
187
+
188
+ JSON Infer Types supports ` rfc3339/iso8601 ` and ` rfc2822 ` string formats
189
+
190
+ ``` js
191
+ inferType (" 2019-01-01 00:00:00.000Z" );
192
+ ```
193
+
194
+ Will result in
195
+
196
+ ``` json
197
+ {
198
+ "name" : " string" ,
199
+ "format" : {
200
+ "name" : " datetime" ,
201
+ "parts" : " datetime" ,
202
+ "variant" : " rfc3339"
203
+ }
204
+ }
205
+ ```
206
+
207
+ The ` parts ` field can be either ` datetime ` , ` date ` or ` time ` , depending on the contents of the string.
208
+
209
+ The following table illustrates the results of different Date/Time strings
210
+
211
+ | String | Variant | Parts |
212
+ | ----------------------------------- | ------- | -------- |
213
+ | ` "2019-01-01 00:00:00.000Z" ` | rfc3339 | datetime |
214
+ | ` "2019-10-12T14:20:50.52+07:00" ` | rfc3339 | datetime |
215
+ | ` "1983-10-14T13:30Z" ` | rfc3339 | datetime |
216
+ | ` "2016-05-25" ` | rfc3339 | date |
217
+ | ` "+002016-05-25" ` | rfc3339 | date |
218
+ | ` "2016-W21-3" ` | rfc3339 | date |
219
+ | ` "09:24:15.123Z" ` | rfc3339 | time |
220
+ | ` "09:24:15.123Z" ` | rfc3339 | time |
221
+ | ` "09:24:15" ` | rfc3339 | time |
222
+ | ` "Mon, 02 Jan 2017 06:00:00 -0800" ` | rfc2822 | datetime |
223
+ | ` "Mon, 02 Jan 2017 06:00:00 PST" ` | rfc2822 | datetime |
224
+
225
+ JSON Infer Types also supports unix epoch timestamps
226
+
227
+ ``` js
228
+ inferType (" 1596597629980" );
229
+ ```
230
+
231
+ Will result in
232
+
233
+ ``` json
234
+ {
235
+ "name" : " string" ,
236
+ "format" : {
237
+ "name" : " timestamp" ,
238
+ "variant" : " millisecondsSinceEpoch"
239
+ }
240
+ }
241
+ ```
242
+
243
+ Also supported are seconds and nanoseconds since epoch timestamp strings
244
+
245
+ ### URI strings
246
+
247
+ JSON Infer Types will interpret certain strings to be URIs
248
+
249
+ ``` js
250
+ inferType (" https://www.example.com/foo#bar" );
251
+ ```
252
+
253
+ Will result in
254
+
255
+ ``` json
256
+ {
257
+ "name" : " string" ,
258
+ "format" : {
259
+ "name" : " uri"
260
+ }
261
+ }
262
+ ```
263
+
264
+ If the URI contains a file extension, the inferred ` contentType ` will be included in the result. For example ` inferType("https://www.example.com/foo.json") ` will result in
265
+
266
+ ``` json
267
+ {
268
+ "name" : " string" ,
269
+ "format" : {
270
+ "name" : " uri" ,
271
+ "contentType" : " application/json"
272
+ }
273
+ }
274
+ ```
275
+
276
+ The mapping of file extension to contentType is done using the [ mime-types] ( https://github.com/jshttp/mime-types ) package
277
+
278
+ ### Email address strings
279
+
280
+ JSON Infer Types supports ` rfc5321 ` and ` rfc5321 ` style email address strings:
281
+
282
+ ``` js
283
+
284
+ ```
285
+
286
+ Will result in
287
+
288
+ ``` json
289
+ {
290
+ "name" : " string" ,
291
+ "format" : {
292
+ "name" : " email" ,
293
+ "variant" : " rfc5321"
294
+ }
295
+ }
296
+ ```
297
+
298
+ The following table illustrates the results of different email strings
299
+
300
+ | String | Variant |
301
+ | ------------------------------------------------ | ------- |
302
+ | ` "[email protected] " ` | rfc5321
|
303
+ | ` "[email protected] " ` | rfc5321
|
304
+ | ` "[email protected] " ` | rfc5321
|
305
+ | ` "Example Name <[email protected] >" ` | rfc5322
|
306
+ | ` "Example S. Name <[email protected] >" ` | rfc5322
|
307
+
308
+ ### Other formats
309
+
310
+ The following table illustrates the rest of the formats JSON Infer Types supports
311
+
312
+ | Example Strings | Name | Variant |
313
+ | ----------------------------------------- | ----------- | --------- |
314
+ | ` "USD" ` , ` "BTC" ` | currency | iso4217 |
315
+ | ` "United States dollar" ` , ` "Euro" ` | currency | english |
316
+ | ` "ETH" ` , ` "LTC" ` | currency | crypto |
317
+ | ` "USA" ` , ` "MMR" ` | country | iso3166-3 |
318
+ | ` "US" ` , ` "GB" ` , ` "JP" ` | country | iso3166-2 |
319
+ | ` ".com" ` , ` ".co.uk" ` , ` ".biz" ` | tld | |
320
+ | ` "192.168.0.1" ` , ` "172.16.0.0" ` , ` ".biz" ` | ip | v4 |
321
+ | ` "2001:db8:1234::1" ` | ip | v6 |
322
+ | ` "en" ` , ` "ab" ` , ` "es" ` | language | iso693-1 |
323
+ | ` "eng" ` , ` "eus" ` , ` "zul" ` | language | iso693-2 |
324
+ | ` "Arabic" ` , ` "Welsh" ` , ` "Russian" ` | language | english |
325
+ | ` "dansk" ` , ` "Español" ` | language | native |
326
+ | ` "+1 (684) 633-5115" ` , ` "+49 30 83050" ` | phoneNumber | e.164 |
327
+ | ` "4677658f-8865-47db-afb0-908e25246348" ` | uuid | v4 |
328
+ | ` "cfa649f0-650b-11ec-acb3-03462fc79f5d" ` | uuid | v1 |
329
+ | ` "bde4a7b9-5793-5a1f-b378-211205b15898" ` | uuid | v5 |
330
+ | ` "foo.example.com" ` , ` "localhost" ` | hostname | rfc1123 |
331
+ | ` "exa_mple.com" ` | hostname | rfc5890 |
332
+ | ` "544B" ` , ` "1.0MB" ` , ` "377K" ` , ` "1.87GB" ` | filesize | human |
333
+ | ` '{ "foo": 1 }' ` | json | ecma262 |
334
+ | ` '{ foo: 1, }' ` | json | json5 |
335
+
336
+ Please feel free to request additional formats by opening a [ Github issue] ( https://github.com/jsonhero-io/json-infer-types/issues )
0 commit comments