Skip to content

Commit

Permalink
fix: issue #98
Browse files Browse the repository at this point in the history
Inspect the stack to discover when a property is defined. Also fixes
an issue where a supplied (but undefined) value on the rhs would not be
detected when the lhs property is not supplied.
  • Loading branch information
sberan committed Apr 28, 2017
1 parent f16f633 commit 5796c75
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,22 @@ function deepDiff(lhs, rhs, changes, prefilter, path, key, stack) {

var ltype = typeof lhs;
var rtype = typeof rhs;
if (ltype === 'undefined') {
if (rtype !== 'undefined') {
changes(new DiffNew(currentPath, rhs));
} else {
changes(new DiffDeleted(currentPath, lhs));
}
} else if (rtype === 'undefined') {

var ldefined = ltype !== 'undefined' || stack && stack[stack.length - 1].lhs.hasOwnProperty(key)
var rdefined = rtype !== 'undefined' || stack && stack[stack.length - 1].rhs.hasOwnProperty(key)

if (!ldefined && rdefined) {
changes(new DiffNew(currentPath, rhs));
} else if (!rdefined && ldefined) {
changes(new DiffDeleted(currentPath, lhs));
} else if (realTypeOf(lhs) !== realTypeOf(rhs)) {
changes(new DiffEdit(currentPath, lhs, rhs));
} else if (realTypeOf(lhs) === 'date' && (lhs - rhs) !== 0) {
changes(new DiffEdit(currentPath, lhs, rhs));
} else if (ltype === 'object' && lhs !== null && rhs !== null) {
stack = stack || [];
if (stack.indexOf(lhs) < 0) {
stack.push(lhs);
if (!stack.filter(function (x) { return x.lhs === lhs }).length) {
stack.push({ lhs: lhs, rhs: rhs });
if (Array.isArray(lhs)) {
var i, len = lhs.length;
for (i = 0; i < lhs.length; i++) {
Expand Down
31 changes: 27 additions & 4 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,11 +565,9 @@ describe('deep-diff', function() {
});

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

it('should detect a difference', function() {
var diff = deep.diff(lhs, rhs);
it('should detect a difference with undefined property on lhs', function() {
var diff = deep.diff({foo: undefined }, {});

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

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

});

it('should detect a difference with undefined property on rhs', function() {
var diff = deep.diff({}, { foo: undefined });

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

expect(diff[0].kind).to.be('N');
expect(diff[0].path).to.be.an('array');
expect(diff[0].path).to.have.length(1);
expect(diff[0].path[0]).to.be('foo');
expect(diff[0].rhs).to.be(undefined);

});
});

describe('regression test for issue #98', function() {
var lhs = {foo: undefined };
var rhs = {foo: undefined };

it('should not detect a difference with two undefined property values', function() {
var diff = deep.diff(lhs, rhs);

expect(diff).to.be(undefined);

});
});


Expand Down

0 comments on commit 5796c75

Please sign in to comment.