From c981eee13394c7b9718fc36ecd631c2d2de89620 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Mon, 25 Apr 2022 19:31:38 +0530 Subject: [PATCH] added better null checks --- src/utils/time/index.ts | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/utils/time/index.ts b/src/utils/time/index.ts index 2783f2201..1b0e0ea44 100644 --- a/src/utils/time/index.ts +++ b/src/utils/time/index.ts @@ -97,21 +97,12 @@ export function parseDateFromFusedDateString(dateTime: string) { which we would extract and use to form the date */ export function tryToParseDateTime(dateTime: string): Date { - let dateComponent: DateComponent = { - year: null, - month: null, - day: null, - hour: null, - minute: null, - second: null, - }; - - dateComponent = getDateComponentsFromSymbolJoinedString(dateTime); + const dateComponent = getDateComponentsFromSymbolJoinedString(dateTime); if (isDateComponentValid(dateComponent)) { return getDateFromComponents(dateComponent); } else if ( - dateComponent.year.length === 8 && - dateComponent.month.length === 6 + dateComponent.year?.length === 8 && + dateComponent.month?.length === 6 ) { // the filename has size 8 consecutive and then 6 consecutive digits // high possibility that the it is some unhandled date time encoding @@ -125,7 +116,8 @@ export function tryToParseDateTime(dateTime: string): Date { function getDateComponentsFromSymbolJoinedString( dateTime: string ): DateComponent { - const [year, month, day, hour, minute, second] = dateTime.match(/\d+/g); + const [year, month, day, hour, minute, second] = + dateTime.match(/\d+/g) ?? []; return { year, month, day, hour, minute, second }; } @@ -133,9 +125,9 @@ function getDateComponentsFromSymbolJoinedString( // has length number of digits in the components function isDateComponentValid(dateComponent: DateComponent) { return ( - dateComponent.year.length === 4 && - dateComponent.month.length === 2 && - dateComponent.day.length === 2 + dateComponent.year?.length === 4 && + dateComponent.month?.length === 2 && + dateComponent.day?.length === 2 ); }