9
9
10
10
- [ 🚀 Features] ( #-features )
11
11
- [ 💻 Usage] ( #-usage )
12
- - [ Objects] ( #objects )
13
- - [ Arrays] ( #arrays )
14
12
- [ Strings] ( #strings )
15
13
- [ String Formats] ( #string-formats )
16
14
- [ Date/Time strings] ( #datetime-strings )
21
19
## 🚀 Features
22
20
23
21
- Written in typescript
24
- - Infers types of values inside objects and arrays
22
+ - Narrows type of the value when using with Typescript
25
23
- Lightweight with only a few third-party dependencies
26
24
- Includes a large set of formats for strings
27
25
- Dates and times (and timestamps)
@@ -51,103 +49,20 @@ $ npm install --save @jsonhero/json-infer-types
51
49
``` js
52
50
const { inferType } = require (" @jsonhero/json-infer-types" );
53
51
54
- inferType (123 ); // => { name: "int" }
52
+ inferType (123 ); // => { name: "int", value: 123 }
55
53
```
56
54
57
- The following basic types are supported:
55
+ The following types are supported:
58
56
59
57
``` js
60
- inferType (null ); // => { name: "null" }
61
- inferType (true ); // => { name: "bool" }
62
- inferType (123 ); // => { name: "int" }
63
- inferType (123.456 ); // => { name: "float" }
64
- inferType (" hello world" ); // => { name: "string" }
65
- ```
66
-
67
- ### Objects
68
-
69
- Objects have an additional ` properties ` property that infers its value types
70
-
71
- ``` js
72
- inferType ({ foo: " bar" });
73
- ```
74
-
75
- Will result in
76
-
77
- ``` json
78
- { "name" : " object" , "properties" : { "foo" : { "name" : " string" } } }
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
- If you don't want or need the ` properties ` or ` items ` inferred you can pass the ` shallow: true ` option to ` inferType `
142
-
143
- ``` js
144
- inferType (
145
- [
146
- { id
: " 1" , email
: " [email protected] " },
147
- { id
: " 2" , email
: " [email protected] " },
148
- ],
149
- { shallow: true }
150
- ); // => { name: "array" }
58
+ inferType (null ); // => { name: "null", value: null }
59
+ inferType (undefined ); // => { name: "null", value: null }
60
+ inferType (true ); // => { name: "bool", value: true }
61
+ inferType (123 ); // => { name: "int", value: 123 }
62
+ inferType (123.456 ); // => { name: "float", value: 123.456 }
63
+ inferType (" hello world" ); // => { name: "string", value: "hello world" }
64
+ inferType ({ foo: " bar" }); // => { name: "object", value: { foo: "bar" } }
65
+ inferType ([1 , 2 , 3 ]); // => { name: "array", value: [1, 2, 3] }
151
66
```
152
67
153
68
### Strings
@@ -163,6 +78,7 @@ Will be
163
78
``` json
164
79
{
165
80
"name" : " string" ,
81
+ "value" : " https://www.example.com/foo#bar" ,
166
82
"format" : {
167
83
"name" : " uri"
168
84
}
@@ -174,6 +90,7 @@ Some formats have mutliple variants, like IP Address. `inferType("192.168.0.1")`
174
90
``` json
175
91
{
176
92
"name" : " string" ,
93
+ "value" : " 192.168.0.1" ,
177
94
"format" : {
178
95
"name" : " ip" ,
179
96
"variant" : " v4"
@@ -186,6 +103,7 @@ And `inferType("2001:db8:1234::1")` will be interpreted as an IPV6 address
186
103
``` json
187
104
{
188
105
"name" : " string" ,
106
+ "value" : " 2001:db8:1234::1" ,
189
107
"format" : {
190
108
"name" : " ip" ,
191
109
"variant" : " v6"
@@ -208,6 +126,7 @@ Will result in
208
126
``` json
209
127
{
210
128
"name" : " string" ,
129
+ "value" : " 2019-01-01 00:00:00.000Z" ,
211
130
"format" : {
212
131
"name" : " datetime" ,
213
132
"parts" : " datetime" ,
@@ -245,6 +164,7 @@ Will result in
245
164
``` json
246
165
{
247
166
"name" : " string" ,
167
+ "value" : " 1596597629980" ,
248
168
"format" : {
249
169
"name" : " timestamp" ,
250
170
"variant" : " millisecondsSinceEpoch"
@@ -267,6 +187,7 @@ Will result in
267
187
``` json
268
188
{
269
189
"name" : " string" ,
190
+ "value" : " https://www.example.com/foo#bar" ,
270
191
"format" : {
271
192
"name" : " uri"
272
193
}
@@ -278,6 +199,7 @@ If the URI contains a file extension, the inferred `contentType` will be include
278
199
``` json
279
200
{
280
201
"name" : " string" ,
202
+ "value" : " https://www.example.com/foo.json" ,
281
203
"format" : {
282
204
"name" : " uri" ,
283
205
"contentType" : " application/json"
@@ -300,6 +222,7 @@ Will result in
300
222
``` json
301
223
{
302
224
"name" : " string" ,
225
+
303
226
"format" : {
304
227
"name" : " email" ,
305
228
"variant" : " rfc5321"
0 commit comments