Skip to content

Commit

Permalink
Fixed isEqual if second object has isEqual implemented and added isOb…
Browse files Browse the repository at this point in the history
…ject method
  • Loading branch information
DmitryBaranovskiy committed Jun 6, 2011
1 parent cf6cc16 commit 42487bf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
17 changes: 17 additions & 0 deletions test/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ $(document).ready(function() {
ok(_.isEqual(new Date(100), new Date(100)), 'identical dates are equal');
ok(_.isEqual((/hello/ig), (/hello/ig)), 'identical regexes are equal');
ok(!_.isEqual(null, [1]), 'a falsy is never equal to a truthy');
ok(_.isEqual({isEqual: function () { return true; }}, {}), 'first object implements `isEqual`');
ok(_.isEqual({}, {isEqual: function () { return true; }}), 'second object implements `isEqual`');
ok(!_.isEqual({x: 1, y: undefined}, {x: 1, z: 2}), 'objects with the same number of undefined keys are not equal');
ok(!_.isEqual(_({x: 1, y: undefined}).chain(), _({x: 1, z: 2}).chain()), 'wrapped objects are not equal');
equals(_({x: 1, y: 2}).chain().isEqual(_({x: 1, y: 2}).chain()).value(), true, 'wrapped objects are equal');
Expand Down Expand Up @@ -136,6 +138,21 @@ $(document).ready(function() {
ok(_.isArguments(iArguments), 'even from another frame');
});

test("objects: isObject", function() {
ok(_.isObject(arguments), 'the arguments object is object');
ok(_.isObject([1, 2, 3]), 'and arrays');
ok(_.isObject($('html')[0]), 'and DOM element');
ok(_.isObject(iElement), 'even from another frame');
ok(_.isObject(function () {}), 'and functions');
ok(_.isObject(iFunction), 'even from another frame');
ok(!_.isObject(null), 'but not null');
ok(!_.isObject(undefined), 'and not undefined');
ok(!_.isObject('string'), 'and not string');
ok(!_.isObject(12), 'and not number');
ok(!_.isObject(true), 'and not boolean');
ok(_.isObject(new String('string')), 'but new String()');
});

test("objects: isArray", function() {
ok(!_.isArray(arguments), 'the arguments object is not an array');
ok(_.isArray([1, 2, 3]), 'but arrays are');
Expand Down
6 changes: 6 additions & 0 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@
if (b._chain) b = b._wrapped;
// One of them implements an isEqual()?
if (a.isEqual) return a.isEqual(b);
if (b.isEqual) return b.isEqual(a);
// Check dates' integer values.
if (_.isDate(a) && _.isDate(b)) return a.getTime() === b.getTime();
// Both are NaN?
Expand Down Expand Up @@ -641,6 +642,11 @@
return toString.call(obj) === '[object Array]';
};

// Is a given variable an object?
_.isObject = function(obj) {
return obj === Object(obj);
};

// Is a given variable an arguments object?
_.isArguments = function(obj) {
return !!(obj && hasOwnProperty.call(obj, 'callee'));
Expand Down

0 comments on commit 42487bf

Please sign in to comment.