]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
add a penalty to formats that do not parse the entire input #818
authorTim Wood <washwithcare@gmail.com>
Mon, 3 Jun 2013 18:14:41 +0000 (11:14 -0700)
committerTim Wood <washwithcare@gmail.com>
Mon, 3 Jun 2013 18:15:40 +0000 (11:15 -0700)
moment.js
test/moment/create.js

index 51b92be0b2e88f5e291322ca04bc83aee895b0e0..0132147c24efe11674d99da11e449d08856f89be 100644 (file)
--- a/moment.js
+++ b/moment.js
                 addTimeToArrayFromToken(tokens[i], parsedInput, config);
             }
         }
+
+        // add remaining unparsed input to the string
+        if (string) {
+            config._il = string;
+        }
+
         // handle am pm
         if (config._isPm && config._a[3] < 12) {
             config._a[3] += 12;
 
             currentScore = compareArrays(tempConfig._a, tempMoment.toArray());
 
+            // if there is any input that was not parsed
+            // add a penalty for that format
+            if (tempMoment._il) {
+                currentScore += tempMoment._il.length;
+            }
+
             if (currentScore < scoreToBeat) {
                 scoreToBeat = currentScore;
                 bestMoment = tempMoment;
index c08536f36a224cd8bf81b097ca85fc0d6dfdeebe..e48fe6a050755a1bf87121194bcf75017e0d1444 100644 (file)
@@ -248,7 +248,7 @@ exports.create = {
     },
 
     "string with array of formats" : function(test) {
-        test.expect(10);
+        test.expect(12);
 
         test.equal(moment('11-02-1999', ['MM-DD-YYYY', 'DD-MM-YYYY']).format('MM DD YYYY'), '11 02 1999', 'switching month and day');
         test.equal(moment('02-11-1999', ['MM/DD/YYYY', 'YYYY-MM-DD', 'MM-DD-YYYY']).format('MM DD YYYY'), '02 11 1999', 'year last');
@@ -262,6 +262,9 @@ exports.create = {
         test.equal(moment('13-14-1999', ['MM/DD/YYYY', 'DD/MM/YYYY']).format('MM DD YYYY'), '01 14 2000', 'either can be a month, month first format');
         test.equal(moment('13-14-1999', ['DD/MM/YYYY', 'MM/DD/YYYY']).format('MM DD YYYY'), '02 13 2000', 'either can be a month, day first format');
 
+        test.equal(moment('13-14-98', ['DD MM YY', 'DD MM YYYY'])._f, 'DD MM YY', 'use two digit year');
+        test.equal(moment('13-14-1998', ['DD MM YY', 'DD MM YYYY'])._f, 'DD MM YYYY', 'use four digit year');
+
         test.equal(moment('01', ["MM", "DD"])._f, "MM", "Should use first valid format");
 
         test.done();