Skip to content

Commit 01a3738

Browse files
committed
Add proper object conversion to updated diff
1 parent 142420f commit 01a3738

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

src/updated/index.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
import { isDate, isEmpty, isObject } from '../utils';
1+
import { isDate, isEmpty, isObject, properObject } 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;
9+
const l = properObject(lhs);
10+
const r = properObject(rhs);
11+
12+
if (isDate(l) || isDate(r)) {
13+
if (l.toString() == r.toString()) return {};
14+
return r;
1215
}
1316

14-
return Object.keys(rhs).reduce((acc, key) => {
17+
return Object.keys(r).reduce((acc, key) => {
1518

16-
if (lhs.hasOwnProperty(key)) {
17-
const difference = updatedDiff(lhs[key], rhs[key]);
19+
if (l.hasOwnProperty(key)) {
20+
const difference = updatedDiff(l[key], r[key]);
1821

1922
if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc;
2023

src/updated/index.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,41 @@ describe('.updatedDiff', () => {
103103
expect(updatedDiff(new Date('2016'), new Date('2016'))).toEqual({});
104104
});
105105
});
106+
107+
describe('object create null', () => {
108+
test('returns right hand side value when given objects are different at root', () => {
109+
const lhs = Object.create(null);
110+
lhs.a = 1;
111+
const rhs = Object.create(null);
112+
rhs.a = 2;
113+
expect(updatedDiff(lhs, rhs)).toEqual({ a: 2 });
114+
});
115+
116+
test('returns subset of right hand side value when sibling objects differ', () => {
117+
const lhs = Object.create(null);
118+
lhs.a = { b: 1 };
119+
lhs.c = 2;
120+
const rhs = Object.create(null);
121+
rhs.a = { b: 1 };
122+
rhs.c = 3;
123+
expect(updatedDiff(lhs, rhs)).toEqual({ c: 3 });
124+
});
125+
126+
test('returns subset of right hand side value when nested values differ', () => {
127+
const lhs = Object.create(null);
128+
lhs.a = { b: 1, c: 2 };
129+
const rhs = Object.create(null);
130+
rhs.a = { b: 1, c: 3 };
131+
expect(updatedDiff(lhs, rhs)).toEqual({ a: { c: 3 } });
132+
});
133+
134+
test('returns subset of right hand side with updated date', () => {
135+
const lhs = Object.create(null);
136+
lhs.date = new Date('2016');
137+
const rhs = Object.create(null);
138+
rhs.date = new Date('2017');
139+
expect(updatedDiff(lhs, rhs)).toEqual({ date: new Date('2017') });
140+
});
141+
});
106142
});
107143
});

0 commit comments

Comments
 (0)