File tree Expand file tree Collapse file tree 2 files changed +34
-5
lines changed Expand file tree Collapse file tree 2 files changed +34
-5
lines changed Original file line number Diff line number Diff line change 1
- import { isEmpty , isObject } from '../utils' ;
1
+ import { isEmpty , isObject , properObject } from '../utils' ;
2
2
3
3
const addedDiff = ( lhs , rhs ) => {
4
4
5
5
if ( lhs === rhs || ! isObject ( lhs ) || ! isObject ( rhs ) ) return { } ;
6
6
7
- return Object . keys ( rhs ) . reduce ( ( acc , key ) => {
8
- if ( lhs . hasOwnProperty ( key ) ) {
9
- const difference = addedDiff ( lhs [ key ] , rhs [ key ] ) ;
7
+ const l = properObject ( lhs ) ;
8
+ const r = properObject ( rhs ) ;
9
+
10
+ return Object . keys ( r ) . reduce ( ( acc , key ) => {
11
+ if ( l . hasOwnProperty ( key ) ) {
12
+ const difference = addedDiff ( l [ key ] , r [ key ] ) ;
10
13
11
14
if ( isObject ( difference ) && isEmpty ( difference ) ) return acc ;
12
15
13
16
return { ...acc , [ key ] : difference } ;
14
17
}
15
18
16
- return { ...acc , [ key ] : rhs [ key ] } ;
19
+ return { ...acc , [ key ] : r [ key ] } ;
17
20
} , { } ) ;
18
21
} ;
19
22
Original file line number Diff line number Diff line change @@ -81,5 +81,31 @@ describe('.addedDiff', () => {
81
81
expect ( addedDiff ( [ ] , [ new Date ( '2016' ) ] ) ) . toEqual ( { 0 : new Date ( '2016' ) } ) ;
82
82
} ) ;
83
83
} ) ;
84
+
85
+ describe ( 'object create null' , ( ) => {
86
+ test ( 'returns subset of right hand side value when a key value has been added to the root' , ( ) => {
87
+ const lhs = Object . create ( null ) ;
88
+ const rhs = Object . create ( null ) ;
89
+ lhs . a = 1 ;
90
+ rhs . a = 1 ;
91
+ rhs . b = 2 ;
92
+ expect ( addedDiff ( lhs , rhs ) ) . toEqual ( { b : 2 } ) ;
93
+ } ) ;
94
+
95
+ test ( 'returns subset of right hand side value when a key value has been added deeply' , ( ) => {
96
+ const lhs = Object . create ( null ) ;
97
+ const rhs = Object . create ( null ) ;
98
+ lhs . a = { b : 1 } ;
99
+ rhs . a = { b : 1 , c : 2 } ;
100
+ expect ( addedDiff ( lhs , rhs ) ) . toEqual ( { a : { c : 2 } } ) ;
101
+ } ) ;
102
+
103
+ test ( 'returns subset of right hand side with added date' , ( ) => {
104
+ const lhs = Object . create ( null ) ;
105
+ const rhs = Object . create ( null ) ;
106
+ rhs . date = new Date ( '2016' ) ;
107
+ expect ( addedDiff ( lhs , rhs ) ) . toEqual ( { date : new Date ( '2016' ) } ) ;
108
+ } ) ;
109
+ } ) ;
84
110
} ) ;
85
111
} ) ;
You can’t perform that action at this time.
0 commit comments