]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
bulletproofing array of formats
authorIsaac Cambron <isaac@isaaccambron.com>
Fri, 4 Oct 2013 17:28:30 +0000 (13:28 -0400)
committerIsaac Cambron <isaac@isaaccambron.com>
Fri, 4 Oct 2013 17:28:30 +0000 (13:28 -0400)
moment.js
test/moment/create.js
test/moment/is_valid.js
test/moment/parsing_flags.js

index 745f42da7ca5516ea79118ccc7fd18f98c5881e7..9822b8356e46da29a2d64a13790ea12fd91db118 100644 (file)
--- a/moment.js
+++ b/moment.js
             charsLeftOver : 0,
             nullInput : false,
             invalidMonth : null,
-            userInvalidated : false,
+            invalidFormat : false,
+            userInvalidated : false
         };
     }
 
                 !m._pf.empty &&
                 !m._pf.invalidMonth &&
                 !m._pf.nullInput &&
+                !m._pf.invalidFormat &&
                 !m._pf.userInvalidated;
 
             if (m._strict) {
         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);
 
             tempConfig._pf.score = currentScore;
 
-            if (currentScore < scoreToBeat) {
+            if (scoreToBeat == null || currentScore < scoreToBeat) {
                 scoreToBeat = currentScore;
                 bestMoment = tempConfig;
             }
index 1b82f06cf7baee5b202e1b4b2a7986ca241d6694..f2a1b126c14be87c565508ced996a4e5eba68342 100644 (file)
@@ -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');
index 216d0346bd793b636e3f1ceec05d9de0a70f4200..9142dadcda66f40480dfed7ea477443ead797074 100644 (file)
@@ -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');
index 4a5d079c67ddaaea7c512fa7f073255c9dfd0154..d0aa691ecf80344bb3282cf69f39fc756f554921 100644 (file)
@@ -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();
     }
 };