]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
wip
authorIsaac Cambron <isaac@isaaccambron.com>
Fri, 20 Sep 2013 08:07:57 +0000 (04:07 -0400)
committerIsaac Cambron <isaac@isaaccambron.com>
Fri, 20 Sep 2013 08:07:57 +0000 (04:07 -0400)
moment.js
test/moment/create.js

index b52aed11d0246f3c3f82eaa28b969f4b66b07cbb..839a9e2b313a66c0ec2c9c202022ca35b1d65526 100644 (file)
--- a/moment.js
+++ b/moment.js
         _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;
         }
     });
 
     // 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]) {
         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
         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();
         }
 
index 22517fe5022b0b52168613668d5d57abccea5058..62cd14e7107617fbff95eeb010f69f4198bd8b03 100644 (file)
@@ -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();
     },