Skip to content

Commit b8e3576

Browse files
committed
Add proper object conversion to diff
1 parent 01a3738 commit b8e3576

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

src/diff/index.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
import { isDate, isEmpty, isObject } from '../utils';
1+
import { isDate, isEmpty, isObject, properObject } from '../utils';
22

33
const diff = (lhs, rhs) => {
44
if (lhs === rhs) return {}; // equal return no diff
55

66
if (!isObject(lhs) || !isObject(rhs)) return rhs; // return updated rhs
77

8-
const deletedValues = Object.keys(lhs).reduce((acc, key) => {
9-
return rhs.hasOwnProperty(key) ? acc : { ...acc, [key]: undefined };
8+
const l = properObject(lhs);
9+
const r = properObject(rhs);
10+
11+
const deletedValues = Object.keys(l).reduce((acc, key) => {
12+
return r.hasOwnProperty(key) ? acc : { ...acc, [key]: undefined };
1013
}, {});
1114

12-
if (isDate(lhs) || isDate(rhs)) {
13-
if (lhs.toString() == rhs.toString()) return {};
14-
return rhs;
15+
if (isDate(l) || isDate(r)) {
16+
if (l.toString() == r.toString()) return {};
17+
return r;
1518
}
1619

17-
return Object.keys(rhs).reduce((acc, key) => {
18-
if (!lhs.hasOwnProperty(key)) return { ...acc, [key]: rhs[key] }; // return added rhs key
20+
return Object.keys(r).reduce((acc, key) => {
21+
if (!l.hasOwnProperty(key)) return { ...acc, [key]: r[key] }; // return added r key
1922

20-
const difference = diff(lhs[key], rhs[key]);
23+
const difference = diff(l[key], r[key]);
2124

2225
if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc; // return no diff
2326

src/diff/index.test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,61 @@ describe('.diff', () => {
117117
expect(diff([], [rhs])).toEqual({ 0: rhs });
118118
});
119119
});
120+
121+
describe('object create null', () => {
122+
test('returns right hand side value when given objects are different', () => {
123+
const lhs = Object.create(null);
124+
lhs.a = 1;
125+
const rhs = Object.create(null);
126+
rhs.a = 2;
127+
expect(diff(lhs, rhs)).toEqual({ a: 2 });
128+
});
129+
130+
test('returns subset of right hand side value when sibling objects differ', () => {
131+
const lhs = Object.create(null);
132+
lhs.a = { b: 1 };
133+
lhs.c = 2;
134+
const rhs = Object.create(null);
135+
rhs.a = { b: 1 };
136+
rhs.c = 3;
137+
expect(diff(lhs, rhs)).toEqual({ c: 3 });
138+
});
139+
140+
test('returns subset of right hand side value when nested values differ', () => {
141+
const lhs = Object.create(null);
142+
lhs.a = { b: 1, c: 2};
143+
const rhs = Object.create(null);
144+
rhs.a = { b: 1, c: 3 };
145+
expect(diff(lhs, rhs)).toEqual({ a: { c: 3 } });
146+
});
147+
148+
test('returns subset of right hand side value when nested values differ at multiple paths', () => {
149+
const lhs = Object.create(null);
150+
lhs.a = { b: 1 };
151+
lhs.c = 2;
152+
const rhs = Object.create(null);
153+
rhs.a = { b: 99 };
154+
rhs.c = 3;
155+
expect(diff(lhs, rhs)).toEqual({ a: { b: 99 }, c: 3 });
156+
});
157+
158+
test('returns subset of right hand side value when a key value has been deleted', () => {
159+
const lhs = Object.create(null);
160+
lhs.a = { b: 1 };
161+
lhs.c = 2;
162+
const rhs = Object.create(null);
163+
rhs.a = { b: 1 };
164+
expect(diff(lhs, rhs)).toEqual({ c: undefined });
165+
});
166+
167+
test('returns subset of right hand side value when a key value has been added', () => {
168+
const lhs = Object.create(null);
169+
lhs.a = 1;
170+
const rhs = Object.create(null);
171+
rhs.a = 1;
172+
rhs.b = 2;
173+
expect(diff(lhs, rhs)).toEqual({ b: 2 });
174+
});
175+
});
120176
});
121177
});

0 commit comments

Comments
 (0)