lion/packages/ui/components/overlays/utils/is-equal-config.js
2022-10-31 16:55:07 +01:00

24 lines
811 B
JavaScript

/**
* @typedef {import('../../types/OverlayConfig').OverlayConfig} OverlayConfig
*/
/**
* Compares two OverlayConfigs to equivalence. Intended to prevent unnecessary resets.
* Note that it doesn't cover as many use cases as common implementations, such as Lodash isEqual.
*
* @param {Partial<OverlayConfig>} a
* @param {Partial<OverlayConfig>} b
* @returns {boolean} Whether the configs are equivalent
*/
export function isEqualConfig(a, b) {
if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) {
return a === b;
}
const aProps = Object.keys(a);
const bProps = Object.keys(b);
if (aProps.length !== bProps.length) {
return false;
}
const isEqual = /** @param {string} prop */ prop => isEqualConfig(a[prop], b[prop]);
return aProps.every(isEqual);
}