Skip to content

Commit

Permalink
Add Object.isDate. [#443 state:resolved] (Nesterenko Dmitry, kangax, …
Browse files Browse the repository at this point in the history
…Samuel Lebeau, Andrew Dupont)
  • Loading branch information
savetheclocktower committed Oct 17, 2010
1 parent e4503ee commit 05e0ebc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 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)
Expand Down
23 changes: 23 additions & 0 deletions src/lang/object.js
Expand Up @@ -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' &&
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -556,6 +578,7 @@
isFunction: isFunction,
isString: isString,
isNumber: isNumber,
isDate: isDate,
isUndefined: isUndefined
});
})();
19 changes: 19 additions & 0 deletions test/unit/object_test.js
Expand Up @@ -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));
Expand Down

0 comments on commit 05e0ebc

Please sign in to comment.