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

make moment calendar extensible with ad-hoc options #3175

Closed
wants to merge 1 commit into
base: develop
from

Conversation

Projects
None yet
3 participants
@maggiepint
Member

maggiepint commented May 11, 2016

There have been numerous requests to be able to add more options to moment().calendar(). Going through and adding this to the locale files in the way that #2996 implements it causes breaking changes for people using updateLocale, and is on the whole not the cleanest thing. What if you don't want those other options? Now you have them and you're stuck with them.

Instead, I added a function called calendarFormat that can be used to define what calendar option should be used. This function defaults to what already exists, but can be overridden. For instance, the following function adds this month and next month options:

    moment.calendarFormat = function (myMoment, now) {
        var diff = myMoment.diff(now, 'days', true);
        var nextMonth = now.clone().add(1, 'month');

        var retVal =  diff < -6 ? 'sameElse' :
            diff < -1 ? 'lastWeek' :
            diff < 0 ? 'lastDay' :
            diff < 1 ? 'sameDay' :
            diff < 2 ? 'nextDay' :
            diff < 7 ? 'nextWeek' :
            (myMoment.month() === now.month() && myMoment.year() === now.year()) ? 'thisMonth' :
            (nextMonth.month() === myMoment.month() && nextMonth.year() === myMoment.year()) ? 'nextMonth' : 'sameElse';
        return retVal;
    };

Then, use update locale to add the corresponding values to the locale:

    moment.updateLocale('en', {
        calendar : {
                sameDay : '[Today at] LT',
                nextDay : '[Tomorrow at] LT',
                nextWeek : 'dddd [at] LT',
                lastDay : '[Yesterday at] LT',
                lastWeek : '[Last] dddd [at] LT',
                thisMonth : '[This month on the] Do',
                nextMonth : '[Next month on the] Do',
                sameElse : 'L'
            }
    });

If the locale doesn't have the values specified, it will fall back to 'sameElse'.

I think this is a cleaner way to go about this task.

It may need more unit tests but I'm tired.

@maggiepint

This comment has been minimized.

Member

maggiepint commented May 11, 2016

The way I implemented the test custom function is wrong. Should really be checking year and month number, not diff. Will fix later. Doesn't affect actual code.

@maggiepint

This comment has been minimized.

Member

maggiepint commented May 14, 2016

Fixed my wrong code in the tests.

@ichernev

This comment has been minimized.

Contributor

ichernev commented Jun 7, 2016

@maggiepint looks very good, and I like the idea. Will release in next version.

@ichernev

This comment has been minimized.

Contributor

ichernev commented Jun 14, 2016

Merged in e3439fa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment