From 07d88ae4d189e0fc5c2ea0ff09f7cc8309080409 Mon Sep 17 00:00:00 2001 From: Caleb Cauthon Date: Sun, 15 Apr 2018 01:09:18 -0500 Subject: [PATCH] [bugfix] Treat periods as periods, not regex-anything period, for weekday 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 --- src/lib/units/day-of-week.js | 6 +++--- src/test/moment/locale.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/lib/units/day-of-week.js b/src/lib/units/day-of-week.js index 55027608d1..438160b870 100644 --- a/src/lib/units/day-of-week.js +++ b/src/lib/units/day-of-week.js @@ -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, ''); diff --git a/src/test/moment/locale.js b/src/test/moment/locale.js index 186c649dfe..adebfaee30 100644 --- a/src/test/moment/locale.js +++ b/src/test/moment/locale.js @@ -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) {