@@ -14,11 +14,19 @@ export class Utils {
14
14
collection : { [ index : string ] : unknown } ,
15
15
typeInstance : T
16
16
) : T [ ] {
17
- return collection && typeInstance
18
- ? ( Object . keys ( collection )
19
- . filter ( ( key : string ) => this . hasAllPropertiesOf ( collection [ key ] , typeInstance ) )
20
- . map ( ( key : string ) => this . parseTimestampsAsDates ( collection [ key ] ) ) as T [ ] )
21
- : [ ] ;
17
+ if ( collection && typeInstance ) {
18
+ const candidateObjects = Object . values ( collection ) . filter ( ( value ) =>
19
+ this . hasAllPropertiesOf ( value , typeInstance )
20
+ ) ;
21
+
22
+ // This recursive step ensures _all_ Timestamp properties are converted properly
23
+ // For example, a payload can contain the history as a property, so if we want to
24
+ // parse each HistoryEvent's Timestamp, we need to traverse the payload recursively
25
+ this . parseTimestampsAsDates ( candidateObjects ) ;
26
+
27
+ return candidateObjects as T [ ] ;
28
+ }
29
+ return [ ] ;
22
30
}
23
31
24
32
public static getHrMilliseconds ( times : number [ ] ) : number {
@@ -46,16 +54,15 @@ export class Utils {
46
54
return obj . hasOwnProperty ( prop ) ;
47
55
}
48
56
49
- public static parseTimestampsAsDates ( obj : unknown ) : unknown {
50
- if (
51
- typeof obj === "object" &&
52
- obj !== null &&
53
- this . hasOwnProperty ( obj , "Timestamp" ) &&
54
- typeof obj . Timestamp === "string"
55
- ) {
56
- obj . Timestamp = new Date ( obj . Timestamp ) ;
57
+ public static parseTimestampsAsDates ( obj : unknown ) : void {
58
+ if ( typeof obj === "object" && obj != null ) {
59
+ if ( this . hasOwnProperty ( obj , "Timestamp" ) && typeof obj . Timestamp === "string" ) {
60
+ obj . Timestamp = new Date ( obj . Timestamp ) ;
61
+ }
62
+ Object . values ( obj ) . map ( ( value ) => {
63
+ this . parseTimestampsAsDates ( value ) ;
64
+ } ) ;
57
65
}
58
- return obj ;
59
66
}
60
67
61
68
public static hasAllPropertiesOf < T > ( obj : unknown , refInstance : T ) : boolean {
0 commit comments