From: Justin Wind <justin.wind+git@gmail.com>
Date: Wed, 30 Mar 2022 16:07:47 +0000 (-0700)
Subject: dateFormat now handles bad dates a little better
X-Git-Tag: v1.1.1~1
X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=5690ad318ed98cff45bf1fd192656a7156c7045c;p=squeep-html-template-helper

dateFormat now handles bad dates a little better
---

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);