]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
[bugfix] Invalid parse against array still picks best format (fixes #4435) (#5080)
authorchrisbucholz <40872939+chrisbucholz@users.noreply.github.com>
Fri, 24 Apr 2020 10:35:57 +0000 (03:35 -0700)
committerGitHub <noreply@github.com>
Fri, 24 Apr 2020 10:35:57 +0000 (13:35 +0300)
src/lib/create/from-string-and-array.js
src/test/moment/create.js

index 1d8a7a806cda2fee37170e4b5615b7acabb555e2..41a77acba3e1c7b788020bf2d1af81ea2160939d 100644 (file)
@@ -11,7 +11,9 @@ export function configFromStringAndArray(config) {
 
         scoreToBeat,
         i,
-        currentScore;
+        currentScore,
+        validFormatFound,
+        bestFormatIsValid = false;
 
     if (config._f.length === 0) {
         getParsingFlags(config).invalidFormat = true;
@@ -21,6 +23,7 @@ export function configFromStringAndArray(config) {
 
     for (i = 0; i < config._f.length; i++) {
         currentScore = 0;
+        validFormatFound = false;
         tempConfig = copyConfig({}, config);
         if (config._useUTC != null) {
             tempConfig._useUTC = config._useUTC;
@@ -28,8 +31,8 @@ export function configFromStringAndArray(config) {
         tempConfig._f = config._f[i];
         configFromStringAndFormat(tempConfig);
 
-        if (!isValid(tempConfig)) {
-            continue;
+        if (isValid(tempConfig)) {
+            validFormatFound = true;
         }
 
         // if there is any input that was not parsed add a penalty for that format
@@ -40,9 +43,19 @@ export function configFromStringAndArray(config) {
 
         getParsingFlags(tempConfig).score = currentScore;
 
-        if (scoreToBeat == null || currentScore < scoreToBeat) {
-            scoreToBeat = currentScore;
-            bestMoment = tempConfig;
+        if (!bestFormatIsValid) {
+            if (scoreToBeat == null || currentScore < scoreToBeat || validFormatFound) {
+                scoreToBeat = currentScore;
+                bestMoment = tempConfig;
+                if (validFormatFound) {
+                    bestFormatIsValid = true;
+                }
+            }
+        } else {
+            if (currentScore < scoreToBeat) {
+                scoreToBeat = currentScore;
+                bestMoment = tempConfig;
+            }
         }
     }
 
index 7f06dedfc3dfa1314ef98ad26fee42368973e37d..85321d41faa4d8471cbde10d941c75f3106eda9b 100644 (file)
@@ -429,6 +429,11 @@ test('string with array of formats + ISO', function (assert) {
     assert.equal(moment('2014-05-05', ['YYYY-MM-DD', moment.ISO_8601]).parsingFlags().iso, false, 'iso: edge case array precedence not iso');
 });
 
+test('strict parsing invalid date against array of formats', function (assert) {
+    var b = moment('2/30/2019 7:00pm', ['M/DD/YYYY h:mma", "MM/DD/YYYY h:mma", "M-D-YYYY h:mma", "MM-D-YYYY h:mma'], true);
+    assert.deepEqual(b.parsingFlags().parsedDateParts, [2019,1,30,7,0], 'strict parsing multiple formats should still select the best format even if the date is invalid');
+});
+
 test('string with format - years', function (assert) {
     assert.equal(moment('67', 'YY').format('YYYY'), '2067', '67 > 2067');
     assert.equal(moment('68', 'YY').format('YYYY'), '2068', '68 > 2068');