Skip to content

Commit

Permalink
Add check for null case in isObject method (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelblend authored and mattphillips committed Nov 30, 2016
1 parent 8733cc0 commit f40bb38
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -23,4 +23,4 @@ const diff = (lhs, rhs) => {
}, deletedValues);
};

export default diff;
export default diff;
6 changes: 5 additions & 1 deletion src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
Expand Down Expand Up @@ -86,4 +90,4 @@ describe('.diff', () => {
});
});
});
});
});

0 comments on commit f40bb38

Please sign in to comment.