Skip to content

Commit 45e655d

Browse files
committed
Added shallow option to exclude array items and object properties from the results
1 parent 6f1adcc commit 45e655d

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,18 @@ Gives the result
138138
}
139139
```
140140

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" }
151+
```
152+
141153
### Strings
142154

143155
JSON Infer Types will also recognize certain string formats and include that information in the result, for example if the string is a `URI`:

src/@types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ export type JSONStringType = {
110110

111111
export type JSONObjectType = {
112112
name: "object";
113-
properties: Record<string, JSONValueType>;
113+
properties?: Record<string, JSONValueType>;
114114
};
115115

116116
export type JSONArrayType = {
117117
name: "array";
118-
items: JSONValueType | JSONValueType[];
118+
items?: JSONValueType | JSONValueType[];
119119
};
120120

121121
export type JSONValueType =

src/index.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@ import { JSONValueType, JSONStringFormat } from "./@types";
44

55
import * as formats from "./formats";
66

7-
export function inferType(value: any): JSONValueType {
7+
type InferTypeOptions = {
8+
shallow?: boolean;
9+
};
10+
11+
export function inferType(
12+
value: any,
13+
options?: InferTypeOptions
14+
): JSONValueType {
15+
const opts = Object.assign({}, { shallow: false }, options);
16+
817
if (value === null) {
918
return { name: "null" };
1019
}
@@ -27,7 +36,13 @@ export function inferType(value: any): JSONValueType {
2736

2837
if (typeof value === "object") {
2938
if (Array.isArray(value)) {
30-
const itemTypes = value.map(inferType);
39+
if (opts.shallow) {
40+
return {
41+
name: "array",
42+
};
43+
}
44+
45+
const itemTypes = value.map((item) => inferType(item, opts));
3146
const uniqTypes = uniqWith(itemTypes, isEqual);
3247

3348
if (uniqTypes.length === 1) {
@@ -43,6 +58,12 @@ export function inferType(value: any): JSONValueType {
4358
};
4459
}
4560

61+
if (opts.shallow) {
62+
return {
63+
name: "object",
64+
};
65+
}
66+
4667
return {
4768
name: "object",
4869
properties: Object.keys(value).reduce((acc, key) => {

tests/basicTypes.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,23 @@ it("Should handle string formats inside objects inside arrays", () => {
101101
},
102102
});
103103
});
104+
105+
it("should allow for shallow inference", () => {
106+
expect(
107+
inferType(
108+
{
109+
foo: [
110+
{
111+
ts: "2019-01-01T00:00:00.000Z",
112+
},
113+
{
114+
ts: "2019-10-12T14:20:50.52+07:00",
115+
},
116+
],
117+
},
118+
{ shallow: true }
119+
)
120+
).toEqual({
121+
name: "object",
122+
});
123+
});

0 commit comments

Comments
 (0)