Skip to content

Commit 1a7971f

Browse files
committed
Removed the optional value feature
1 parent 612617f commit 1a7971f

File tree

3 files changed

+16
-28
lines changed

3 files changed

+16
-28
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";
3131
format?: JSONObjectFormat;
32-
value?: object;
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: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,27 @@ export {
3434
JSONArrayType,
3535
} from "./@types";
3636

37-
export type InferOptions = {
38-
returnValue?: "yes" | "no";
39-
};
40-
41-
export function inferType(value: unknown, options?: InferOptions): JSONValueType {
42-
const opts: Required<InferOptions> = Object.assign({}, { returnValue: "yes" }, options);
43-
44-
const shouldReturnValue = opts.returnValue === "yes";
45-
37+
export function inferType(value: unknown): JSONValueType {
4638
if (value === null) {
47-
return { name: "null", value: shouldReturnValue ? null : undefined };
39+
return { name: "null", value };
4840
}
4941

5042
if (typeof value === "boolean") {
51-
return { name: "bool", value: shouldReturnValue ? value : undefined };
43+
return { name: "bool", value };
5244
}
5345

5446
if (typeof value === "number") {
5547
if (Number.isInteger(value)) {
56-
return { name: "int", value: shouldReturnValue ? value : undefined };
48+
return { name: "int", value };
5749
} else {
58-
return { name: "float", value: shouldReturnValue ? value : undefined };
50+
return { name: "float", value };
5951
}
6052
}
6153

6254
if (typeof value === "string") {
6355
return {
6456
name: "string",
65-
value: shouldReturnValue ? value : undefined,
57+
value,
6658
format: inferFormat(value),
6759
};
6860
}
@@ -71,16 +63,16 @@ export function inferType(value: unknown, options?: InferOptions): JSONValueType
7163
if (Array.isArray(value)) {
7264
return {
7365
name: "array",
74-
value: shouldReturnValue ? value : undefined,
66+
value,
7567
};
7668
}
7769

7870
return {
7971
name: "object",
8072
format: inferObjectFormat(value),
81-
value: shouldReturnValue ? value : undefined,
73+
value,
8274
};
8375
}
8476

85-
return { name: "null", value: shouldReturnValue ? null : undefined };
77+
return { name: "null", value: null };
8678
}

tests/basicTypes.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ 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-
2218
test("It should narrow the type of the value", () => {
2319
const unknownInfer = (value: unknown): number | undefined => {
2420
const result = inferType(value);

0 commit comments

Comments
 (0)