From: Isaac Cambron Date: Fri, 20 Sep 2013 08:07:57 +0000 (-0400) Subject: wip X-Git-Tag: 2.3.0~7^2~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4460dcf6bab516d6e142c047b76679d17a59baf3;p=thirdparty%2Fmoment.git wip --- diff --git a/moment.js b/moment.js index b52aed11d..839a9e2b3 100644 --- a/moment.js +++ b/moment.js @@ -567,6 +567,11 @@ _week : { dow : 0, // Sunday is the first day of the week. doy : 6 // The week that contains Jan 1st is the first week of the year. + }, + + _invalidDate: 'Invalid date', + invalidDate: function () { + return this._invalidDate; } }); @@ -645,6 +650,10 @@ // format date using native date object function formatMoment(m, format) { + if (!m.isValid()) { + return m.lang().invalidDate(); + } + format = expandFormat(format, m.lang()); if (!formatFunctions[format]) { @@ -784,7 +793,7 @@ case 'a' : // fall through to A case 'A' : config._isPm = getLangDefinition(config._l).isPM(input); - break; + return; // 24 HOUR case 'H' : // fall through to hh case 'HH' : // fall through to hh @@ -1105,7 +1114,9 @@ var input = config._i, format = config._f; - if (input === null || input === '') { + if (input === null || + (typeof input === 'string' && + input.replace(/^\s+|\s+$/g, '') === '')) { return moment.invalid(); } diff --git a/test/moment/create.js b/test/moment/create.js index 22517fe50..62cd14e71 100644 --- a/test/moment/create.js +++ b/test/moment/create.js @@ -119,22 +119,31 @@ exports.create = { "string with format dropped am/pm bug" : function (test) { moment.lang('en'); - test.expect(3); + test.expect(6); - test.equal(moment('05/1/2012', 'MM/DD/YYYY h:m:s a').format('MM/DD/YYYY'), '05/01/2012', 'should not break if am/pm is left off from the parsing tokens'); + test.equal(moment('05/1/2012 12:25:00', 'MM/DD/YYYY h:m:s a').format('MM/DD/YYYY'), '05/01/2012', 'should not break if am/pm is left off from the parsing tokens'); test.equal(moment('05/1/2012 12:25:00 am', 'MM/DD/YYYY h:m:s a').format('MM/DD/YYYY'), '05/01/2012', 'should not break if am/pm is left off from the parsing tokens'); test.equal(moment('05/1/2012 12:25:00 pm', 'MM/DD/YYYY h:m:s a').format('MM/DD/YYYY'), '05/01/2012', 'should not break if am/pm is left off from the parsing tokens'); + test.ok(moment('05/1/2012 12:25:00', 'MM/DD/YYYY h:m:s a').isValid()) + test.ok(moment('05/1/2012 12:25:00 am', 'MM/DD/YYYY h:m:s a').isValid()) + test.ok(moment('05/1/2012 12:25:00 pm', 'MM/DD/YYYY h:m:s a').isValid()) + test.done(); }, "empty string with formats" : function (test) { - test.expect(3); + test.expect(8); - var currentDate = moment().startOf('day').format('YYYY-MM-DD HH:mm:ss'); - test.equal(moment(' ', 'MM').format('YYYY-MM-DD HH:mm:ss'), currentDate, 'should not break if input is an empty string'); - test.equal(moment(' ', 'DD').format('YYYY-MM-DD HH:mm:ss'), currentDate, 'should not break if input is an empty string'); - test.equal(moment(' ', ['MM', "DD"]).format('YYYY-MM-DD HH:mm:ss'), currentDate, 'should not break if input is an empty string'); + test.equal(moment('', 'MM').format('YYYY-MM-DD HH:mm:ss'), 'Invalid date'); + test.equal(moment(' ', 'MM').format('YYYY-MM-DD HH:mm:ss'), 'Invalid date'); + test.equal(moment(' ', 'DD').format('YYYY-MM-DD HH:mm:ss'), 'Invalid date'); + test.equal(moment(' ', ['MM', "DD"]).format('YYYY-MM-DD HH:mm:ss'), 'Invalid date'); + + test.ok(!moment('', 'MM').isValid()) + test.ok(!moment(' ', 'MM').isValid()) + test.ok(!moment(' ', 'DD').isValid()) + test.ok(!moment(' ', ['MM', "DD"]).isValid()) test.done(); }, @@ -211,7 +220,7 @@ exports.create = { ['HH:mm:ss S', '00:30:00 7'], ['HH:mm:ss SS', '00:30:00 78'], ['HH:mm:ss SSS', '00:30:00 789'], - ['X.SSS', '1234567890.123'], + ['X', '1234567890'], ['LT', '12:30 AM'], ['L', '09/02/1999'], ['l', '9/2/1999'], @@ -224,9 +233,11 @@ exports.create = { ], i; - test.expect(a.length); + test.expect(2 * a.length); for (i = 0; i < a.length; i++) { - test.equal(moment(a[i][1], a[i][0]).format(a[i][0]), a[i][1], a[i][0] + ' ---> ' + a[i][1]); + m = moment(a[i][1], a[i][0]); + test.ok(m.isValid()); + test.equal(m.format(a[i][0]), a[i][1], a[i][0] + ' ---> ' + a[i][1]); } test.done(); },