diff --git a/src/added/index.spec.js b/src/added/index.spec.js index 2d6281c..4f460f0 100644 --- a/src/added/index.spec.js +++ b/src/added/index.spec.js @@ -16,6 +16,7 @@ describe('.addedDiff', () => { ['object', { a: 1 }], ['array', [1]], ['function', () => ({})], + ['date', new Date()], ]).it('returns empty object when given values of type %s are equal', (type, value) => { expect(addedDiff(value, value)).to.deep.equal({}); }); @@ -34,6 +35,7 @@ describe('.addedDiff', () => { ['872983', { areaCode: '+44', number: '872983' }], [100, () => ({})], [() => ({}), 100], + [new Date('2017-01-01'), new Date('2017-01-02')], ]).it('returns empty object when values are not equal (%s, %s)', (lhs, rhs) => { expect(addedDiff(lhs, rhs)).to.deep.equal({}); }); @@ -57,6 +59,10 @@ describe('.addedDiff', () => { it('returns subset of right hand side value when a key value has been added deeply', () => { expect(addedDiff({ a: { b: 1} }, { a: { b: 1, c: 2 } })).to.deep.equal({ a: { c: 2 } }); }); + + it('returns subset of right hand side with added date', () => { + expect(addedDiff({}, { date: new Date('2016') })).to.eql({ date: new Date('2016') }); + }); }); describe('arrays', () => { @@ -71,6 +77,10 @@ describe('.addedDiff', () => { it('returns subset of right hand side array as object of indices to value when right hand side array has additions', () => { expect(addedDiff([1, 2, 3], [1, 2, 3, 9])).to.deep.equal({ 3: 9 }); }); + + it('returns subset of right hand side with added date', () => { + expect(addedDiff([], [new Date('2016')])).to.eql({ 0: new Date('2016') }); + }); }); }); }); diff --git a/src/deleted/index.spec.js b/src/deleted/index.spec.js index fe48dbe..5197a91 100644 --- a/src/deleted/index.spec.js +++ b/src/deleted/index.spec.js @@ -16,6 +16,7 @@ describe('.deletedDiff', () => { ['object', { a: 1 }], ['array', [1]], ['function', () => ({})], + ['date', new Date()], ]).it('returns empty object when given values of type %s are equal', (type, value) => { expect(deletedDiff(value, value)).to.deep.equal({}); }); @@ -34,6 +35,7 @@ describe('.deletedDiff', () => { ['872983', { areaCode: '+44', number: '872983' }], [100, () => ({})], [() => ({}), 100], + [new Date('2017-01-01'), new Date('2017-01-02')], ]).it('returns empty object when given values are unequal', (lhs, rhs) => { expect(deletedDiff(lhs, rhs)).to.deep.equal({}); }); @@ -57,6 +59,10 @@ describe('.deletedDiff', () => { it('returns keys as undefined when deeply deleted from right hand side', () => { expect(deletedDiff({ a: { b: 1 }, c: 2, d: { e: 100 } }, { a: { b: 1 }, c: 2, d: {} })).to.deep.equal({ d: { e: undefined } }); }); + + it('returns subset of right hand side with deleted date', () => { + expect(deletedDiff({ date: new Date('2016') }, {})).to.eql({ date: undefined }); + }); }); describe('arrays', () => { @@ -71,6 +77,10 @@ describe('.deletedDiff', () => { it('returns subset of right hand side array as object of indices to value when right hand side array has deletions', () => { expect(deletedDiff([1, 2, 3], [1, 3])).to.deep.equal({ 2: undefined }); }); + + it('returns subset of right hand side with added date', () => { + expect(deletedDiff([new Date('2016')], [])).to.eql({ 0: undefined }); + }); }); }); }); diff --git a/src/diff/index.js b/src/diff/index.js index 6b18d16..b681591 100644 --- a/src/diff/index.js +++ b/src/diff/index.js @@ -1,4 +1,4 @@ -import { isEmpty, isObject } from '../utils'; +import { isDate, isEmpty, isObject } from '../utils'; const diff = (lhs, rhs) => { if (lhs === rhs) return {}; // equal return no diff @@ -9,12 +9,17 @@ const diff = (lhs, rhs) => { return rhs.hasOwnProperty(key) ? acc : { ...acc, [key]: undefined }; }, {}); + if (isDate(lhs) || isDate(rhs)) { + if (lhs.toString() == rhs.toString()) return {}; + return rhs; + } + return Object.keys(rhs).reduce((acc, key) => { if (!lhs.hasOwnProperty(key)) return { ...acc, [key]: rhs[key] }; // return added rhs key const difference = diff(lhs[key], rhs[key]); - if (isObject(difference) && isEmpty(difference)) return acc; // return no diff + if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc; // return no diff return { ...acc, [key]: difference }; // return updated key }, deletedValues); diff --git a/src/diff/index.spec.js b/src/diff/index.spec.js index a1946da..a301fd7 100644 --- a/src/diff/index.spec.js +++ b/src/diff/index.spec.js @@ -16,6 +16,7 @@ describe('.diff', () => { ['object', { a: 1 }], ['array', [1]], ['function', () => ({})], + ['date', new Date()], ]).it('returns empty object when given values of type %s are equal', (type, value) => { expect(diff(value, value)).to.deep.equal({}); }); @@ -34,6 +35,7 @@ describe('.diff', () => { ['872983', { areaCode: '+44', number: '872983' }], [100, () => ({})], [() => ({}), 100], + [new Date('2017-01-01'), new Date('2017-01-02')], ]).it('returns right hand side value when different to left hand side value (%s, %s)', (lhs, rhs) => { expect(diff(lhs, rhs)).to.deep.equal(rhs); }); @@ -92,5 +94,24 @@ describe('.diff', () => { expect(diff([1, 2, 3], [1, 2, 3, 9])).to.deep.equal({ 3: 9 }); }); }); + + describe('date', () => { + const lhs = new Date('2016'); + const rhs = new Date('2017'); + it('returns right hand side date when updated', () => { + expect(diff({ date: lhs }, { date: rhs })).to.deep.equal({ date: rhs }); + expect(diff([lhs], [rhs])).to.deep.equal({ 0: rhs }); + }); + + it('returns undefined when date deleted', () => { + expect(diff({ date: lhs }, {})).to.deep.equal({ date: undefined }); + expect(diff([lhs], [])).to.deep.equal({ 0: undefined }); + }); + + it('returns right hand side when date is added', () => { + expect(diff({}, { date: rhs })).to.deep.equal({ date: rhs }); + expect(diff([], [rhs])).to.deep.equal({ 0: rhs }); + }); + }); }); }); diff --git a/src/updated/index.js b/src/updated/index.js index 9deacef..c98acd2 100644 --- a/src/updated/index.js +++ b/src/updated/index.js @@ -1,4 +1,4 @@ -import { isEmpty, isObject } from '../utils'; +import { isDate, isEmpty, isObject } from '../utils'; const updatedDiff = (lhs, rhs) => { @@ -6,12 +6,17 @@ const updatedDiff = (lhs, rhs) => { if (!isObject(lhs) || !isObject(rhs)) return rhs; + if (isDate(lhs) || isDate(rhs)) { + if (lhs.toString() == rhs.toString()) return {}; + return rhs; + } + return Object.keys(rhs).reduce((acc, key) => { if (lhs.hasOwnProperty(key)) { const difference = updatedDiff(lhs[key], rhs[key]); - if (isObject(difference) && isEmpty(difference)) return acc; + if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc; return { ...acc, [key]: difference }; } diff --git a/src/updated/index.spec.js b/src/updated/index.spec.js index f01fcce..0bd7a68 100644 --- a/src/updated/index.spec.js +++ b/src/updated/index.spec.js @@ -16,6 +16,7 @@ describe('.updatedDiff', () => { ['object', { a: 1 }], ['array', [1]], ['function', () => ({})], + ['date', new Date()], ]).it('returns empty object when given values of type %s are equal', (type, value) => { expect(updatedDiff(value, value)).to.deep.equal({}); }); @@ -34,6 +35,7 @@ describe('.updatedDiff', () => { ['872983', { areaCode: '+44', number: '872983' }], [100, () => ({})], [() => ({}), 100], + [new Date('2017-01-01'), new Date('2017-01-02')], ]).it('returns right hand side value when different to left hand side value (%s, %s)', (lhs, rhs) => { expect(updatedDiff(lhs, rhs)).to.deep.equal(rhs); }); @@ -69,6 +71,10 @@ describe('.updatedDiff', () => { it('returns empty object when a key value has been added', () => { expect(updatedDiff({ a: 1 }, { a: 1, b: 2 })).to.deep.equal({}); }); + + it('returns subset of right hand side with updated date', () => { + expect(updatedDiff({ date: new Date('2016') }, { date: new Date('2017') })).to.eql({ date: new Date('2017') }); + }); }); describe('arrays', () => { @@ -87,6 +93,10 @@ describe('.updatedDiff', () => { it('returns empty object when right hand side array has additions', () => { expect(updatedDiff([1, 2, 3], [1, 2, 3, 9])).to.deep.equal({}); }); + + it('returns subset of right hand side with updated date', () => { + expect(updatedDiff([new Date('2016')], [new Date('2017')])).to.eql({ 0: new Date('2017') }); + }); }); }); });