From: Tim Wood Date: Mon, 3 Jun 2013 18:14:41 +0000 (-0700) Subject: add a penalty to formats that do not parse the entire input #818 X-Git-Tag: 2.1.0~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=71bf2ea172f62f510c895f70a36ddb90641d3935;p=thirdparty%2Fmoment.git add a penalty to formats that do not parse the entire input #818 --- diff --git a/moment.js b/moment.js index 51b92be0b..0132147c2 100644 --- a/moment.js +++ b/moment.js @@ -847,6 +847,12 @@ 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; @@ -877,6 +883,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; diff --git a/test/moment/create.js b/test/moment/create.js index c08536f36..e48fe6a05 100644 --- a/test/moment/create.js +++ b/test/moment/create.js @@ -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();