]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fix strict month parsing of abbreviated/full months
authorIskren Chernev <iskren.chernev@gmail.com>
Fri, 17 Oct 2014 09:06:45 +0000 (02:06 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Mon, 17 Nov 2014 06:39:14 +0000 (22:39 -0800)
moment.js
test/moment/create.js

index f3f708325c67085ed1a94b2eacbe71a10e8524e0..94a3859b2f1da115e67aa1828814ae38dd3b6659 100644 (file)
--- a/moment.js
+++ b/moment.js
             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;
                 }
             }
             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;
index 1baed4a1e7e01a43195e1cf25668751e76f52520..c3b3ad5bd5c467dc6addaf5f36666582ab867eb0 100644 (file)
@@ -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();
     },