]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Ensure createFromInputFallback is called in all cases
authorIskren Chernev <iskren.chernev@gmail.com>
Fri, 11 Apr 2014 05:52:35 +0000 (22:52 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Fri, 11 Apr 2014 05:52:35 +0000 (22:52 -0700)
Every case where user supplied input ends up into the Date constructor, without
us knowing what would be the result, call createFromInputFallback so the
function can be deprecated, overwritten and replaced in future versions.

moment.js
test/moment/create.js
test/moment/is_moment.js
test/moment/is_valid.js

index 86ad1f1d7ea5f408512e653823459f96c054bdf0..0d45e33e2a95cf4a9c73aa69fee1f52fbeb2d44d 100644 (file)
--- a/moment.js
+++ b/moment.js
         };
     }
 
+    function deprecate(msg, fn) {
+        var firstTime = true;
+        function printMsg() {
+            if (typeof console !== 'undefined' && console.warn) {
+                console.warn("Deprecation warning: " + msg);
+            }
+        }
+        return extend(function () {
+            if (firstTime) {
+                printMsg();
+                firstTime = false;
+            }
+            return fn.apply(this, arguments);
+        }, fn);
+    }
+
     function padToken(func, count) {
         return function (a) {
             return leftZeroFill(func.call(this, a), count);
             makeDateFromStringAndFormat(config);
         }
         else {
-            config._d = new Date(string);
+            config._d = moment.createFromInputFallback(string);
         }
     }
 
         var input = config._i,
             format = config._f;
 
-        if (input === null) {
+        if (input === null || (format === undefined && input === '')) {
             return moment.invalid({nullInput: true});
         }
 
         return makeMoment(c);
     };
 
-    moment.createFromInputFallback = function (config) {
+    moment.createFromInputFallback = deprecate(
+            "moment construction falls back to js Date. This is " +
+            "discouraged and will be removed in upcoming major " +
+            "release. Please refer to " +
+            "https://github.com/moment/moment/issues/1407 for more info.",
+            function (config) {
         config._d = new Date(config._i);
-    };
+    });
 
     // creating with utc
     moment.utc = function (input, format, lang, strict) {
         }
     });
 
-    function deprecate(msg, fn) {
-        var firstTime = true;
-        function printMsg() {
-            if (typeof console !== 'undefined' && console.warn) {
-                console.warn("Deprecation warning: " + msg);
-            }
-        }
-        return extend(function () {
-            if (firstTime) {
-                printMsg();
-                firstTime = false;
-            }
-            return fn.apply(this, arguments);
-        }, fn);
-    }
-
     function rawMonthSetter(mom, value) {
         var dayOfMonth;
 
index 3f6beaf62879c4e0dcd2a91967f37e0de7ba5f41..0c5cebb2eeb068fce3f857fd4f15c2fe67e7a372 100644 (file)
@@ -135,13 +135,6 @@ exports.create = {
         test.done();
     },
 
-    "string without format" : function (test) {
-        test.expect(2);
-        test.ok(moment("Aug 9, 1995").toDate() instanceof Date, "Aug 9, 1995");
-        test.ok(moment("Mon, 25 Dec 1995 13:30:00 GMT").toDate() instanceof Date, "Mon, 25 Dec 1995 13:30:00 GMT");
-        test.done();
-    },
-
     "string without format - json" : function (test) {
         test.expect(5);
         test.equal(moment("Date(1325132654000)").valueOf(), 1325132654000, "Date(1325132654000)");
index 202f2ce1f59df503949a4d8719b3dd9900bd6c3a..93a78b306bc3f4511f462679c069d76023c4921f 100644 (file)
@@ -24,7 +24,7 @@ exports.isMoment = {
         };
 
         test.ok(moment.isMoment(moment()), 'simple moment object');
-        test.ok(moment.isMoment(moment('invalid date')), 'invalid moment object');
+        test.ok(moment.isMoment(moment(null)), 'invalid moment object');
         test.ok(moment.isMoment(extend({}, moment())), 'externally cloned moments are moments');
         test.ok(moment.isMoment(extend({}, moment.utc())), 'externally cloned utc moments are moments');
 
index 5a8b23b8c6915b23e2457d6e584fc79b05928755..eb53ad1b67a80d63400f27dacfd2839e032b036a 100644 (file)
@@ -78,13 +78,6 @@ exports.isValid = {
         test.done();
     },
 
-    "string nonsensical" : function (test) {
-        test.expect(1);
-
-        test.equal(moment('fail').isValid(), false, 'string "fail"');
-        test.done();
-    },
-
     "string nonsensical with format" : function (test) {
         test.expect(2);
 
@@ -239,7 +232,6 @@ exports.isValid = {
     "empty" : function (test) {
         test.equal(moment(null).isValid(), false, 'null');
         test.equal(moment('').isValid(), false, 'empty string');
-        test.equal(moment(' ').isValid(), false, 'empty when trimmed');
 
         test.equal(moment(null, 'YYYY').isValid(), false, 'format + null');
         test.equal(moment('', 'YYYY').isValid(), false, 'format + empty string');