@@ -98,13 +98,20 @@ function displayControllers(state, otherState, outputElement, column) {
98
98
title . prepend ( icon ) ;
99
99
100
100
const content = document . createElement ( 'pre' ) ;
101
+
102
+ // Always use the current state's data for display
101
103
const controllerData = state . data [ controller ] ;
102
104
const otherControllerData = otherState . data ? otherState . data [ controller ] : undefined ;
103
105
104
- content . innerHTML = highlightDifferences ( controllerData , otherControllerData , column ) ;
106
+ // Pass the states in the correct order for comparison
107
+ content . innerHTML = highlightDifferences (
108
+ controllerData , // current state's data
109
+ otherControllerData , // other state's data for comparison
110
+ column
111
+ ) ;
105
112
106
113
if ( JSON . stringify ( controllerData ) !== JSON . stringify ( otherControllerData ) ) {
107
- title . classList . add ( 'updated' ) ; // Highlight the title if there are differences
114
+ title . classList . add ( 'updated' ) ;
108
115
}
109
116
110
117
content . classList . add ( 'controller-content' , 'hidden' ) ;
@@ -133,32 +140,65 @@ function toggleController(controller) {
133
140
}
134
141
135
142
function highlightDifferences ( obj1 , obj2 , column ) {
143
+ if ( ! obj1 || ! obj2 ) {
144
+ return JSON . stringify ( column === 'B' ? obj2 : obj1 , null , 2 ) ;
145
+ }
136
146
const diff = compareObjects ( obj1 , obj2 , column ) ;
137
147
return formatDiff ( diff ) ;
138
148
}
139
149
140
150
function compareObjects ( obj1 , obj2 , column ) {
141
151
const result = { } ;
142
-
143
- for ( const key in obj1 ) {
144
- if ( ! ( key in obj2 ) ) {
145
- result [ key ] = { value : obj1 [ key ] , type : 'extra' } ;
146
- } else if ( typeof obj1 [ key ] === 'object' && obj1 [ key ] !== null && typeof obj2 [ key ] === 'object' && obj2 [ key ] !== null ) {
147
- const nestedDiff = compareObjects ( obj1 [ key ] , obj2 [ key ] , column ) ;
148
- result [ key ] = { value : nestedDiff , type : Object . keys ( nestedDiff ) . some ( k => nestedDiff [ k ] . type !== 'equal' ) ? 'updated' : 'equal' } ;
149
- } else if ( JSON . stringify ( obj1 [ key ] ) !== JSON . stringify ( obj2 [ key ] ) ) {
150
- result [ key ] = { value : obj1 [ key ] , type : 'updated' } ;
151
- } else {
152
- result [ key ] = { value : obj1 [ key ] , type : 'equal' } ;
153
- }
154
- }
155
-
156
- if ( column === 'A' ) {
157
- for ( const key in obj2 ) {
158
- if ( ! ( key in obj1 ) ) {
159
- result [ key ] = { value : obj2 [ key ] , type : 'extra' } ;
152
+ const allKeys = new Set ( [ ...Object . keys ( obj1 || { } ) , ...Object . keys ( obj2 || { } ) ] ) ;
153
+
154
+ for ( const key of allKeys ) {
155
+ const val1 = obj1 ?. [ key ] ;
156
+ const val2 = obj2 ?. [ key ] ;
157
+
158
+ // Always use the first parameter's value (obj1)
159
+ const currentValue = val1 ;
160
+
161
+ // Key exists in both objects
162
+ if ( key in ( obj1 || { } ) && key in ( obj2 || { } ) ) {
163
+ if ( typeof val1 === 'object' && val1 !== null && typeof val2 === 'object' && val2 !== null ) {
164
+ // For arrays, handle them directly
165
+ if ( Array . isArray ( val1 ) || Array . isArray ( val2 ) ) {
166
+ if ( JSON . stringify ( val1 ) !== JSON . stringify ( val2 ) ) {
167
+ result [ key ] = {
168
+ value : currentValue ,
169
+ type : 'updated'
170
+ } ;
171
+ } else {
172
+ result [ key ] = {
173
+ value : currentValue ,
174
+ type : 'equal'
175
+ } ;
176
+ }
177
+ } else {
178
+ // For objects, recurse
179
+ const nestedDiff = compareObjects ( val1 , val2 , column ) ;
180
+ if ( Object . keys ( nestedDiff ) . length > 0 ) {
181
+ result [ key ] = { value : nestedDiff , type : 'nested' } ;
182
+ } else {
183
+ result [ key ] = { value : currentValue , type : 'equal' } ;
184
+ }
185
+ }
186
+ } else if ( JSON . stringify ( val1 ) !== JSON . stringify ( val2 ) ) {
187
+ result [ key ] = {
188
+ value : currentValue ,
189
+ type : 'updated'
190
+ } ;
191
+ } else {
192
+ result [ key ] = {
193
+ value : currentValue ,
194
+ type : 'equal'
195
+ } ;
160
196
}
161
197
}
198
+ // Key only in current object
199
+ else if ( key in ( obj1 || { } ) ) {
200
+ result [ key ] = { value : val1 , type : 'updated' } ;
201
+ }
162
202
}
163
203
164
204
return result ;
@@ -168,17 +208,27 @@ function formatDiff(diff) {
168
208
const formatted = [ ] ;
169
209
170
210
for ( const key in diff ) {
171
- const value = diff [ key ] . value ;
172
- const type = diff [ key ] . type ;
211
+ const { value, type, oldValue } = diff [ key ] ;
173
212
let formattedValue ;
174
213
175
- if ( typeof value === 'object' && value !== null ) {
176
- formattedValue = Object . keys ( value ) . length === 0 ? ( Array . isArray ( value ) ? '[]' : '{}' ) : formatDiff ( value ) ;
214
+ if ( value === null ) {
215
+ formattedValue = 'null' ;
216
+ } else if ( typeof value === 'object' && type === 'nested' ) {
217
+ formattedValue = formatDiff ( value ) ;
218
+ } else if ( typeof value === 'object' ) {
219
+ if ( Array . isArray ( value ) ) {
220
+ formattedValue = `[${ value . map ( v => JSON . stringify ( v ) ) . join ( ', ' ) } ]` ;
221
+ } else if ( Object . keys ( value ) . length === 0 ) {
222
+ formattedValue = '{}' ;
223
+ } else {
224
+ formattedValue = JSON . stringify ( value , null , 2 ) ;
225
+ }
177
226
} else {
178
- formattedValue = JSON . stringify ( value , null , 2 ) ;
227
+ formattedValue = JSON . stringify ( value ) ;
179
228
}
180
229
181
- formatted . push ( `<div class="${ type } ">${ key } : ${ formattedValue } </div>` ) ;
230
+ const className = type === 'updated' ? 'updated' : type === 'equal' ? 'equal' : '' ;
231
+ formatted . push ( `<div class="${ className } ">${ key } : ${ formattedValue } </div>` ) ;
182
232
}
183
233
184
234
return formatted . join ( '' ) ;
0 commit comments