Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add Object.isDate. [#443 state:resolved] (Nesterenko Dmitry, kangax, …

…Samuel Lebeau, Andrew Dupont)
  • Loading branch information...
commit 05e0ebc2c5df6e81b1dd908a15c48b919d992f09 1 parent e4503ee
@savetheclocktower savetheclocktower authored
Showing with 44 additions and 0 deletions.
  1. +2 −0  CHANGELOG
  2. +23 −0 src/lang/object.js
  3. +19 −0 test/unit/object_test.js
View
2  CHANGELOG
@@ -1,3 +1,5 @@
+* Add Object.isDate. [#443 state:resolved] (Nesterenko Dmitry, kangax, Samuel Lebeau, Andrew Dupont)
+
* Handle cases where `document` or `document.documentElement` is passed into Element#getOffsetParent. Fixes IE errors with many layout/positioning methods. [#90 state:resolved] (Padraig Kennedy, Andrew Dupont)
* Wrap `element` in `$` for Element#cumulativeOffset, #viewportOffset, #positionedOffset, and #getOffsetParent. [#782 state:resolved] (Radoslav Stankov, Andrew Dupont)
View
23 src/lang/object.js
@@ -32,6 +32,7 @@
NUMBER_CLASS = '[object Number]',
STRING_CLASS = '[object String]',
ARRAY_CLASS = '[object Array]',
+ DATE_CLASS = '[object Date]',
NATIVE_JSON_STRINGIFY_SUPPORT = window.JSON &&
typeof JSON.stringify === 'function' &&
JSON.stringify(0) === '0' &&
@@ -513,6 +514,27 @@
function isNumber(object) {
return _toString.call(object) === NUMBER_CLASS;
}
+
+ /**
+ * Object.isDate(object) -> Boolean
+ * - object (Object): The object to test.
+ *
+ * Returns `true` if `object` is of type [[Date]]; `false` otherwise.
+ *
+ * ##### Examples
+ *
+ * Object.isDate(new Date);
+ * //-> true
+ *
+ * Object.isDate("Dec 25, 1995");
+ * //-> false
+ *
+ * Object.isDate(new Date("Dec 25, 1995"));
+ * //-> true
+ **/
+ function isDate(object) {
+ return _toString.call(object) === DATE_CLASS;
+ }
/**
* Object.isUndefined(object) -> Boolean
@@ -556,6 +578,7 @@
isFunction: isFunction,
isString: isString,
isNumber: isNumber,
+ isDate: isDate,
isUndefined: isUndefined
});
})();
View
19 test/unit/object_test.js
@@ -160,6 +160,25 @@ new Test.Unit.Runner({
this.assert(!Object.isNumber(undefined));
this.assert(!Object.isNumber(document), 'host objects should return false rather than throw exceptions');
},
+
+ testObjectIsDate: function() {
+ var d = new Date();
+ this.assert(Object.isDate(d), 'constructor with no arguments');
+ this.assert(Object.isDate(new Date(0)), 'constructor with milliseconds');
+ this.assert(Object.isDate(new Date(1995, 11, 17)), 'constructor with Y, M, D');
+ this.assert(Object.isDate(new Date(1995, 11, 17, 3, 24, 0)), 'constructor with Y, M, D, H, M, S');
+ this.assert(Object.isDate(new Date(Date.parse("Dec 25, 1995"))), 'constructor with result of Date.parse');
+
+ this.assert(!Object.isDate(d.valueOf()), 'Date#valueOf returns a number');
+ this.assert(!Object.isDate(function() { }));
+ this.assert(!Object.isDate(0));
+ this.assert(!Object.isDate("a string"));
+ this.assert(!Object.isDate([]));
+ this.assert(!Object.isDate({}));
+ this.assert(!Object.isDate(false));
+ this.assert(!Object.isDate(undefined));
+ this.assert(!Object.isDate(document), 'host objects should return false rather than throw exceptions');
+ },
testObjectIsUndefined: function() {
this.assert(Object.isUndefined(undefined));
Please sign in to comment.
Something went wrong with that request. Please try again.