From 2120b27b22efd58622c4d2919ee5a9241bc233d3 Mon Sep 17 00:00:00 2001 From: Jason Davies Date: Fri, 3 Dec 2010 13:01:58 +0000 Subject: [PATCH] Fix _.isEquals() for wrapped objects This caught me out when writing some unit tests that use _.isEquals(). They were all passing even though I knew they shouldn't be, and I realised I was checking equality with a wrapped object that I'd forgotten to unwrap. --- test/objects.js | 8 +++++--- underscore.js | 3 +++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/test/objects.js b/test/objects.js index b47de7fc4..f0a60ecfe 100644 --- a/test/objects.js +++ b/test/objects.js @@ -45,13 +45,15 @@ $(document).ready(function() { ok(_.isEqual(moe, clone), 'deep equality is true'); ok(_(moe).isEqual(clone), 'OO-style deep equality works'); ok(!_.isEqual(5, NaN), '5 is not equal to NaN'); - ok(NaN != NaN, 'NaN is not equal to NaN (native equality)'); - ok(NaN !== NaN, 'NaN is not equal to NaN (native identity)'); + ok(NaN != NaN, 'NaN is not equal to NaN (native equality)'); + ok(NaN !== NaN, 'NaN is not equal to NaN (native identity)'); ok(!_.isEqual(NaN, NaN), 'NaN is not equal to NaN'); 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({x: 1, y: undefined}, {x: 1, z: 2}), 'object with the same number of undefined keys are not equal'); + 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'); }); test("objects: isEmpty", function() { diff --git a/underscore.js b/underscore.js index 887d039c3..ca580fc2b 100644 --- a/underscore.js +++ b/underscore.js @@ -519,6 +519,9 @@ // Perform a deep comparison to check if two objects are equal. _.isEqual = function(a, b) { + // Unwrap any wrapped objects + if (a && a._chain) a = a.value(); + if (b && b._chain) b = b.value(); // Check object identity. if (a === b) return true; // Different types?