From 8f4662bd0bb49a2fac343dea07f679fcb02417ad Mon Sep 17 00:00:00 2001 From: chris48s Date: Sat, 8 Oct 2022 11:43:29 +0100 Subject: [PATCH] fix formatRelativeDate error handling; run [date] (#8497) Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com> --- services/text-formatters.js | 8 +++++--- services/text-formatters.spec.js | 6 ++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/services/text-formatters.js b/services/text-formatters.js index 519ddc13ac..ac3f5382c5 100644 --- a/services/text-formatters.js +++ b/services/text-formatters.js @@ -124,9 +124,11 @@ function formatDate(d) { } function formatRelativeDate(timestamp) { - return dayjs() - .to(dayjs.unix(parseInt(timestamp, 10))) - .toLowerCase() + const parsedDate = dayjs.unix(parseInt(timestamp, 10)) + if (!parsedDate.isValid()) { + return 'invalid date' + } + return dayjs().to(parsedDate).toLowerCase() } export { diff --git a/services/text-formatters.spec.js b/services/text-formatters.spec.js index 13fc841e41..ba8788e1f6 100644 --- a/services/text-formatters.spec.js +++ b/services/text-formatters.spec.js @@ -153,5 +153,11 @@ describe('Text formatters', function () { .describe('when given the beginning of october') .expect('a month ago') }) + + test(formatRelativeDate, () => { + given(9999999999999) + .describe('when given invalid date') + .expect('invalid date') + }) }) })