Spaces:
Paused
Paused
File size: 8,235 Bytes
f55c62a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | export interface DiffItem {
path: string[];
type: 'CHANGE' | 'ADD' | 'REMOVE';
oldValue?: any;
newValue?: any;
}
function isObject(val: any) {
return val != null && typeof val === 'object' && !Array.isArray(val);
}
function isEmptyValue(val: any) {
return (
val == null ||
(Array.isArray(val) && val.length === 0) ||
(isObject(val) && Object.keys(val).length === 0)
);
}
export function sortKeys(obj: any): any {
if (Array.isArray(obj)) {
return obj.map(sortKeys);
}
if (isObject(obj)) {
return Object.keys(obj)
.sort()
.reduce((sortedObj: any, key) => {
sortedObj[key] = sortKeys(obj[key]);
return sortedObj;
}, {});
}
return obj;
}
export function getObjectDiff(
obj1: any,
obj2: any,
path: string[] = []
): DiffItem[] {
const diffs: DiffItem[] = [];
const ignoredKeys = new Set([
'uuid',
'trusted',
'encryptedPassword',
'showChanges',
'ip',
'accessToken',
]);
if (obj1 == null && obj2 == null) return diffs;
// Treat empty objects/arrays as equal to null/undefined to avoid ghost diffs
if (isEmptyValue(obj1) && isEmptyValue(obj2)) return diffs;
if (Array.isArray(obj1) && Array.isArray(obj2)) {
if (obj1 === obj2) return [];
try {
if (JSON.stringify(obj1) === JSON.stringify(obj2)) return [];
} catch (e) {
console.warn('Fast array comparison failed:', e);
}
const lastPath = path.length > 0 ? path[path.length - 1] : '';
const isPresets = lastPath === 'presets';
const isCatalogModifications = lastPath === 'catalogModifications';
const getKey = (item: any) => {
if (!isObject(item)) {
return String(item);
}
if (isCatalogModifications && item.id && item.type)
return `${item.id}_${item.type}`;
if (item.instanceId) return item.instanceId;
if (item.id) return item.id;
if (item.pattern) return item.pattern;
if (item.condition && Array.isArray(item.addons)) {
try {
return `grouping:${item.condition}:${JSON.stringify(item.addons)}`;
} catch {
return item.condition;
}
}
if (item.condition) return item.condition;
if (item.expression) return item.expression;
if (Array.isArray(item.addons)) {
try {
return `addons:${JSON.stringify(item.addons)}`;
} catch {
return null;
}
}
if (item.key) return item.key;
if (item.name) return item.name;
return null;
};
const keys1 = obj1.map(getKey);
const keys2 = obj2.map(getKey);
const canKey =
keys1.every((k) => k !== null) &&
keys2.every((k) => k !== null) &&
new Set(keys1).size === obj1.length &&
new Set(keys2).size === obj2.length;
if (canKey) {
const oldMap = new Map();
const oldOrder: any[] = [];
obj1.forEach((item: any) => {
const key = getKey(item);
oldMap.set(key, item);
oldOrder.push(key);
});
const newMap = new Map();
const newOrder: any[] = [];
obj2.forEach((item: any) => {
const key = getKey(item);
newMap.set(key, item);
newOrder.push(key);
});
const getLabel = (key: any) => {
const item = newMap.get(key) || oldMap.get(key);
if (item?.expression && item?.score !== undefined) {
return `${item.expression} (Score: ${item.score})`;
}
if (item?.addons && Array.isArray(item.addons)) {
const firstAddon = item.addons[0] || '';
const count = item.addons.length;
const summary = firstAddon
? ` (${firstAddon}${count > 1 ? ` +${count - 1}` : ''})`
: '';
return `Group: ${item.condition || 'true'}${summary}`;
}
if (item?.name) {
if (isCatalogModifications && item.type) {
return `${item.name} (${item.type})`;
}
return item.name;
}
return item?.options?.name || item?.instanceId || item?.id || key;
};
oldMap.forEach((val, key) => {
if (!newMap.has(key)) {
const originalIndex = obj1.findIndex((i: any) => getKey(i) === key);
diffs.push({
path: [...path, `[${originalIndex}]`],
type: 'REMOVE',
oldValue: isPresets ? `Deleted: ${getLabel(key)}` : val,
});
}
});
newMap.forEach((val, key) => {
const newIndex = obj2.findIndex((i: any) => getKey(i) === key);
if (!oldMap.has(key)) {
diffs.push({
path: [...path, `[${newIndex}]`],
type: 'ADD',
newValue: val,
});
} else {
const oldVal = oldMap.get(key);
diffs.push(...getObjectDiff(oldVal, val, [...path, `[${newIndex}]`]));
}
});
const intersectionOld = oldOrder.filter((key) => newOrder.includes(key));
const intersectionNew = newOrder.filter((key) => oldOrder.includes(key));
const isOrderChanged =
intersectionOld.length !== intersectionNew.length ||
intersectionOld.some((key, i) => key !== intersectionNew[i]);
if (isOrderChanged) {
diffs.push({
path: [...path],
type: 'CHANGE',
oldValue: oldOrder.map(getLabel),
newValue: newOrder.map(getLabel),
});
}
return diffs;
}
let i = 0;
let j = 0;
while (i < obj1.length || j < obj2.length) {
if (i >= obj1.length) {
diffs.push({
path: [...path, `[${j}]`],
type: 'ADD',
newValue: obj2[j],
});
j++;
continue;
}
if (j >= obj2.length) {
diffs.push({
path: [...path, `[${i}]`],
type: 'REMOVE',
oldValue: obj1[i],
});
i++;
continue;
}
const diff = getObjectDiff(obj1[i], obj2[j], [...path, `[${j}]`]);
if (diff.length === 0) {
i++;
j++;
continue;
}
let isDeletion = false;
if (i + 1 < obj1.length) {
if (getObjectDiff(obj1[i + 1], obj2[j], []).length === 0) {
isDeletion = true;
}
}
let isInsertion = false;
if (j + 1 < obj2.length) {
if (getObjectDiff(obj1[i], obj2[j + 1], []).length === 0) {
isInsertion = true;
}
}
if (isDeletion && !isInsertion) {
diffs.push({
path: [...path, `[${i}]`],
type: 'REMOVE',
oldValue: obj1[i],
});
i++;
} else if (isInsertion && !isDeletion) {
diffs.push({
path: [...path, `[${j}]`],
type: 'ADD',
newValue: obj2[j],
});
j++;
} else {
diffs.push(...diff);
i++;
j++;
}
}
return diffs;
}
if (!isObject(obj1) || !isObject(obj2)) {
if (obj1 === obj2) return diffs;
try {
if (JSON.stringify(obj1) === JSON.stringify(obj2)) return diffs;
} catch {}
return [
{
path,
type: 'CHANGE',
oldValue: obj1,
newValue: obj2,
},
];
}
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
for (const key of keys1) {
if (ignoredKeys.has(key)) continue;
if (!Object.prototype.hasOwnProperty.call(obj2, key)) {
if (obj1[key] == null || isEmptyValue(obj1[key])) continue;
diffs.push({
path: [...path, key],
type: 'REMOVE',
oldValue: obj1[key],
});
} else {
diffs.push(...getObjectDiff(obj1[key], obj2[key], [...path, key]));
}
}
for (const key of keys2) {
if (ignoredKeys.has(key)) continue;
if (!Object.prototype.hasOwnProperty.call(obj1, key)) {
if (obj2[key] == null || isEmptyValue(obj2[key])) continue;
diffs.push({
path: [...path, key],
type: 'ADD',
newValue: obj2[key],
});
}
}
return diffs;
}
export function formatValue(value: any): string {
if (typeof value === 'object' && value !== null) {
try {
return JSON.stringify(value, null, 2);
} catch {
return '[Circular Reference]';
}
}
return String(value);
}
|