]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Add `moment.isMoment()` 205/head
authorhokaccha <k.hokamura@gmail.com>
Thu, 15 Mar 2012 09:50:00 +0000 (18:50 +0900)
committerhokaccha <k.hokamura@gmail.com>
Thu, 15 Mar 2012 09:50:00 +0000 (18:50 +0900)
moment.js
test/moment/is_moment.js [new file with mode: 0644]

index 8a0ad9d2bbfed533168ed02bf80b26dec4148c92..fd1313cf27609ad34c9a18c29184dc163a206b14 100644 (file)
--- a/moment.js
+++ b/moment.js
         }
     });
 
+    // compare moment object
+    moment.isMoment = function (obj) {
+        return obj instanceof Moment;
+    };
+
     // shortcut for prototype
     moment.fn = Moment.prototype = {
 
diff --git a/test/moment/is_moment.js b/test/moment/is_moment.js
new file mode 100644 (file)
index 0000000..d1f405c
--- /dev/null
@@ -0,0 +1,27 @@
+var moment = require('../../moment');
+
+exports.is_moment = {
+    "is moment object": function(test) {
+        test.expect(11);
+
+        var MyObj = function() {};
+        MyObj.prototype.native = function() {
+            return new Date();
+        }
+
+        test.ok(moment.isMoment(moment()), 'simple moment object');
+        test.ok(moment.isMoment(moment('invalid date')), 'invalid moment object');
+
+        test.ok(!moment.isMoment(new MyObj()), 'myObj is not moment object');
+        test.ok(!moment.isMoment(moment), 'moment function is not moment object');
+        test.ok(!moment.isMoment(new Date()), 'date object is not moment object');
+        test.ok(!moment.isMoment(Object), 'Object is not moment object');
+        test.ok(!moment.isMoment('foo'), 'string is not moment object');
+        test.ok(!moment.isMoment(1), 'number is not moment object');
+        test.ok(!moment.isMoment(NaN), 'NaN is not moment object');
+        test.ok(!moment.isMoment(null), 'null is not moment object');
+        test.ok(!moment.isMoment(undefined), 'undefined is not moment object');
+
+        test.done();
+    }
+};