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' ;
22
33const addedDiff = ( lhs , rhs ) => {
44
55 if ( lhs === rhs || ! isObject ( lhs ) || ! isObject ( rhs ) ) return { } ;
66
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 ] ) ;
1013
1114 if ( isObject ( difference ) && isEmpty ( difference ) ) return acc ;
1215
1316 return { ...acc , [ key ] : difference } ;
1417 }
1518
16- return { ...acc , [ key ] : rhs [ key ] } ;
19+ return { ...acc , [ key ] : r [ key ] } ;
1720 } , { } ) ;
1821} ;
1922
Original file line number Diff line number Diff line change @@ -81,5 +81,31 @@ describe('.addedDiff', () => {
8181 expect ( addedDiff ( [ ] , [ new Date ( '2016' ) ] ) ) . toEqual ( { 0 : new Date ( '2016' ) } ) ;
8282 } ) ;
8383 } ) ;
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+ } ) ;
84110 } ) ;
85111} ) ;
You can’t perform that action at this time.
0 commit comments