Skip to content

Commit

Permalink
[bugfix] Treat periods as periods, not regex-anything period, for wee…
Browse files Browse the repository at this point in the history
…kday parsing in strict mode. (#4453)

* Treat periods in short weekdays as literal periods when in strict mode with exact parsing turned off

* Add tests for the other 2 areas where periods were treated improperly, and organize the tests

* Remove trailing commas

* Follow styling standards
  • Loading branch information
calebcauthon authored and marwahaha committed Apr 15, 2018
1 parent c58511b commit 07d88ae
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
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

0 comments on commit 07d88ae

Please sign in to comment.