// src/utils/objectUtils.ts

/**
 * Deeply compares two values to determine if they are different.
 * @param val1 - The first value to compare.
 * @param val2 - The second value to compare.
 * @returns {boolean} - True if the values are different, false otherwise.
 */
const deepCompare = (val1: any, val2: any): boolean => {
    if (Array.isArray(val1) && Array.isArray(val2)) {
        if (val1.length !== val2.length) return true;
        for (let i = 0; i < val1.length; i++) {
            if (deepCompare(val1[i], val2[i])) return true;
        }
        return false;
    } else if (typeof val1 === 'object' && typeof val2 === 'object') {
        return hasObjectChanged(val1, val2);
    } else {
        return val1 !== val2;
    }
};

/**
 * Compares two objects and returns true if any values are different.
 * @param obj1 - The first object to compare.
 * @param obj2 - The second object to compare.
 * @returns {boolean} - True if any values are different, false otherwise.
 */
export const hasObjectChanged = (obj1: Record<string, any>, obj2: Record<string, any>): boolean => {
    const keys1 = Object.keys(obj1);
    const keys2 = Object.keys(obj2);

    if (keys1.length !== keys2.length) {
        return true;
    }

    for (const key of keys1) {
        if (deepCompare(obj1[key], obj2[key])) {
            return true;
        }
    }

    return false;
};

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: