]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Add moment.isDate method
authorIskren Chernev <iskren.chernev@gmail.com>
Fri, 29 Aug 2014 15:12:59 +0000 (18:12 +0300)
committerIskren Chernev <iskren.chernev@gmail.com>
Wed, 24 Dec 2014 21:38:09 +0000 (13:38 -0800)
moment.js
test/moment/is_date.js [new file with mode: 0644]

index aa2e5c4e411b9900aac468636cb47d97adae958d..fe571731160a269bbfcc513de5c39791a27e7a68 100644 (file)
--- a/moment.js
+++ b/moment.js
         return toInt(input) + (toInt(input) > 68 ? 1900 : 2000);
     };
 
+    moment.isDate = isDate;
+
     /************************************
         Moment Prototype
     ************************************/
diff --git a/test/moment/is_date.js b/test/moment/is_date.js
new file mode 100644 (file)
index 0000000..455a395
--- /dev/null
@@ -0,0 +1,32 @@
+var moment = require('../../moment');
+
+exports.add = {
+    setUp : function (done) {
+        moment.createFromInputFallback = function () {
+            throw new Error('input not handled by moment');
+        };
+        done();
+    },
+
+    'isDate recognizes Date objects' : function (test) {
+        test.ok(moment.isDate(new Date()), 'no args (now)');
+        test.ok(moment.isDate(new Date([2014, 02, 15])), 'array args');
+        test.ok(moment.isDate(new Date('2014-03-15')), 'string args');
+        test.ok(moment.isDate(new Date('does NOT look like a date')), 'invalid date');
+        test.done();
+    },
+
+    'isDate rejects non-Date objects' : function (test) {
+        test.ok(!moment.isDate(), 'nothing');
+        test.ok(!moment.isDate(undefined), 'undefined');
+        test.ok(!moment.isDate(null), 'string args');
+        test.ok(!moment.isDate(42), 'number');
+        test.ok(!moment.isDate('2014-03-15'), 'string');
+        test.ok(!moment.isDate([2014, 2, 15]), 'array');
+        test.ok(!moment.isDate({year: 2014, month: 2, day: 15}), 'object');
+        test.ok(!moment.isDate({toString: function () {
+            return '[object Date]';
+        }}), 'lying object');
+        test.done();
+    }
+};