Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 142420f

Browse files
Add proper object conversion to deleted diff
1 parent 8f98d7f commit 142420f

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

‎src/deleted/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { isEmpty, isObject } from '../utils';
1+
import { isEmpty, isObject,properObject } from '../utils';
22

33
const deletedDiff = (lhs, rhs) => {
44
if (lhs === rhs || !isObject(lhs) || !isObject(rhs)) return {};
55

6-
return Object.keys(lhs).reduce((acc, key) => {
7-
if (rhs.hasOwnProperty(key)) {
8-
const difference = deletedDiff(lhs[key], rhs[key]);
6+
const l = properObject(lhs);
7+
const r = properObject(rhs);
8+
9+
return Object.keys(l).reduce((acc, key) => {
10+
if (r.hasOwnProperty(key)) {
11+
const difference = deletedDiff(l[key], r[key]);
912

1013
if (isObject(difference) && isEmpty(difference)) return acc;
1114

‎src/deleted/index.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,33 @@ describe('.deletedDiff', () => {
8181
expect(deletedDiff([new Date('2016')], [])).toEqual({ 0: undefined });
8282
});
8383
});
84+
85+
describe('object create null', () => {
86+
test('returns keys as undefined when deleted from right hand side root', () => {
87+
const lhs = Object.create(null);
88+
const rhs = Object.create(null);
89+
lhs.a = 1;
90+
lhs.b = 2;
91+
rhs.a = 1;
92+
expect(deletedDiff(lhs, rhs)).toEqual({ b: undefined });
93+
});
94+
95+
test('returns keys as undefined when deeply deleted from right hand side', () => {
96+
const lhs = Object.create(null);
97+
const rhs = Object.create(null);
98+
lhs.a = { b: 1 };
99+
lhs.c = { d: 100 };
100+
rhs.a = { b: 1 };
101+
rhs.c = {};
102+
expect(deletedDiff(lhs, rhs)).toEqual({ c: { d: undefined } });
103+
});
104+
105+
test('returns subset of right hand side with deleted date', () => {
106+
const lhs = Object.create(null);
107+
const rhs = Object.create(null);
108+
lhs.date = new Date('2016');
109+
expect(deletedDiff({ date: new Date('2016') }, rhs)).toEqual({ date: undefined });
110+
});
111+
});
84112
});
85113
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /