diff --git a/.gitignore b/.gitignore index a175d44..3ccf798 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules/ dist coverage npm-debug.log +yarn-error.log diff --git a/src/diff/index.js b/src/diff/index.js index cbecd19..ca85c85 100644 --- a/src/diff/index.js +++ b/src/diff/index.js @@ -1,4 +1,4 @@ -import { isDate, isEmpty, isObject, hasOwnProperty } from '../utils'; +import { isDate, isEmptyObject, isObject, hasOwnProperty } from '../utils'; const diff = (lhs, rhs) => { if (lhs === rhs) return {}; // equal return no diff @@ -22,7 +22,9 @@ const diff = (lhs, rhs) => { const difference = diff(l[key], r[key]); - if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc; // return no diff + // If the difference is empty, and the lhs is an empty object or the rhs is not an empty object + if (isEmptyObject(difference) && !isDate(difference) && (isEmptyObject(l[key]) || !isEmptyObject(r[key]))) + return acc; // return no diff return { ...acc, [key]: difference }; // return updated key }, deletedValues); diff --git a/src/diff/index.test.js b/src/diff/index.test.js index 32ba35f..1c19ef6 100644 --- a/src/diff/index.test.js +++ b/src/diff/index.test.js @@ -43,6 +43,10 @@ describe('.diff', () => { describe('recursive case', () => { describe('object', () => { + test("return right hand side empty object value when left hand side has been updated", () => { + expect(diff({ a: 1 }, { a: {} })).toEqual({ a: {} }); + }); + test('returns right hand side value when given objects are different', () => { expect(diff({ a: 1 }, { a: 2 })).toEqual({ a: 2 }); }); @@ -77,6 +81,9 @@ describe('.diff', () => { }); describe('arrays', () => { + test("return right hand side empty object value when left hand side has been updated", () => { + expect(diff([{ a: 1 }], [{ a: {} }])).toEqual({ 0: { a: {} } }); + }); test('returns right hand side value as object of indices to value when arrays are different', () => { expect(diff([1], [2])).toEqual({ 0: 2 }); }); diff --git a/src/updated/index.js b/src/updated/index.js index c75734b..7472759 100644 --- a/src/updated/index.js +++ b/src/updated/index.js @@ -1,7 +1,6 @@ -import { isDate, isEmpty, isObject, hasOwnProperty } from '../utils'; +import { isDate, isEmptyObject, isObject, hasOwnProperty } from '../utils'; const updatedDiff = (lhs, rhs) => { - if (lhs === rhs) return {}; if (!isObject(lhs) || !isObject(rhs)) return rhs; @@ -15,11 +14,12 @@ const updatedDiff = (lhs, rhs) => { } return Object.keys(r).reduce((acc, key) => { - if (hasOwnProperty(l, key)) { const difference = updatedDiff(l[key], r[key]); - if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc; + // If the difference is empty, and the lhs is an empty object or the rhs is not an empty object + if (isEmptyObject(difference) && !isDate(difference) && (isEmptyObject(l[key]) || !isEmptyObject(r[key]))) + return acc; // return no diff return { ...acc, [key]: difference }; } diff --git a/src/updated/index.test.js b/src/updated/index.test.js index c9bd125..48b419a 100644 --- a/src/updated/index.test.js +++ b/src/updated/index.test.js @@ -43,6 +43,10 @@ describe('.updatedDiff', () => { describe('recursive case', () => { describe('object', () => { + test("return right hand side empty object value when left hand side has been updated", () => { + expect(updatedDiff({ a: 1 }, { a: {} })).toEqual({ a: {} }); + }); + test('returns right hand side value when given objects are different at root', () => { expect(updatedDiff({ a: 1 }, { a: 2 })).toEqual({ a: 2 }); }); @@ -77,6 +81,10 @@ describe('.updatedDiff', () => { }); describe('arrays', () => { + test("return right hand side empty object value when left hand side has been updated", () => { + expect(updatedDiff([{ a: 1 }], [{ a: {} }])).toEqual({ 0: { a: {} } }); + }); + test('returns right hand side value as object of indices to value when arrays are different', () => { expect(updatedDiff([1], [2])).toEqual({ 0: 2 }); }); diff --git a/src/utils/index.js b/src/utils/index.js index e7a961d..282c5d1 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -2,3 +2,4 @@ export const isDate = d => d instanceof Date; export const isEmpty = o => Object.keys(o).length === 0; export const isObject = o => o != null && typeof o === 'object'; export const hasOwnProperty = (o, ...args) => Object.prototype.hasOwnProperty.call(o, ...args) +export const isEmptyObject = (o) => isObject(o) && isEmpty(o);