@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.
38 lines (37 loc) • 1.55 kB
TypeScript
type Diff<T> = {
[K in keyof T]?: T[K] extends (infer U)[] ? (Diff<U> | {
curr_value: U;
prev_value: U;
})[] : T[K] extends object ? Diff<T[K]> | {
curr_value: T[K];
prev_value: T[K];
} : {
curr_value: T[K];
prev_value: T[K];
};
};
/**
* 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 declare function compareObjects<T extends object>(prev: T, curr: Partial<T>): Diff<T>;
export {};