99
1010- [ 🚀 Features] ( #-features )
1111- [ 💻 Usage] ( #-usage )
12- - [ Objects] ( #objects )
13- - [ Arrays] ( #arrays )
1412 - [ Strings] ( #strings )
1513- [ String Formats] ( #string-formats )
1614 - [ Date/Time strings] ( #datetime-strings )
2119## 🚀 Features
2220
2321- Written in typescript
24- - Infers types of values inside objects and arrays
22+ - Narrows type of the value when using with Typescript
2523- Lightweight with only a few third-party dependencies
2624- Includes a large set of formats for strings
2725 - Dates and times (and timestamps)
@@ -51,103 +49,20 @@ $ npm install --save @jsonhero/json-infer-types
5149``` js
5250const { inferType } = require (" @jsonhero/json-infer-types" );
5351
54- inferType (123 ); // => { name: "int" }
52+ inferType (123 ); // => { name: "int", value: 123 }
5553```
5654
57- The following basic types are supported:
55+ The following types are supported:
5856
5957``` 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: " eric@example.com" },
94- { id: " 2" , email: " matt@example.com" },
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: " eric@example.com" },
147- { id: " 2" , email: " matt@example.com" },
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] }
15166```
15267
15368### Strings
@@ -163,6 +78,7 @@ Will be
16378``` json
16479{
16580 "name" : " string" ,
81+ "value" : " https://www.example.com/foo#bar" ,
16682 "format" : {
16783 "name" : " uri"
16884 }
@@ -174,6 +90,7 @@ Some formats have mutliple variants, like IP Address. `inferType("192.168.0.1")`
17490``` json
17591{
17692 "name" : " string" ,
93+ "value" : " 192.168.0.1" ,
17794 "format" : {
17895 "name" : " ip" ,
17996 "variant" : " v4"
@@ -186,6 +103,7 @@ And `inferType("2001:db8:1234::1")` will be interpreted as an IPV6 address
186103``` json
187104{
188105 "name" : " string" ,
106+ "value" : " 2001:db8:1234::1" ,
189107 "format" : {
190108 "name" : " ip" ,
191109 "variant" : " v6"
@@ -208,6 +126,7 @@ Will result in
208126``` json
209127{
210128 "name" : " string" ,
129+ "value" : " 2019-01-01 00:00:00.000Z" ,
211130 "format" : {
212131 "name" : " datetime" ,
213132 "parts" : " datetime" ,
@@ -245,6 +164,7 @@ Will result in
245164``` json
246165{
247166 "name" : " string" ,
167+ "value" : " 1596597629980" ,
248168 "format" : {
249169 "name" : " timestamp" ,
250170 "variant" : " millisecondsSinceEpoch"
@@ -267,6 +187,7 @@ Will result in
267187``` json
268188{
269189 "name" : " string" ,
190+ "value" : " https://www.example.com/foo#bar" ,
270191 "format" : {
271192 "name" : " uri"
272193 }
@@ -278,6 +199,7 @@ If the URI contains a file extension, the inferred `contentType` will be include
278199``` json
279200{
280201 "name" : " string" ,
202+ "value" : " https://www.example.com/foo.json" ,
281203 "format" : {
282204 "name" : " uri" ,
283205 "contentType" : " application/json"
@@ -300,6 +222,7 @@ Will result in
300222``` json
301223{
302224 "name" : " string" ,
225+ "value" : " eallam@example.com" ,
303226 "format" : {
304227 "name" : " email" ,
305228 "variant" : " rfc5321"
0 commit comments