Navigation Menu

Skip to content

Commit

Permalink
fix(webapp): return empty string when range doesn't make sense (#1419)
Browse files Browse the repository at this point in the history
  • Loading branch information
eh-am committed Aug 18, 2022
1 parent e2128b9 commit 6f69c6f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions webapp/javascript/util/formatDate.spec.ts
Expand Up @@ -33,6 +33,10 @@ describe('FormatDate', () => {
'1640090089000000000',
'2021-06-21 12:34 PM - 2021-12-21 12:34 PM',
],

// Return nothing when mixing absolute/relative
['1624278889000000000', 'now-1h', ''],
['now-1h', '1624278889000000000', ''],
];

test.each(cases)(
Expand Down
9 changes: 9 additions & 0 deletions webapp/javascript/util/formatDate.ts
Expand Up @@ -55,9 +55,18 @@ export function readableRange(

const d1 = getUTCdate(parseUnixTime(from), offsetInMinutes);
const d2 = getUTCdate(parseUnixTime(until), offsetInMinutes);

if (!isValidDate(d1) || !isValidDate(d2)) {
return '';
}

return `${format(d1, dateFormat)} - ${format(d2, dateFormat)}`;
}

function isValidDate(d: Date) {
return d instanceof Date && !isNaN(d.getTime());
}

/**
* formateAsOBject() returns a Date object
* based on the passed-in parameter value
Expand Down

0 comments on commit 6f69c6f

Please sign in to comment.