From 5690ad318ed98cff45bf1fd192656a7156c7045c Mon Sep 17 00:00:00 2001 From: Justin Wind Date: Wed, 30 Mar 2022 09:07:47 -0700 Subject: [PATCH] dateFormat now handles bad dates a little better --- lib/template-helper.js | 5 ++++- test/lib/template-helper.js | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/template-helper.js b/lib/template-helper.js index 7e1693c..deb3a37 100644 --- a/lib/template-helper.js +++ b/lib/template-helper.js @@ -29,7 +29,9 @@ const dateOrNot = (date, otherwise) => { /** * Why is rendering a Date as a string this complicated? - * @param {Date|Number} date + * We handle the infinities because pg-promise might provide those in + * lieu of a Date object from timestamp fields. + * @param {Date|Number|String} date * @param {String=} pInf * @param {String=} nInf * @param {String=} otherwise @@ -43,6 +45,7 @@ const dateFormat = (date, pInf = 'Never', nInf = 'Forever', otherwise = '') => { return nInf; default: if (!date + || Number.isNaN(date.valueOf()) || (!(date instanceof Date) && !isDatableType)) { return otherwise; } diff --git a/test/lib/template-helper.js b/test/lib/template-helper.js index c0cd948..a21b141 100644 --- a/test/lib/template-helper.js +++ b/test/lib/template-helper.js @@ -56,6 +56,11 @@ describe('Template Helper', function () { const result = th.dateFormat(undefined, undefined, undefined, expected); assert.strictEqual(result, expected); }); + it('handles invalid date', function () { + const expected = 'otherwise'; + const result = th.dateFormat(new Date('bad date'), undefined, undefined, expected); + assert.strictEqual(result, expected); + }); it('renders Infinity', function () { const expected = 'end of time'; const result = th.dateFormat(Infinity, expected); -- 2.45.2