Skip to content

Commit

Permalink
Add support for date comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
mattphillips committed Feb 14, 2017
1 parent cc4b0e5 commit 39ebda0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/added/index.spec.js
Expand Up @@ -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({});
});
Expand All @@ -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({});
});
Expand All @@ -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', () => {
Expand All @@ -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') });
});
});
});
});
10 changes: 10 additions & 0 deletions src/deleted/index.spec.js
Expand Up @@ -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({});
});
Expand All @@ -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({});
});
Expand All @@ -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', () => {
Expand All @@ -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 });
});
});
});
});
9 changes: 7 additions & 2 deletions 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
Expand All @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions src/diff/index.spec.js
Expand Up @@ -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({});
});
Expand All @@ -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);
});
Expand Down Expand Up @@ -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 });
});
});
});
});
9 changes: 7 additions & 2 deletions src/updated/index.js
@@ -1,17 +1,22 @@
import { isEmpty, isObject } from '../utils';
import { isDate, isEmpty, isObject } from '../utils';

const updatedDiff = (lhs, rhs) => {

if (lhs === rhs) return {};

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 };
}
Expand Down
10 changes: 10 additions & 0 deletions src/updated/index.spec.js
Expand Up @@ -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({});
});
Expand All @@ -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);
});
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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') });
});
});
});
});

0 comments on commit 39ebda0

Please sign in to comment.