Skip to content

Commit 809c1ee

Browse files
author
Phillip Clark
authored
Merge pull request #99 from sberan/fix-undefined-compare
fix: issue #98 I appreciate the additional tests too!
2 parents f16f633 + 5796c75 commit 809c1ee

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

index.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -150,22 +150,22 @@ function deepDiff(lhs, rhs, changes, prefilter, path, key, stack) {
150150

151151
var ltype = typeof lhs;
152152
var rtype = typeof rhs;
153-
if (ltype === 'undefined') {
154-
if (rtype !== 'undefined') {
155-
changes(new DiffNew(currentPath, rhs));
156-
} else {
157-
changes(new DiffDeleted(currentPath, lhs));
158-
}
159-
} else if (rtype === 'undefined') {
153+
154+
var ldefined = ltype !== 'undefined' || stack && stack[stack.length - 1].lhs.hasOwnProperty(key)
155+
var rdefined = rtype !== 'undefined' || stack && stack[stack.length - 1].rhs.hasOwnProperty(key)
156+
157+
if (!ldefined && rdefined) {
158+
changes(new DiffNew(currentPath, rhs));
159+
} else if (!rdefined && ldefined) {
160160
changes(new DiffDeleted(currentPath, lhs));
161161
} else if (realTypeOf(lhs) !== realTypeOf(rhs)) {
162162
changes(new DiffEdit(currentPath, lhs, rhs));
163163
} else if (realTypeOf(lhs) === 'date' && (lhs - rhs) !== 0) {
164164
changes(new DiffEdit(currentPath, lhs, rhs));
165165
} else if (ltype === 'object' && lhs !== null && rhs !== null) {
166166
stack = stack || [];
167-
if (stack.indexOf(lhs) < 0) {
168-
stack.push(lhs);
167+
if (!stack.filter(function (x) { return x.lhs === lhs }).length) {
168+
stack.push({ lhs: lhs, rhs: rhs });
169169
if (Array.isArray(lhs)) {
170170
var i, len = lhs.length;
171171
for (i = 0; i < lhs.length; i++) {

test/tests.js

+27-4
Original file line numberDiff line numberDiff line change
@@ -565,11 +565,9 @@ describe('deep-diff', function() {
565565
});
566566

567567
describe('regression test for issue #70', function() {
568-
var lhs = {foo: undefined };
569-
var rhs = {};
570568

571-
it('should detect a difference', function() {
572-
var diff = deep.diff(lhs, rhs);
569+
it('should detect a difference with undefined property on lhs', function() {
570+
var diff = deep.diff({foo: undefined }, {});
573571

574572
expect(diff.length).to.be(1);
575573

@@ -580,6 +578,31 @@ describe('deep-diff', function() {
580578
expect(diff[0].lhs).to.be(undefined);
581579

582580
});
581+
582+
it('should detect a difference with undefined property on rhs', function() {
583+
var diff = deep.diff({}, { foo: undefined });
584+
585+
expect(diff.length).to.be(1);
586+
587+
expect(diff[0].kind).to.be('N');
588+
expect(diff[0].path).to.be.an('array');
589+
expect(diff[0].path).to.have.length(1);
590+
expect(diff[0].path[0]).to.be('foo');
591+
expect(diff[0].rhs).to.be(undefined);
592+
593+
});
594+
});
595+
596+
describe('regression test for issue #98', function() {
597+
var lhs = {foo: undefined };
598+
var rhs = {foo: undefined };
599+
600+
it('should not detect a difference with two undefined property values', function() {
601+
var diff = deep.diff(lhs, rhs);
602+
603+
expect(diff).to.be(undefined);
604+
605+
});
583606
});
584607

585608

0 commit comments

Comments
 (0)