Skip to content

Commit

Permalink
Fix strict month parsing of abbreviated/full months
Browse files Browse the repository at this point in the history
  • Loading branch information
ichernev committed Nov 17, 2014
1 parent 2d5a7bb commit 9cf3d91
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
20 changes: 15 additions & 5 deletions moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,22 +829,32 @@
return this._monthsShort[m.month()];
},

monthsParse : function (monthName) {
monthsParse : function (monthName, format, strict) {
var i, mom, regex;

if (!this._monthsParse) {
this._monthsParse = [];
this._longMonthsParse = [];
this._shortMonthsParse = [];
}

for (i = 0; i < 12; i++) {
// make the regex if we don't have it already
if (!this._monthsParse[i]) {
mom = moment.utc([2000, i]);
mom = moment.utc([2000, i]);
if (strict && !this._longMonthsParse[i]) {
this._longMonthsParse[i] = new RegExp('^' + this.months(mom, '').replace('.', '') + '$', 'i');
this._shortMonthsParse[i] = new RegExp('^' + this.monthsShort(mom, '').replace('.', '') + '$', 'i');
}
if (!strict && !this._monthsParse[i]) {
regex = '^' + this.months(mom, '') + '|^' + this.monthsShort(mom, '');
this._monthsParse[i] = new RegExp(regex.replace('.', ''), 'i');
}
// test the regex
if (this._monthsParse[i].test(monthName)) {
if (strict && format === 'MMMM' && this._longMonthsParse[i].test(monthName)) {
return i;
} else if (strict && format === 'MMM' && this._shortMonthsParse[i].test(monthName)) {
return i;
} else if (!strict && this._monthsParse[i].test(monthName)) {
return i;
}
}
Expand Down Expand Up @@ -1180,7 +1190,7 @@
break;
case 'MMM' : // fall through to MMMM
case 'MMMM' :
a = config._locale.monthsParse(input);
a = config._locale.monthsParse(input, token, config._strict);
// if we didn't find a month name, mark the date as invalid.
if (a != null) {
datePartArray[MONTH] = a;
Expand Down
10 changes: 10 additions & 0 deletions test/moment/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,16 @@ exports.create = {
test.equal(moment('12', 'SSS', true).isValid(), false, 'invalid two-digit milisecond');
test.equal(moment('123', 'SSS', true).isValid(), true, 'valid three-digit milisecond');

// strict parsing respects month length
test.ok(moment('1 January 2000', 'D MMMM YYYY', true).isValid(), "capital long-month + MMMM");
test.ok(!moment('1 January 2000', 'D MMM YYYY', true).isValid(), "capital long-month + MMM");
test.ok(!moment('1 Jan 2000', 'D MMMM YYYY', true).isValid(), "capital short-month + MMMM");
test.ok(moment('1 Jan 2000', 'D MMM YYYY', true).isValid(), "capital short-month + MMM");
test.ok(moment('1 january 2000', 'D MMMM YYYY', true).isValid(), "lower long-month + MMMM");
test.ok(!moment('1 january 2000', 'D MMM YYYY', true).isValid(), "lower long-month + MMM");
test.ok(!moment('1 jan 2000', 'D MMMM YYYY', true).isValid(), "lower short-month + MMMM");
test.ok(moment('1 jan 2000', 'D MMM YYYY', true).isValid(), "lower short-month + MMM");

test.done();
},

Expand Down

0 comments on commit 9cf3d91

Please sign in to comment.