export class JsonTransformerUtil { /** * Since JSOn does not support dates natively, we'll recursively transform dates formatted like ISO * @param obj object to transform dates in * @example * transformDates({a: 'whatever'}); * //=>{a: 'whatever'} * @example * transformDates({someDate: '2020-11-06T10:12:05Z'}); * //=> {someDate: Fri Nov 06 2020 11:12:05 GMT+0100 (Central European Standard Time)} */ static transformDates(obj: any): any { if (obj == null) { return obj; } switch (typeof obj) { case 'object': Object.keys(obj).forEach(key => { obj[key] = JsonTransformerUtil.transformDates(obj[key]); }); return obj; case 'string': if (/^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z)?$/.test(obj)) { // If object is a string formatted like an ISO-8601 string, we'll turn it into a date return new Date(Date.parse(obj)); } return obj; default: return obj; } } }