Skip to content

Commit 39ebda0

Browse files
committed
Add support for date comparisons
1 parent cc4b0e5 commit 39ebda0

File tree

6 files changed

+65
-4
lines changed

6 files changed

+65
-4
lines changed

src/added/index.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('.addedDiff', () => {
1616
['object', { a: 1 }],
1717
['array', [1]],
1818
['function', () => ({})],
19+
['date', new Date()],
1920
]).it('returns empty object when given values of type %s are equal', (type, value) => {
2021
expect(addedDiff(value, value)).to.deep.equal({});
2122
});
@@ -34,6 +35,7 @@ describe('.addedDiff', () => {
3435
['872983', { areaCode: '+44', number: '872983' }],
3536
[100, () => ({})],
3637
[() => ({}), 100],
38+
[new Date('2017-01-01'), new Date('2017-01-02')],
3739
]).it('returns empty object when values are not equal (%s, %s)', (lhs, rhs) => {
3840
expect(addedDiff(lhs, rhs)).to.deep.equal({});
3941
});
@@ -57,6 +59,10 @@ describe('.addedDiff', () => {
5759
it('returns subset of right hand side value when a key value has been added deeply', () => {
5860
expect(addedDiff({ a: { b: 1} }, { a: { b: 1, c: 2 } })).to.deep.equal({ a: { c: 2 } });
5961
});
62+
63+
it('returns subset of right hand side with added date', () => {
64+
expect(addedDiff({}, { date: new Date('2016') })).to.eql({ date: new Date('2016') });
65+
});
6066
});
6167

6268
describe('arrays', () => {
@@ -71,6 +77,10 @@ describe('.addedDiff', () => {
7177
it('returns subset of right hand side array as object of indices to value when right hand side array has additions', () => {
7278
expect(addedDiff([1, 2, 3], [1, 2, 3, 9])).to.deep.equal({ 3: 9 });
7379
});
80+
81+
it('returns subset of right hand side with added date', () => {
82+
expect(addedDiff([], [new Date('2016')])).to.eql({ 0: new Date('2016') });
83+
});
7484
});
7585
});
7686
});

src/deleted/index.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('.deletedDiff', () => {
1616
['object', { a: 1 }],
1717
['array', [1]],
1818
['function', () => ({})],
19+
['date', new Date()],
1920
]).it('returns empty object when given values of type %s are equal', (type, value) => {
2021
expect(deletedDiff(value, value)).to.deep.equal({});
2122
});
@@ -34,6 +35,7 @@ describe('.deletedDiff', () => {
3435
['872983', { areaCode: '+44', number: '872983' }],
3536
[100, () => ({})],
3637
[() => ({}), 100],
38+
[new Date('2017-01-01'), new Date('2017-01-02')],
3739
]).it('returns empty object when given values are unequal', (lhs, rhs) => {
3840
expect(deletedDiff(lhs, rhs)).to.deep.equal({});
3941
});
@@ -57,6 +59,10 @@ describe('.deletedDiff', () => {
5759
it('returns keys as undefined when deeply deleted from right hand side', () => {
5860
expect(deletedDiff({ a: { b: 1 }, c: 2, d: { e: 100 } }, { a: { b: 1 }, c: 2, d: {} })).to.deep.equal({ d: { e: undefined } });
5961
});
62+
63+
it('returns subset of right hand side with deleted date', () => {
64+
expect(deletedDiff({ date: new Date('2016') }, {})).to.eql({ date: undefined });
65+
});
6066
});
6167

6268
describe('arrays', () => {
@@ -71,6 +77,10 @@ describe('.deletedDiff', () => {
7177
it('returns subset of right hand side array as object of indices to value when right hand side array has deletions', () => {
7278
expect(deletedDiff([1, 2, 3], [1, 3])).to.deep.equal({ 2: undefined });
7379
});
80+
81+
it('returns subset of right hand side with added date', () => {
82+
expect(deletedDiff([new Date('2016')], [])).to.eql({ 0: undefined });
83+
});
7484
});
7585
});
7686
});

src/diff/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isEmpty, isObject } from '../utils';
1+
import { isDate, isEmpty, isObject } from '../utils';
22

33
const diff = (lhs, rhs) => {
44
if (lhs === rhs) return {}; // equal return no diff
@@ -9,12 +9,17 @@ const diff = (lhs, rhs) => {
99
return rhs.hasOwnProperty(key) ? acc : { ...acc, [key]: undefined };
1010
}, {});
1111

12+
if (isDate(lhs) || isDate(rhs)) {
13+
if (lhs.toString() == rhs.toString()) return {};
14+
return rhs;
15+
}
16+
1217
return Object.keys(rhs).reduce((acc, key) => {
1318
if (!lhs.hasOwnProperty(key)) return { ...acc, [key]: rhs[key] }; // return added rhs key
1419

1520
const difference = diff(lhs[key], rhs[key]);
1621

17-
if (isObject(difference) && isEmpty(difference)) return acc; // return no diff
22+
if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc; // return no diff
1823

1924
return { ...acc, [key]: difference }; // return updated key
2025
}, deletedValues);

src/diff/index.spec.js

+21
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('.diff', () => {
1616
['object', { a: 1 }],
1717
['array', [1]],
1818
['function', () => ({})],
19+
['date', new Date()],
1920
]).it('returns empty object when given values of type %s are equal', (type, value) => {
2021
expect(diff(value, value)).to.deep.equal({});
2122
});
@@ -34,6 +35,7 @@ describe('.diff', () => {
3435
['872983', { areaCode: '+44', number: '872983' }],
3536
[100, () => ({})],
3637
[() => ({}), 100],
38+
[new Date('2017-01-01'), new Date('2017-01-02')],
3739
]).it('returns right hand side value when different to left hand side value (%s, %s)', (lhs, rhs) => {
3840
expect(diff(lhs, rhs)).to.deep.equal(rhs);
3941
});
@@ -92,5 +94,24 @@ describe('.diff', () => {
9294
expect(diff([1, 2, 3], [1, 2, 3, 9])).to.deep.equal({ 3: 9 });
9395
});
9496
});
97+
98+
describe('date', () => {
99+
const lhs = new Date('2016');
100+
const rhs = new Date('2017');
101+
it('returns right hand side date when updated', () => {
102+
expect(diff({ date: lhs }, { date: rhs })).to.deep.equal({ date: rhs });
103+
expect(diff([lhs], [rhs])).to.deep.equal({ 0: rhs });
104+
});
105+
106+
it('returns undefined when date deleted', () => {
107+
expect(diff({ date: lhs }, {})).to.deep.equal({ date: undefined });
108+
expect(diff([lhs], [])).to.deep.equal({ 0: undefined });
109+
});
110+
111+
it('returns right hand side when date is added', () => {
112+
expect(diff({}, { date: rhs })).to.deep.equal({ date: rhs });
113+
expect(diff([], [rhs])).to.deep.equal({ 0: rhs });
114+
});
115+
});
95116
});
96117
});

src/updated/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
import { isEmpty, isObject } from '../utils';
1+
import { isDate, isEmpty, isObject } from '../utils';
22

33
const updatedDiff = (lhs, rhs) => {
44

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

77
if (!isObject(lhs) || !isObject(rhs)) return rhs;
88

9+
if (isDate(lhs) || isDate(rhs)) {
10+
if (lhs.toString() == rhs.toString()) return {};
11+
return rhs;
12+
}
13+
914
return Object.keys(rhs).reduce((acc, key) => {
1015

1116
if (lhs.hasOwnProperty(key)) {
1217
const difference = updatedDiff(lhs[key], rhs[key]);
1318

14-
if (isObject(difference) && isEmpty(difference)) return acc;
19+
if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc;
1520

1621
return { ...acc, [key]: difference };
1722
}

src/updated/index.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('.updatedDiff', () => {
1616
['object', { a: 1 }],
1717
['array', [1]],
1818
['function', () => ({})],
19+
['date', new Date()],
1920
]).it('returns empty object when given values of type %s are equal', (type, value) => {
2021
expect(updatedDiff(value, value)).to.deep.equal({});
2122
});
@@ -34,6 +35,7 @@ describe('.updatedDiff', () => {
3435
['872983', { areaCode: '+44', number: '872983' }],
3536
[100, () => ({})],
3637
[() => ({}), 100],
38+
[new Date('2017-01-01'), new Date('2017-01-02')],
3739
]).it('returns right hand side value when different to left hand side value (%s, %s)', (lhs, rhs) => {
3840
expect(updatedDiff(lhs, rhs)).to.deep.equal(rhs);
3941
});
@@ -69,6 +71,10 @@ describe('.updatedDiff', () => {
6971
it('returns empty object when a key value has been added', () => {
7072
expect(updatedDiff({ a: 1 }, { a: 1, b: 2 })).to.deep.equal({});
7173
});
74+
75+
it('returns subset of right hand side with updated date', () => {
76+
expect(updatedDiff({ date: new Date('2016') }, { date: new Date('2017') })).to.eql({ date: new Date('2017') });
77+
});
7278
});
7379

7480
describe('arrays', () => {
@@ -87,6 +93,10 @@ describe('.updatedDiff', () => {
8793
it('returns empty object when right hand side array has additions', () => {
8894
expect(updatedDiff([1, 2, 3], [1, 2, 3, 9])).to.deep.equal({});
8995
});
96+
97+
it('returns subset of right hand side with updated date', () => {
98+
expect(updatedDiff([new Date('2016')], [new Date('2017')])).to.eql({ 0: new Date('2017') });
99+
});
90100
});
91101
});
92102
});

0 commit comments

Comments
 (0)