Skip to content

Commit f40bb38

Browse files
pixelblendmattphillips
authored andcommitted
Add check for null case in isObject method (#1)
1 parent 8733cc0 commit f40bb38

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const isObject = o => typeof o === 'object';
1+
const isObject = o => o != null && typeof o === 'object';
22
const isEmpty = o => Object.keys(o).length === 0;
33

44
const diff = (lhs, rhs) => {
55
if (lhs === rhs) return {};
66

7-
if (!isObject(lhs) || !isObject(rhs) || rhs === null || lhs === null) return rhs;
7+
if (!isObject(lhs) || !isObject(rhs)) return rhs;
88

99
const rhsKeys = Object.keys(rhs);
1010

@@ -23,4 +23,4 @@ const diff = (lhs, rhs) => {
2323
}, deletedValues);
2424
};
2525

26-
export default diff;
26+
export default diff;

src/index.spec.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ describe('.diff', () => {
4343
expect(diff({ a: 1 }, { a: 2 })).to.deep.equal({ a: 2 });
4444
});
4545

46+
it('returns right hand side value when right hand side value is null', () => {
47+
expect(diff({ a: 1 }, { a: null })).to.deep.equal({ a: null });
48+
});
49+
4650
it('returns subset of right hand side value when sibling objects differ', () => {
4751
expect(diff({ a: { b: 1 }, c: 2 }, { a: { b: 1 }, c: 3 })).to.deep.equal({ c: 3 });
4852
});
@@ -86,4 +90,4 @@ describe('.diff', () => {
8690
});
8791
});
8892
});
89-
});
93+
});

0 commit comments

Comments
 (0)