Skip to content

Commit 7c04715

Browse files
committed
[patch] fix null diff
1 parent 9ffb0a8 commit 7c04715

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ function diffler(obj1, obj2) {
5050
// Iterate over obj2 looking for any new additions
5151
for (key in obj2) {
5252
if (obj2.hasOwnProperty(key) && typeof obj2[key] !== 'function') {
53+
if (obj1 === null) {
54+
diff[key] = {
55+
from: obj1,
56+
to: obj2[key],
57+
};
58+
break;
59+
}
60+
5361
var obj1Val = obj1[key],
5462
obj2Val = obj2[key];
5563

tests/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,18 @@ describe('getDiff', () => {
120120
assert.deepEqual(difference.weight.from, { unit: 'kg', value: 80 });
121121
assert.equal(difference.weight.to, null);
122122
});
123+
124+
it('should detect comparisons with null', () => {
125+
const differenceFrom = diffler({ a: null, b: 'things' }, { a: 'more', b: 'things' });
126+
const differenceTo = diffler({ a: 'some', b: 'things' }, { a: null, b: 'things' });
127+
const same = diffler({ a: null, b: 'things' }, { a: null, b: 'things' });
128+
129+
assert.equal(Object.keys(differenceFrom).length, 1);
130+
assert.equal(Object.keys(differenceTo).length, 1);
131+
assert.equal(Object.keys(same).length, 0);
132+
133+
assert.equal(differenceFrom.a.from, null);
134+
assert.equal(differenceTo.a.to, null);
135+
});
123136
});
124137
});

0 commit comments

Comments
 (0)