Skip to content

Commit

Permalink
Fix issue when obj1 property value is !Object (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeljic authored and dispix committed Jul 12, 2019
1 parent 069764b commit 27eb242
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/deepDiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ function deepDiff (obj1, obj2, keepNewKeys = false) {
return
}

if (!(obj1[key] instanceof Object)) {
diff[key] = obj2[key]

return
}

diff[key] = deepDiff(obj1[key], obj2[key], keepNewKeys)

// Don't keep trace of a null key if it was generated by a recursive use of deepDiff.
Expand Down
43 changes: 39 additions & 4 deletions test/deepDiff.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ describe('deepDiff', () => {
i: true,
j: null,
k: 'blue',
l: [[0, 1], [2, 3]]
l: [[0, 1], [2, 3]],
m: 'one',
n: {
a: 1,
b: {},
c: 'one'
}
}

const objTwo = {
Expand All @@ -31,7 +37,18 @@ describe('deepDiff', () => {
i: false,
j: null,
k: null,
l: [[0, 1], [2, 4]]
l: [[0, 1], [2, 4]],
m: {
a: 1,
b: 2
},
n: {
a: 1,
b: {},
c: {
d: 'one'
}
}
}

const expected = {
Expand All @@ -42,7 +59,16 @@ describe('deepDiff', () => {
h: [1, 'owt', 3, 'four'],
i: false,
k: null,
l: [[0, 1], [2, 4]]
l: [[0, 1], [2, 4]],
m: {
a: 1,
b: 2
},
n: {
c: {
d: 'one'
}
}
}

const expectedWithNewKeys = {
Expand All @@ -54,7 +80,16 @@ describe('deepDiff', () => {
h: [1, 'owt', 3, 'four'],
i: false,
k: null,
l: [[0, 1], [2, 4]]
l: [[0, 1], [2, 4]],
m: {
a: 1,
b: 2
},
n: {
c: {
d: 'one'
}
}
}

it('should return the diff object between two objects', () => {
Expand Down

0 comments on commit 27eb242

Please sign in to comment.