Skip to content

Commit ea127fc

Browse files
committed
fix styles and display issues
1 parent b93bccd commit ea127fc

File tree

2 files changed

+80
-30
lines changed

2 files changed

+80
-30
lines changed

script.js

Lines changed: 76 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,20 @@ function displayControllers(state, otherState, outputElement, column) {
9898
title.prepend(icon);
9999

100100
const content = document.createElement('pre');
101+
102+
// Always use the current state's data for display
101103
const controllerData = state.data[controller];
102104
const otherControllerData = otherState.data ? otherState.data[controller] : undefined;
103105

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+
);
105112

106113
if (JSON.stringify(controllerData) !== JSON.stringify(otherControllerData)) {
107-
title.classList.add('updated'); // Highlight the title if there are differences
114+
title.classList.add('updated');
108115
}
109116

110117
content.classList.add('controller-content', 'hidden');
@@ -133,32 +140,65 @@ function toggleController(controller) {
133140
}
134141

135142
function highlightDifferences(obj1, obj2, column) {
143+
if (!obj1 || !obj2) {
144+
return JSON.stringify(column === 'B' ? obj2 : obj1, null, 2);
145+
}
136146
const diff = compareObjects(obj1, obj2, column);
137147
return formatDiff(diff);
138148
}
139149

140150
function compareObjects(obj1, obj2, column) {
141151
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+
};
160196
}
161197
}
198+
// Key only in current object
199+
else if (key in (obj1 || {})) {
200+
result[key] = { value: val1, type: 'updated' };
201+
}
162202
}
163203

164204
return result;
@@ -168,17 +208,27 @@ function formatDiff(diff) {
168208
const formatted = [];
169209

170210
for (const key in diff) {
171-
const value = diff[key].value;
172-
const type = diff[key].type;
211+
const { value, type, oldValue } = diff[key];
173212
let formattedValue;
174213

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+
}
177226
} else {
178-
formattedValue = JSON.stringify(value, null, 2);
227+
formattedValue = JSON.stringify(value);
179228
}
180229

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>`);
182232
}
183233

184234
return formatted.join('');

styles.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ button {
9191
display: none;
9292
}
9393

94-
.extra {
95-
background-color: #97EAD2;
96-
}
97-
9894
.updated {
9995
background-color: #fff95b;
10096
}
10197

10298
.equal {
10399
background-color: transparent;
100+
}
101+
102+
.controller-content div {
103+
margin-left: 20px;
104104
}

0 commit comments

Comments
 (0)