]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
[bugfix] allow calendar with falsy input (#5647)
authorJakka Prihatna <jprihatna@gmail.com>
Tue, 15 Sep 2020 02:23:02 +0000 (09:23 +0700)
committerGitHub <noreply@github.com>
Tue, 15 Sep 2020 02:23:02 +0000 (19:23 -0700)
src/lib/moment/calendar.js
src/test/moment/calendar.js

index 5d2998fac1c9a78bd191f25396150cbfc291fd83..fdd53dcb34a466a74641bae97a0c3a93f0e56928 100644 (file)
@@ -25,7 +25,10 @@ export function getCalendarFormat(myMoment, now) {
 export function calendar(time, formats) {
     // Support for single parameter, formats only overload to the calendar function
     if (arguments.length === 1) {
-        if (isMomentInput(arguments[0])) {
+        if (!arguments[0]) {
+            time = undefined;
+            formats = undefined;
+        } else if (isMomentInput(arguments[0])) {
             time = arguments[0];
             formats = undefined;
         } else if (isCalendarSpec(arguments[0])) {
index 47ee29d7dfe85e8f9b6c005eab49d2e4bf3aaf39..b1e4728793ae089ee5cd81cd3903c214139cf130 100644 (file)
@@ -184,3 +184,31 @@ test('calendar overload format - passing one parameter - object w/ sameDay as fu
         'should equate'
     );
 });
+
+test('defaulting to current date', function (assert) {
+    var a = moment().hours(13).minutes(23).seconds(45);
+    assert.equal(moment(a).calendar(), 'Today at 1:23 PM', 'should equate');
+});
+
+test('calendar overload time - passing one parameter - a falsy value', function (assert) {
+    var a = moment().hours(13).minutes(23).seconds(45),
+        tests = [
+            '',
+            0,
+            -0,
+            // 0n,
+            false,
+            NaN,
+            null,
+            undefined,
+        ],
+        i;
+
+    for (i = 0; i < tests.length; ++i) {
+        assert.equal(
+            moment(a).calendar(tests[i]),
+            'Today at 1:23 PM',
+            'should equate'
+        );
+    }
+});