@tmlmobilidade/utils
Version:
A collection of utility functions and helpers for the TML Mobilidade Go monorepo, providing common functionality for batching operations, caching, HTTP requests, object manipulation, permissions, and more.
66 lines (65 loc) • 2.61 kB
JavaScript
/**
* Compares two objects and returns the differences between them.
*
* @template T - The type of the objects being compared.
* @param {T} prev - The previous state of the object.
* @param {Partial<T>} curr - The current state of the object.
* @returns {Diff<T>} - An object representing the differences between the previous and current states.
*
* This function iterates over the keys of the current object and compares each value with the corresponding value in the previous object.
* If the values are arrays, it compares each element in the arrays and records any differences.
* If the values are objects, it recursively compares the nested objects.
* If the values are primitive types, it records the current and previous values if they differ.
*
* @example
* const prev = { name: 'Alice', age: 30, hobbies: ['reading', 'hiking'] };
* const curr = { name: 'Alice', age: 31, hobbies: ['reading', 'swimming'] };
* const differences = compareObjects(prev, curr);
* // differences will be:
* // {
* // age: { curr_value: 31, prev_value: 30 },
* // hobbies: [{ curr_value: 'swimming', prev_value: 'hiking' }]
* // }
*/
export function compareObjects(prev, curr) {
const diff = {};
Object.keys(curr).forEach((key) => {
const prevVal = prev[key];
const currVal = curr[key];
if (prevVal === currVal)
return;
if (Array.isArray(prevVal) && Array.isArray(currVal)) {
const arrayDiff = [];
const maxLength = Math.max(prevVal.length, currVal.length);
for (let i = 0; i < maxLength; i++) {
const p = prevVal[i];
const c = currVal[i];
if (p === c)
continue;
if (typeof p === 'object' && typeof c === 'object' && p && c) {
const nestedDiff = compareObjects(p, c);
arrayDiff.push(nestedDiff);
}
else {
arrayDiff.push({ curr_value: c, prev_value: p });
}
}
if (arrayDiff.length > 0) {
diff[key] = arrayDiff;
}
}
else if (prevVal
&& currVal
&& typeof prevVal === 'object'
&& typeof currVal === 'object') {
const nestedDiff = compareObjects(prevVal, currVal);
if (Object.keys(nestedDiff).length > 0) {
diff[key] = nestedDiff;
}
}
else {
diff[key] = { curr_value: currVal, prev_value: prevVal };
}
});
return diff;
}