From: Iskren Chernev Date: Fri, 11 Apr 2014 05:52:35 +0000 (-0700) Subject: Ensure createFromInputFallback is called in all cases X-Git-Tag: 2.6.0~2^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d85c3a03578c3914800f57166b074bd1971f063d;p=thirdparty%2Fmoment.git Ensure createFromInputFallback is called in all cases 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. --- diff --git a/moment.js b/moment.js index 86ad1f1d7..0d45e33e2 100644 --- a/moment.js +++ b/moment.js @@ -300,6 +300,22 @@ }; } + 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); @@ -1415,7 +1431,7 @@ makeDateFromStringAndFormat(config); } else { - config._d = new Date(string); + config._d = moment.createFromInputFallback(string); } } @@ -1567,7 +1583,7 @@ var input = config._i, format = config._f; - if (input === null) { + if (input === null || (format === undefined && input === '')) { return moment.invalid({nullInput: true}); } @@ -1613,9 +1629,14 @@ 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) { @@ -2192,22 +2213,6 @@ } }); - 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; diff --git a/test/moment/create.js b/test/moment/create.js index 3f6beaf62..0c5cebb2e 100644 --- a/test/moment/create.js +++ b/test/moment/create.js @@ -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)"); diff --git a/test/moment/is_moment.js b/test/moment/is_moment.js index 202f2ce1f..93a78b306 100644 --- a/test/moment/is_moment.js +++ b/test/moment/is_moment.js @@ -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'); diff --git a/test/moment/is_valid.js b/test/moment/is_valid.js index 5a8b23b8c..eb53ad1b6 100644 --- a/test/moment/is_valid.js +++ b/test/moment/is_valid.js @@ -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');