Skip to content

Add check for null case in isObject method #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', () => {
});
});
});
});
});