File tree 6 files changed +81
-6
lines changed
6 files changed +81
-6
lines changed Original file line number Diff line number Diff line change 1
- import { JSONStringFormat , JSONObjectFormat } from "./formats" ;
1
+ import { JSONStringFormat , JSONObjectFormat , JSONIntFormat } from "./formats" ;
2
2
3
3
export type JSONNullType = {
4
4
name : "null" ;
@@ -17,6 +17,7 @@ export type JSONFloatType = {
17
17
18
18
export type JSONIntType = {
19
19
name : "int" ;
20
+ format ?: JSONIntFormat ;
20
21
value : number ;
21
22
} ;
22
23
Original file line number Diff line number Diff line change @@ -119,3 +119,19 @@ export function inferObjectFormat(value: object): JSONObjectFormat | undefined {
119
119
120
120
return undefined ;
121
121
}
122
+
123
+ export type JSONIntFormat = JSONTimestampFormat
124
+
125
+ const intFormats = [ inferTimestamp ] ;
126
+
127
+ export function inferIntFormat ( value : number ) : JSONIntFormat | undefined {
128
+ for ( const [ , format ] of Object . entries ( intFormats ) ) {
129
+ const result = format ( value ) ;
130
+
131
+ if ( result ) {
132
+ return result ;
133
+ }
134
+ }
135
+
136
+ return undefined ;
137
+ }
Original file line number Diff line number Diff line change @@ -18,7 +18,10 @@ function inRangeOfNow(msSinceEpoch: number): boolean {
18
18
return now >= lowerBound && now <= upperBound ;
19
19
}
20
20
21
- export function inferTimestamp ( value : string ) : JSONTimestampFormat | undefined {
21
+ export function inferTimestamp ( value : string | number ) : JSONTimestampFormat | undefined {
22
+ if ( typeof value === "number" ) {
23
+ return inferTimestamp ( `${ value } ` ) ;
24
+ }
22
25
if ( timestampSecondsSinceEpoch . test ( value ) ) {
23
26
const seconds = parseInt ( value ) ;
24
27
Original file line number Diff line number Diff line change 1
1
import { JSONValueType } from "./@types" ;
2
- import { inferFormat , inferObjectFormat } from "./formats" ;
2
+ import { inferFormat , inferObjectFormat , inferIntFormat } from "./formats" ;
3
3
4
4
export { JSONValueType } ;
5
5
export {
@@ -45,7 +45,11 @@ export function inferType(value: unknown): JSONValueType {
45
45
46
46
if ( typeof value === "number" ) {
47
47
if ( Number . isInteger ( value ) ) {
48
- return { name : "int" , value } ;
48
+ return {
49
+ name : "int" ,
50
+ value,
51
+ format : inferIntFormat ( value ) ,
52
+ } ;
49
53
} else {
50
54
return { name : "float" , value } ;
51
55
}
Original file line number Diff line number Diff line change
1
+ import { inferType } from "../src" ;
2
+
3
+ describe ( "timestamps" , ( ) => {
4
+ test . each ( [ 1664976736123 , 1664976736567 ] ) (
5
+ "%p should be inferred as an timestamp" ,
6
+ ( value ) => {
7
+ expect ( inferType ( value ) ) . toEqual ( {
8
+ name : "int" ,
9
+ value,
10
+ format : {
11
+ name : "timestamp" ,
12
+ variant : "millisecondsSinceEpoch" ,
13
+ } ,
14
+ } ) ;
15
+ } ,
16
+ ) ;
17
+
18
+ test . each ( [ 1664976736 , 1664976736 ] ) ( "%p should be inferred as an timestamp" , ( value ) => {
19
+ expect ( inferType ( value ) ) . toEqual ( {
20
+ name : "int" ,
21
+ value,
22
+ format : {
23
+ name : "timestamp" ,
24
+ variant : "secondsSinceEpoch" ,
25
+ } ,
26
+ } ) ;
27
+ } ) ;
28
+
29
+ test . each ( [ 1664976736946364285 ] ) ( "%p should be inferred as an timestamp" , ( value ) => {
30
+ expect ( inferType ( value ) ) . toEqual ( {
31
+ name : "int" ,
32
+ value,
33
+ format : {
34
+ name : "timestamp" ,
35
+ variant : "nanosecondsSinceEpoch" ,
36
+ } ,
37
+ } ) ;
38
+ } ) ;
39
+ } ) ;
40
+
41
+ describe ( "without format" , ( ) => {
42
+ test . each ( [ 46 , 2244994945 , 1212092628029698048 ] ) (
43
+ "%p should be inferred as having no format" ,
44
+ ( value ) => {
45
+ expect ( inferType ( value ) ) . toEqual ( {
46
+ name : "int" ,
47
+ value,
48
+ } ) ;
49
+ } ,
50
+ ) ;
51
+ } ) ;
Original file line number Diff line number Diff line change @@ -162,7 +162,7 @@ describe("rfc2822", () => {
162
162
} ) ;
163
163
164
164
describe ( "timestamps" , ( ) => {
165
- test . each ( [ "1596597629980 " , "1640273437757" ] ) (
165
+ test . each ( [ "1664976736980 " , "1640273437757" ] ) (
166
166
"%p should be inferred as an timestamp" ,
167
167
( value ) => {
168
168
expect ( inferType ( value ) ) . toEqual ( {
@@ -187,7 +187,7 @@ describe("timestamps", () => {
187
187
} ) ;
188
188
} ) ;
189
189
190
- test . each ( [ "1596597839946364285 " ] ) ( "%p should be inferred as an timestamp" , ( value ) => {
190
+ test . each ( [ "1664976736946364285 " ] ) ( "%p should be inferred as an timestamp" , ( value ) => {
191
191
expect ( inferType ( value ) ) . toEqual ( {
192
192
name : "string" ,
193
193
value,
You can’t perform that action at this time.
0 commit comments