From cb6605d744e95fea33f90bfb5f973661bf1654ed Mon Sep 17 00:00:00 2001 From: Dan Nuttall Date: Tue, 29 Nov 2016 14:11:01 +0000 Subject: [PATCH] Add check for `null` case in `isObject` method --- src/index.js | 6 +++--- src/index.spec.js | 6 +++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index aac35c5..8622e2d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,10 @@ -const isObject = o => typeof o === 'object'; +const isObject = o => o != null && typeof o === 'object'; const isEmpty = o => Object.keys(o).length === 0; const diff = (lhs, rhs) => { if (lhs === rhs) return {}; - if (!isObject(lhs) || !isObject(rhs) || rhs === null || lhs === null) return rhs; + if (!isObject(lhs) || !isObject(rhs)) return rhs; const rhsKeys = Object.keys(rhs); @@ -23,4 +23,4 @@ const diff = (lhs, rhs) => { }, deletedValues); }; -export default diff; \ No newline at end of file +export default diff; diff --git a/src/index.spec.js b/src/index.spec.js index 441b1df..0d73236 100644 --- a/src/index.spec.js +++ b/src/index.spec.js @@ -43,6 +43,10 @@ describe('.diff', () => { expect(diff({ a: 1 }, { a: 2 })).to.deep.equal({ a: 2 }); }); + it('returns right hand side value when right hand side value is null', () => { + expect(diff({ a: 1 }, { a: null })).to.deep.equal({ a: null }); + }); + it('returns subset of right hand side value when sibling objects differ', () => { expect(diff({ a: { b: 1 }, c: 2 }, { a: { b: 1 }, c: 3 })).to.deep.equal({ c: 3 }); }); @@ -86,4 +90,4 @@ describe('.diff', () => { }); }); }); -}); \ No newline at end of file +});