]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Implemented suggested changes 1693/head
authorSimon Bernier St-Pierre <sbernierstpierre@gmail.com>
Thu, 5 Jun 2014 14:38:22 +0000 (10:38 -0400)
committerSimon Bernier St-Pierre <sbernierstpierre@gmail.com>
Thu, 5 Jun 2014 14:38:22 +0000 (10:38 -0400)
`moment.js` now has a separate parseISO function which only handles
the parsing. `makeDateFromString` keeps the same behavior and uses
parseISO. The tests have also been refactored and moved into
their own function.

moment.js
test/moment/create.js

index 8364399c4b6c436d0020a4c45c4339180eceb34f..07525236df62512fe3195cc6208692f3c80180ea 100644 (file)
--- a/moment.js
+++ b/moment.js
     function makeDateFromStringAndFormat(config) {
 
         if (config._f === moment.ISO_8601) {
-            makeDateFromString(config, false);
+            parseISO(config);
             return;
         }
 
     }
 
     // date from iso format
-    function makeDateFromString(config, useFallback) {
+    function parseISO(config) {
         var i, l,
             string = config._i,
             match = isoRegex.exec(string);
 
-        useFallback = typeof useFallback === 'undefined' ? true : useFallback;
-
         if (match) {
             config._pf.iso = true;
             for (i = 0, l = isoDates.length; i < l; i++) {
                 config._f += "Z";
             }
             makeDateFromStringAndFormat(config);
+        } else {
+            config._isValid = false;
         }
-        else {
-            if (useFallback) {
-                moment.createFromInputFallback(config);
-            } else {
-                config._isValid = false;
-            }
+    }
+
+    // date from iso format or fallback
+    function makeDateFromString(config) {
+        parseISO(config);
+        if (config._isValid === false) {
+            delete config._isValid;
+            moment.createFromInputFallback(config);
         }
     }
 
     moment.defaultFormat = isoFormat;
 
     // constant that refers to the ISO standard
-    moment.ISO_8601 = 'ISO 8601';
+    moment.ISO_8601 = function () {};
 
     // Plugins that add properties should also add the key here (null value),
     // so we can properly clone ourselves.
index e81eedb23306b5e2d20de24c2208459aec089696..ca33d4e12107f1ddf4bbf6fca6a2c4c8f90c9eac 100644 (file)
@@ -395,18 +395,17 @@ exports.create = {
 
         test.equal(moment('01', ["MM", "DD"])._f, "MM", "Should use first valid format");
 
-        // pass an ISO date in the array of formats
-        function parseISO(string) {
-            return moment(string, [moment.ISO_8601, 'MM', 'HH:mm', 'YYYY']);
-        }
-        test.equal(parseISO('1994')._f, 'YYYY', 'iso: test parse YYYY');
-        test.equal(parseISO('17:15')._f, 'HH:mm', 'iso: test parse HH:mm');
-        test.equal(parseISO('06')._f, 'MM', 'iso: test parse MM');
-        test.equal(parseISO('2012-06-01')._pf.iso, true, 'iso: test parse iso');
-        
-        test.equal(moment('2014-05-05', [moment.ISO_8601, 'YYYY-MM-DD'])._pf.iso, true, 'iso: edge case array precedence iso');
-        test.equal(moment('2014-05-05', ['YYYY-MM-DD', moment.ISO_8601])._pf.iso, false, 'iso: edge case array precedence not iso');
+        test.done();
+    },
 
+    "string with array of formats + ISO": function (test) {
+        test.equal(moment('1994', [moment.ISO_8601, 'MM', 'HH:mm', 'YYYY']).year(), 1994, 'iso: test parse YYYY');
+        test.equal(moment('17:15', [moment.ISO_8601, 'MM', 'HH:mm', 'YYYY']).hour(), 17, 'iso: test parse HH:mm (1)');
+        test.equal(moment('17:15', [moment.ISO_8601, 'MM', 'HH:mm', 'YYYY']).minutes(), 15, 'iso: test parse HH:mm (2)');
+        test.equal(moment('06', [moment.ISO_8601, 'MM', 'HH:mm', 'YYYY']).month(), 6-1, 'iso: test parse MM');
+        test.equal(moment('2012-06-01', [moment.ISO_8601, 'MM', 'HH:mm', 'YYYY']).parsingFlags().iso, true, 'iso: test parse iso');
+        test.equal(moment('2014-05-05', [moment.ISO_8601, 'YYYY-MM-DD']).parsingFlags().iso, true, 'iso: edge case array precedence iso');
+        test.equal(moment('2014-05-05', ['YYYY-MM-DD', moment.ISO_8601]).parsingFlags().iso, false, 'iso: edge case array precedence not iso');
         test.done();
     },