From: Isaac Cambron Date: Fri, 4 Oct 2013 17:28:30 +0000 (-0400) Subject: bulletproofing array of formats X-Git-Tag: 2.3.0~2^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3750215a3ad19d86b2b06360efe8ebb240e162cd;p=thirdparty%2Fmoment.git bulletproofing array of formats --- diff --git a/moment.js b/moment.js index 745f42da7..9822b8356 100644 --- a/moment.js +++ b/moment.js @@ -509,7 +509,8 @@ charsLeftOver : 0, nullInput : false, invalidMonth : null, - userInvalidated : false, + invalidFormat : false, + userInvalidated : false }; } @@ -520,6 +521,7 @@ !m._pf.empty && !m._pf.invalidMonth && !m._pf.nullInput && + !m._pf.invalidFormat && !m._pf.userInvalidated; if (m._strict) { @@ -1165,10 +1167,16 @@ var tempConfig, bestMoment, - scoreToBeat = 1000, + scoreToBeat, i, currentScore; + if (config._f.length == 0) { + config._pf.invalidFormat = true; + config._d = new Date(NaN); + return; + } + for (i = 0; i < config._f.length; i++) { currentScore = 0; tempConfig = extend({}, config); @@ -1188,7 +1196,7 @@ tempConfig._pf.score = currentScore; - if (currentScore < scoreToBeat) { + if (scoreToBeat == null || currentScore < scoreToBeat) { scoreToBeat = currentScore; bestMoment = tempConfig; } diff --git a/test/moment/create.js b/test/moment/create.js index 1b82f06cf..f2a1b126c 100644 --- a/test/moment/create.js +++ b/test/moment/create.js @@ -306,7 +306,6 @@ exports.create = { }, "string with array of formats" : function (test) { - test.expect(19); 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'); @@ -327,6 +326,15 @@ exports.create = { test.equal(moment('11-02-10 junk', ['MM-DD-YY', 'YY.MM.DD junk']).format('MM DD YYYY'), '02 10 2011', 'prefer formats that dont result in extra characters'); test.equal(moment('11-22-10', ['YY-MM-DD', 'YY-DD-MM']).format('MM DD YYYY'), '10 22 2011', 'prefer valid results'); + test.equal(moment('gibberish', ['YY-MM-DD', 'YY-DD-MM']).format('MM DD YYYY'), 'Invalid date', 'doest throw for invalid strings'); + test.equal(moment('gibberish', []).format('MM DD YYYY'), 'Invalid date', 'doest throw for an empty array'); + + //https://github.com/moment/moment/issues/1143 + test.equal(moment( + "System Administrator and Database Assistant (7/1/2011), System Administrator and Database Assistant (7/1/2011), Database Coordinator (7/1/2011), Vice President (7/1/2011), System Administrator and Database Assistant (5/31/2012), Database Coordinator (7/1/2012), System Administrator and Database Assistant (7/1/2013)", + ["MM/DD/YYYY", "MM-DD-YYYY", "YYYY-MM-DD", "YYYY-MM-DDTHH:mm:ssZ"]) + .format('YYYY-MM-DD'), '2011-07-01', 'Works for long strings'); + test.equal(moment('11-02-10', ['MM.DD.YY', 'DD-MM-YY']).format('MM DD YYYY'), '02 11 2010', 'escape RegExp special characters on comparing'); test.equal(moment('13-10-98', ['DD MM YY', 'DD MM YYYY'])._f, 'DD MM YY', 'use two digit year'); diff --git a/test/moment/is_valid.js b/test/moment/is_valid.js index 216d0346b..9142dadcd 100644 --- a/test/moment/is_valid.js +++ b/test/moment/is_valid.js @@ -56,8 +56,7 @@ exports.is_valid = { }, "string + formats bad date" : function (test) { - test.expect(9); - + test.equal(moment('2020-00-00', []).isValid(), false, 'invalid on empty array'); test.equal(moment('2020-00-00', ['YYYY-MM-DD', 'DD-MM-YYYY']).isValid(), false, 'invalid on all in array'); test.equal(moment('2020-00-00', ['DD-MM-YYYY', 'YYYY-MM-DD']).isValid(), false, 'invalid on all in array'); test.equal(moment('2020-01-01', ['YYYY-MM-DD', 'DD-MM-YYYY']).isValid(), true, 'valid on first'); diff --git a/test/moment/parsing_flags.js b/test/moment/parsing_flags.js index 4a5d079c6..d0aa691ec 100644 --- a/test/moment/parsing_flags.js +++ b/test/moment/parsing_flags.js @@ -173,6 +173,12 @@ exports.parsing_flags = { test.equal(flags('1982 May', 'YYYY MMMM').invalidMonth, null, 'normal input'); test.equal(flags('1982 Laser', 'YYYY MMMM').invalidMonth, 'Laser', 'bad month name'); + test.done(); + }, + + 'empty format array' : function (test) { + test.equal(flags('1982 May', ['YYYY MMM']).invalidFormat, false, 'empty format array'); + test.equal(flags('1982 May', []).invalidFormat, true, 'empty format array'); test.done(); } };