Skip to content

Commit

Permalink
deepEqual now passes several tests
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Jun 3, 2011
1 parent 5267ae1 commit 6fe06a5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
62 changes: 62 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,68 @@ Traverse.prototype.reduce = function (cb, init) {
return acc;
};

Traverse.prototype.deepEqual = function (obj) {
var equal = true;
var node = obj;

this.forEach(function (y) {
var notEqual = (function () {
equal = false;
//this.stop();
}).bind(this);

if (this.key) node = node[this.key];
var x = node;

this.post(function () {
node = x;
});

var toS = function (o) {
return Object.prototype.toString.call(o);
};

if (typeof x !== typeof y) {
notEqual();
}
else if (x.__proto__ !== y.__proto__) {
notEqual();
}
else if (x === y) {
// nop
}
else if (typeof y === 'object') {
if (x === null || y === null) {
if (x !== y) notEqual();
}
else if (toS(y) === '[object Arguments]'
|| toS(x) === '[object Arguments]') {
if (toS(x) !== toS(y)) {
notEqual();
}
}
else if (x instanceof Date && y instanceof Date) {
if (x.getTime() !== y.getTime()) {
notEqual();
}
}
else {
var kx = Object.keys(x);
var ky = Object.keys(y);
if (kx.length !== ky.length) return false;
for (var i = 0; i < kx.length; i++) {
var k = kx[i];
if (!Object.hasOwnProperty.call(y, k)) {
notEqual();
}
}
}
}
});

return equal;
};

Traverse.prototype.paths = function () {
var acc = [];
this.forEach(function (x) {
Expand Down
2 changes: 1 addition & 1 deletion test/equal.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ exports.deepDates = function () {

var d0 = new Date;
setTimeout(function () {
assert.ok(traverse.deepEqual(
assert.ok(!traverse.deepEqual(
{ d : d0, x : [ 1, 2, 3 ], },
{ d : new Date, x : [ 1, 2, 3 ] },
'microseconds should count in date equality'
Expand Down

0 comments on commit 6fe06a5

Please sign in to comment.