@@ -4,41 +4,53 @@ import { inferFormat, inferObjectFormat } from "./formats";
44export { JSONValueType } ;
55export { 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}
0 commit comments