Skip to content

Commit 07503d7

Browse files
authored
Properly Convert all Timestamps from Srings to Date Types (#336)
* fix timestamp conversion bug
1 parent 7cb1fe8 commit 07503d7

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

src/utils.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@ export class Utils {
1414
collection: { [index: string]: unknown },
1515
typeInstance: T
1616
): 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 [];
2230
}
2331

2432
public static getHrMilliseconds(times: number[]): number {
@@ -46,16 +54,15 @@ export class Utils {
4654
return obj.hasOwnProperty(prop);
4755
}
4856

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+
});
5765
}
58-
return obj;
5966
}
6067

6168
public static hasAllPropertiesOf<T>(obj: unknown, refInstance: T): boolean {

0 commit comments

Comments
 (0)