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

[bugfix] Treat periods as periods, not regex-anything period, for weekday parsing in strict mode. #4453

Merged
merged 4 commits into from Apr 15, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions src/lib/units/day-of-week.js
Expand Up @@ -200,9 +200,9 @@ export function localeWeekdaysParse (weekdayName, format, strict) {

mom = createUTC([2000, 1]).day(i);
if (strict && !this._fullWeekdaysParse[i]) {
this._fullWeekdaysParse[i] = new RegExp('^' + this.weekdays(mom, '').replace('.', '\.?') + '$', 'i');
this._shortWeekdaysParse[i] = new RegExp('^' + this.weekdaysShort(mom, '').replace('.', '\.?') + '$', 'i');
this._minWeekdaysParse[i] = new RegExp('^' + this.weekdaysMin(mom, '').replace('.', '\.?') + '$', 'i');
this._fullWeekdaysParse[i] = new RegExp('^' + this.weekdays(mom, '').replace('.', '\\.?') + '$', 'i');
this._shortWeekdaysParse[i] = new RegExp('^' + this.weekdaysShort(mom, '').replace('.', '\\.?') + '$', 'i');
this._minWeekdaysParse[i] = new RegExp('^' + this.weekdaysMin(mom, '').replace('.', '\\.?') + '$', 'i');
}
if (!this._weekdaysParse[i]) {
regex = '^' + this.weekdays(mom, '') + '|^' + this.weekdaysShort(mom, '') + '|^' + this.weekdaysMin(mom, '');
Expand Down
33 changes: 33 additions & 0 deletions src/test/moment/locale.js
Expand Up @@ -471,6 +471,39 @@ test('moment().lang with missing key doesn\'t change locale', function (assert)
'preserve global locale in case of bad locale id');
});

test('when in strict mode with inexact parsing, treat periods in short weekdays literally, not as the regex-period', function (assert) {
moment.defineLocale('periods-in-short-weekdays', {
weekdays : 'Monday_Tuesday_Wednesday_Thursday_Friday_Saturday_Sunday'.split('_'),
weekdaysShort : 'mon_t...s_wed_thurs_fri_sat_sun'.split('_'),
weekdaysParseExact : false
});

moment().locale('periods-in-short-weekdays');
assert.equal(moment('thurs', 'ddd', true).format('dddd'), 'Thursday');
});

test('when in strict mode with inexact parsing, treat periods in full weekdays literally, not as the regex-period', function (assert) {
moment.defineLocale('periods-in-full-weekdays', {
weekdays : 'Monday_T....day_Wednesday_Thursday_Friday_Saturday_Sunday'.split('_'),
weekdaysShort : 'mon_tues_wed_thurs_fri_sat_sun'.split('_'),
weekdaysParseExact : false
});

moment().locale('periods-in-full-weekdays');
assert.equal(moment('Thursday', 'dddd', true).format('ddd'), 'thurs');
});

test('when in strict mode with inexact parsing, treat periods in min-weekdays literally, not as the regex-period', function (assert) {
moment.defineLocale('periods-in-min-weekdays', {
weekdays : 'Monday_Tuesday_Wednesday_Thursday_Friday_Saturday_Sunday'.split('_'),
weekdaysMin : 'mon_t...s_wed_thurs_fri_sat_sun'.split('_'),
weekdaysParseExact : false
});

moment().locale('periods-in-min-weekdays');
assert.equal(moment('thurs', 'dd', true).format('dddd'), 'Thursday');
});


// TODO: Enable this after fixing pl months parse hack hack
// test('monthsParseExact', function (assert) {
Expand Down