File tree 5 files changed +28
-10
lines changed
5 files changed +28
-10
lines changed Original file line number Diff line number Diff line change 1
- import { isDate , isEmpty , isObject , properObject } from ' ../utils' ;
1
+ import { isDate , isEmptyObject , isObject , properObject } from " ../utils" ;
2
2
3
3
const diff = ( lhs , rhs ) => {
4
4
if ( lhs === rhs ) return { } ; // equal return no diff
@@ -22,7 +22,9 @@ const diff = (lhs, rhs) => {
22
22
23
23
const difference = diff ( l [ key ] , r [ key ] ) ;
24
24
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
26
28
27
29
return { ...acc , [ key ] : difference } ; // return updated key
28
30
} , deletedValues ) ;
Original file line number Diff line number Diff line change @@ -43,6 +43,10 @@ describe('.diff', () => {
43
43
44
44
describe ( 'recursive case' , ( ) => {
45
45
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
+
46
50
test ( 'returns right hand side value when given objects are different' , ( ) => {
47
51
expect ( diff ( { a : 1 } , { a : 2 } ) ) . toEqual ( { a : 2 } ) ;
48
52
} ) ;
@@ -77,6 +81,9 @@ describe('.diff', () => {
77
81
} ) ;
78
82
79
83
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
+ } ) ;
80
87
test ( 'returns right hand side value as object of indices to value when arrays are different' , ( ) => {
81
88
expect ( diff ( [ 1 ] , [ 2 ] ) ) . toEqual ( { 0 : 2 } ) ;
82
89
} ) ;
Original file line number Diff line number Diff line change 1
- import { isDate , isEmpty , isObject , properObject } from ' ../utils' ;
1
+ import { isDate , isEmptyObject , isObject , properObject } from " ../utils" ;
2
2
3
3
const updatedDiff = ( lhs , rhs ) => {
4
-
5
4
if ( lhs === rhs ) return { } ;
6
5
7
6
if ( ! isObject ( lhs ) || ! isObject ( rhs ) ) return rhs ;
@@ -15,11 +14,12 @@ const updatedDiff = (lhs, rhs) => {
15
14
}
16
15
17
16
return Object . keys ( r ) . reduce ( ( acc , key ) => {
18
-
19
17
if ( l . hasOwnProperty ( key ) ) {
20
18
const difference = updatedDiff ( l [ key ] , r [ key ] ) ;
21
19
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
23
23
24
24
return { ...acc , [ key ] : difference } ;
25
25
}
Original file line number Diff line number Diff line change @@ -43,6 +43,10 @@ describe('.updatedDiff', () => {
43
43
44
44
describe ( 'recursive case' , ( ) => {
45
45
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
+
46
50
test ( 'returns right hand side value when given objects are different at root' , ( ) => {
47
51
expect ( updatedDiff ( { a : 1 } , { a : 2 } ) ) . toEqual ( { a : 2 } ) ;
48
52
} ) ;
@@ -77,6 +81,10 @@ describe('.updatedDiff', () => {
77
81
} ) ;
78
82
79
83
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
+
80
88
test ( 'returns right hand side value as object of indices to value when arrays are different' , ( ) => {
81
89
expect ( updatedDiff ( [ 1 ] , [ 2 ] ) ) . toEqual ( { 0 : 2 } ) ;
82
90
} ) ;
Original file line number Diff line number Diff line change 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 ) ;
You can’t perform that action at this time.
0 commit comments