From: hokaccha Date: Thu, 15 Mar 2012 09:50:00 +0000 (+0900) Subject: Add `moment.isMoment()` X-Git-Tag: 1.5.0~3^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F205%2Fhead;p=thirdparty%2Fmoment.git Add `moment.isMoment()` --- diff --git a/moment.js b/moment.js index 8a0ad9d2b..fd1313cf2 100644 --- a/moment.js +++ b/moment.js @@ -561,6 +561,11 @@ } }); + // 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 index 000000000..d1f405c50 --- /dev/null +++ b/test/moment/is_moment.js @@ -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(); + } +};