UNPKG

@gravityforms/utils

Version:
53 lines (46 loc) 1.29 kB
/** * @module isEqual * @description Performs a deep comparison to determine if two variables of any type are equal. * * @since 3.1.0 * * @param {*} a The first variable to compare. * @param {*} b The second variable to compare. * * @return {boolean} Returns true if the specified variables are equal. Returns false otherwise. * * @example * import { isEqual } from "@gravityforms/utils"; * * function Example() { * const obj1 = { prop: 'val' }; * const obj2 = { prop: 'val2' }; * const equal = isEqual( obj1, obj2 ); * } * */ const isEqual = ( a, b ) => { // Check if both values are identical if ( a === b ) { return true; } // Check if both values are objects (and not null) if ( a === null || a === undefined || typeof a !== 'object' || b === null || b === undefined || typeof b !== 'object' ) { return false; } // Get the keys of both objects const keysA = Object.keys( a ); const keysB = Object.keys( b ); // If number of keys is different, objects are not equal if ( keysA.length !== keysB.length ) { return false; } // Check if all keys and values are identical for ( const key of keysA ) { if ( ! keysB.includes( key ) || ! isEqual( a[ key ], b[ key ] ) ) { return false; } } return true; }; export default isEqual;