Skip to content

Commit 51fd395

Browse files
committed
Add option to turn off returning the value in the type response
1 parent df4820b commit 51fd395

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

src/@types.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,39 @@ import { JSONStringFormat, JSONObjectFormat } from "./formats";
22

33
export type JSONNullType = {
44
name: "null";
5-
value: null;
5+
value?: null;
66
};
77

88
export type JSONBoolType = {
99
name: "bool";
10-
value: boolean;
10+
value?: boolean;
1111
};
1212

1313
export type JSONFloatType = {
1414
name: "float";
15-
value: number;
15+
value?: number;
1616
};
1717

1818
export type JSONIntType = {
1919
name: "int";
20-
value: number;
20+
value?: number;
2121
};
2222

2323
export type JSONStringType = {
2424
name: "string";
2525
format?: JSONStringFormat;
26-
value: string;
26+
value?: string;
2727
};
2828

2929
export type JSONObjectType = {
3030
name: "object";
31-
value: object;
3231
format?: JSONObjectFormat;
32+
value?: object;
3333
};
3434

3535
export type JSONArrayType = {
3636
name: "array";
37-
value: Array<unknown>;
37+
value?: Array<unknown>;
3838
};
3939

4040
export type JSONValueType =

src/index.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,53 @@ import { inferFormat, inferObjectFormat } from "./formats";
44
export { JSONValueType };
55
export { JSONStringFormat } from "./formats";
66

7-
export function inferType(value: unknown): JSONValueType {
7+
export type InferOptions = {
8+
returnValue?: "yes" | "no";
9+
};
10+
11+
export function inferType(value: unknown, options?: InferOptions): JSONValueType {
12+
const opts: Required<InferOptions> = Object.assign({}, { returnValue: "yes" }, options);
13+
14+
const shouldReturnValue = opts.returnValue === "yes";
15+
816
if (value === null) {
9-
return { name: "null", value: null };
17+
return { name: "null", value: shouldReturnValue ? null : undefined };
1018
}
1119

1220
if (typeof value === "boolean") {
13-
return { name: "bool", value };
21+
return { name: "bool", value: shouldReturnValue ? value : undefined };
1422
}
1523

1624
if (typeof value === "number") {
1725
if (Number.isInteger(value)) {
18-
return { name: "int", value };
26+
return { name: "int", value: shouldReturnValue ? value : undefined };
1927
} else {
20-
return { name: "float", value };
28+
return { name: "float", value: shouldReturnValue ? value : undefined };
2129
}
2230
}
2331

2432
if (typeof value === "string") {
25-
return { name: "string", value, format: inferFormat(value) };
33+
return {
34+
name: "string",
35+
value: shouldReturnValue ? value : undefined,
36+
format: inferFormat(value),
37+
};
2638
}
2739

2840
if (typeof value === "object") {
2941
if (Array.isArray(value)) {
3042
return {
3143
name: "array",
32-
value: value,
44+
value: shouldReturnValue ? value : undefined,
3345
};
3446
}
3547

3648
return {
3749
name: "object",
38-
value,
3950
format: inferObjectFormat(value),
51+
value: shouldReturnValue ? value : undefined,
4052
};
4153
}
4254

43-
return { name: "null", value: null };
55+
return { name: "null", value: shouldReturnValue ? null : undefined };
4456
}

tests/basicTypes.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ test("It should infer basic types", () => {
1515
expect(inferType([{ foo: "bar" }])).toEqual({ name: "array", value: [{ foo: "bar" }] });
1616
});
1717

18+
test("it should allow an option to not include the values in the result", () => {
19+
expect(inferType("", { returnValue: "no" })).toEqual({ name: "string" });
20+
});
21+
1822
test("It should narrow the type of the value", () => {
1923
const unknownInfer = (value: unknown): number | undefined => {
2024
const result = inferType(value);

0 commit comments

Comments
 (0)