This commit is contained in:
Your Name
2024-02-16 19:38:14 -06:00
parent 5c2c44b780
commit 3ba35357c7

View File

@@ -68,13 +68,22 @@ function calculateDiff(oldValue, newValue) {
const changes = [];
function findChanges(path, oldVal, newVal) {
const oldKeys = oldVal ? Object.keys(oldVal) : [];
// Check if both values are objects (and not null), otherwise compare directly
if (!(typeof oldVal === 'object' && oldVal !== null) ||
!(typeof newVal === 'object' && newVal !== null)) {
if (oldVal !== newVal) {
changes.push(`${path} = ${JSON.stringify(newVal)};`);
}
return oldVal !== newVal ? 1 : 0;
}
const oldKeys = Object.keys(oldVal);
const newKeys = Object.keys(newVal);
const allKeys = new Set([...oldKeys, ...newKeys]);
let changedCount = 0;
allKeys.forEach(key => {
const oldKeyValue = oldVal ? oldVal[key] : undefined;
const oldKeyValue = oldVal[key];
const newKeyValue = newVal[key];
const currentPath = path ? `${path}['${key}']` : `['${key}']`;
@@ -82,7 +91,7 @@ function calculateDiff(oldValue, newValue) {
// New key added
changes.push(`${currentPath} = ${JSON.stringify(newKeyValue)};`);
changedCount++;
} else if (typeof oldKeyValue === 'object' && oldKeyValue !== null && typeof newKeyValue === 'object') {
} else if (typeof oldKeyValue === 'object' && oldKeyValue !== null && typeof newKeyValue === 'object' && newKeyValue !== null) {
// Recursive diff for objects
const subChanges = findChanges(currentPath, oldKeyValue, newKeyValue);
if (subChanges > 0) changedCount++;
@@ -103,10 +112,16 @@ function calculateDiff(oldValue, newValue) {
return changedCount;
}
// Adjust initial call to handle non-object types
if ((typeof oldValue === 'object' && oldValue !== null) && (typeof newValue === 'object' && newValue !== null)) {
findChanges('progvars', oldValue, newValue);
} else if (oldValue !== newValue) {
changes.push(`progvars = ${JSON.stringify(newValue)};`);
}
return changes.join('\n');
}
}
//// HTTP SERVER ////