From bfdc40e35f7a3ec3acf96f7f8599602747ef999d Mon Sep 17 00:00:00 2001 From: James Halliday Date: Sun, 5 Jun 2011 21:18:31 -0700 Subject: [PATCH] deep equal tests now pass, delete map tests fail though --- index.js | 17 +++++++++++++---- test/equal.js | 27 ++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 794d09a..3ae4eb4 100644 --- a/index.js +++ b/index.js @@ -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 () { @@ -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)) { @@ -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_) { diff --git a/test/equal.js b/test/equal.js index bfc3f80..43b6479 100644 --- a/test/equal.js +++ b/test/equal.js @@ -122,14 +122,27 @@ 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( @@ -137,7 +150,9 @@ exports.falsy = function () { [ 1, 2, null, 4 ], 'deleted array elements cannot be null' )); - +}; + +exports.deletedObjectEqual = function () { var obj = { a : 1, b : 2, c : 3 }; delete obj.c; @@ -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 ],