Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Calendar function handles formats only arg #3666

Merged
merged 5 commits into from Apr 25, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/lib/moment/calendar.js
@@ -1,3 +1,4 @@
import { isMoment } from './constructor';
import { createLocal } from '../create/local';
import { cloneWithOffset } from '../units/offset';
import isFunction from '../utils/is-function';
Expand All @@ -14,6 +15,12 @@ export function getCalendarFormat(myMoment, now) {
}

export function calendar (time, formats) {
// #3658 - Adding overload to the calendar function in order to allow:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can say in a comment above the if along the line of accept format only, but don't include the PR/issue number, there is git blame for that.

// calendar(FORMATS) a single parameter, formats only function call
if (arguments.length === 1 && typeof time === 'object' && !isMoment(time)) {
formats = arguments[0];
time = undefined;
}
// We want to compare the start of today, vs this.
// Getting start-of-today depends on whether we're local/utc/offset or not.
var now = time || createLocal(),
Expand Down
9 changes: 9 additions & 0 deletions src/test/moment/calendar.js
Expand Up @@ -55,3 +55,12 @@ test('extending calendar options', function (assert) {
moment.updateLocale('en', null);
}
});

test('calendar overload - passing one parameter formats', function (assert) {
var a = moment().hours(13).minutes(23).seconds(45);
assert.equal(moment(a).calendar({
'sameDay': function () {
return 'h:mm:ssA';
}
}), '1:23:45PM', 'should equate');
});