Skip to content

Commit

Permalink
deep equal tests now pass, delete map tests fail though
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Jun 6, 2011
1 parent 2b15a41 commit bfdc40e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
17 changes: 13 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,18 @@ Traverse.prototype.deepEqual = function (obj) {
var notEqual = (function () {
equal = false;
//this.stop();
return undefined;
}).bind(this);

if (this.key) node = node[this.key];
if (!this.isRoot) {
/*
if (!Object.hasOwnProperty.call(node, this.key)) {
return notEqual();
}
*/
node = node[this.key];
}

var x = node;

this.post(function () {
Expand Down Expand Up @@ -107,7 +116,7 @@ Traverse.prototype.deepEqual = function (obj) {
else {
var kx = Object.keys(x);
var ky = Object.keys(y);
if (kx.length !== ky.length) return false;
if (kx.length !== ky.length) return notEqual();
for (var i = 0; i < kx.length; i++) {
var k = kx[i];
if (!Object.hasOwnProperty.call(y, k)) {
Expand Down Expand Up @@ -207,8 +216,8 @@ function walk (root, cb, immutable) {
post : function (f) { modifiers.post = f }
};

if (typeof node == 'object' && node !== null) {
state.isLeaf = Object.keys(node).length == 0
if (typeof node === 'object' && node !== null) {
state.isLeaf = Object.keys(node).length == 0;

for (var i = 0; i < parents.length; i++) {
if (parents[i].node_ === node_) {
Expand Down
27 changes: 24 additions & 3 deletions test/equal.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,37 @@ exports.falsy = function () {
{ a : 1, b : 2, c : [ 3, null, 5 ] },
'null is not undefined, however deeply!'
));

};

exports.deletedArrayEqual = function () {
var xs = [ 1, 2, 3, 4 ];
delete xs[2];

var ys = Object.create(Array.prototype);
ys[0] = 1;
ys[1] = 2;
ys[3] = 4;

assert.ok(traverse.deepEqual(
xs, ys,
'arrays with deleted elements are only equal to'
+ ' arrays with similarly deleted elements'
));

assert.ok(!traverse.deepEqual(
xs,
[ 1, 2, undefined, 4 ],
'deleted array elements can be undefined'
'deleted array elements cannot be undefined'
));

assert.ok(!traverse.deepEqual(
xs,
[ 1, 2, null, 4 ],
'deleted array elements cannot be null'
));

};

exports.deletedObjectEqual = function () {
var obj = { a : 1, b : 2, c : 3 };
delete obj.c;

Expand All @@ -157,6 +172,12 @@ exports.falsy = function () {
));
};

exports.emptyKeyEqual = function () {
assert.ok(!traverse.deepEqual(
{ a : 1 }, { a : 1, '' : 55 }
));
};

exports.deepArguments = function () {
assert.ok(!traverse.deepEqual(
[ 4, 5, 6 ],
Expand Down

0 comments on commit bfdc40e

Please sign in to comment.