From 503219eb470ce0339a53572fb6e5f920591f77d7 Mon Sep 17 00:00:00 2001 From: Jakka Prihatna Date: Tue, 15 Sep 2020 09:23:02 +0700 Subject: [PATCH] [bugfix] allow calendar with falsy input (#5647) --- src/lib/moment/calendar.js | 5 ++++- src/test/moment/calendar.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/lib/moment/calendar.js b/src/lib/moment/calendar.js index 5d2998fac..fdd53dcb3 100644 --- a/src/lib/moment/calendar.js +++ b/src/lib/moment/calendar.js @@ -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])) { diff --git a/src/test/moment/calendar.js b/src/test/moment/calendar.js index 47ee29d7d..b1e472879 100644 --- a/src/test/moment/calendar.js +++ b/src/test/moment/calendar.js @@ -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' + ); + } +}); -- 2.47.2