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 ff1235e

Browse files
Fix bug that does not return updated diffs for empty objects
1 parent 0694e7d commit ff1235e

File tree

5 files changed

+28
-10
lines changed

5 files changed

+28
-10
lines changed

‎src/diff/index.js

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

33
const diff = (lhs, rhs) => {
44
if (lhs === rhs) return {}; // equal return no diff
@@ -22,7 +22,9 @@ const diff = (lhs, rhs) => {
2222

2323
const difference = diff(l[key], r[key]);
2424

25-
if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc; // return no diff
25+
// If the difference is empty, and the lhs is an empty object or the rhs is not an empty object
26+
if (isEmptyObject(difference) && !isDate(difference) && (isEmptyObject(l[key]) || !isEmptyObject(r[key])))
27+
return acc; // return no diff
2628

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

‎src/diff/index.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ describe('.diff', () => {
4343

4444
describe('recursive case', () => {
4545
describe('object', () => {
46+
test("return right hand side empty object value when left hand side has been updated", () => {
47+
expect(diff({ a: 1 }, { a: {} })).toEqual({ a: {} });
48+
});
49+
4650
test('returns right hand side value when given objects are different', () => {
4751
expect(diff({ a: 1 }, { a: 2 })).toEqual({ a: 2 });
4852
});
@@ -77,6 +81,9 @@ describe('.diff', () => {
7781
});
7882

7983
describe('arrays', () => {
84+
test("return right hand side empty object value when left hand side has been updated", () => {
85+
expect(diff([{ a: 1 }], [{ a: {} }])).toEqual({ 0: { a: {} } });
86+
});
8087
test('returns right hand side value as object of indices to value when arrays are different', () => {
8188
expect(diff([1], [2])).toEqual({ 0: 2 });
8289
});

‎src/updated/index.js

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

33
const updatedDiff = (lhs, rhs) => {
4-
54
if (lhs === rhs) return {};
65

76
if (!isObject(lhs) || !isObject(rhs)) return rhs;
@@ -15,11 +14,12 @@ const updatedDiff = (lhs, rhs) => {
1514
}
1615

1716
return Object.keys(r).reduce((acc, key) => {
18-
1917
if (l.hasOwnProperty(key)) {
2018
const difference = updatedDiff(l[key], r[key]);
2119

22-
if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc;
20+
// If the difference is empty, and the lhs is an empty object or the rhs is not an empty object
21+
if (isEmptyObject(difference) && !isDate(difference) && (isEmptyObject(l[key]) || !isEmptyObject(r[key])))
22+
return acc; // return no diff
2323

2424
return { ...acc, [key]: difference };
2525
}

‎src/updated/index.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ describe('.updatedDiff', () => {
4343

4444
describe('recursive case', () => {
4545
describe('object', () => {
46+
test("return right hand side empty object value when left hand side has been updated", () => {
47+
expect(updatedDiff({ a: 1 }, { a: {} })).toEqual({ a: {} });
48+
});
49+
4650
test('returns right hand side value when given objects are different at root', () => {
4751
expect(updatedDiff({ a: 1 }, { a: 2 })).toEqual({ a: 2 });
4852
});
@@ -77,6 +81,10 @@ describe('.updatedDiff', () => {
7781
});
7882

7983
describe('arrays', () => {
84+
test("return right hand side empty object value when left hand side has been updated", () => {
85+
expect(updatedDiff([{ a: 1 }], [{ a: {} }])).toEqual({ 0: { a: {} } });
86+
});
87+
8088
test('returns right hand side value as object of indices to value when arrays are different', () => {
8189
expect(updatedDiff([1], [2])).toEqual({ 0: 2 });
8290
});

‎src/utils/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
export const isDate = d => d instanceof Date;
2-
export const isEmpty = o => Object.keys(o).length === 0;
3-
export const isObject = o => o != null && typeof o === 'object';
4-
export const properObject = o => isObject(o) && !o.hasOwnProperty ? { ...o } : o;
1+
export const isDate = (d) => d instanceof Date;
2+
export const isEmpty = (o) => Object.keys(o).length === 0;
3+
export const isObject = (o) => o != null && typeof o === "object";
4+
export const properObject = (o) => (isObject(o) && !o.hasOwnProperty ? { ...o } : o);
5+
export const isEmptyObject = (o) => isObject(o) && isEmpty(o);

0 commit comments

Comments
(0)

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