blob: d3197f13244c82ba4c48977c60aaca282699a4e3 [file] [log] [blame]
/**
* @license
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export function deepEqual<T>(a: T, b: T): boolean {
if (a === b) return true;
if (a === undefined || b === undefined) return false;
if (a === null || b === null) return false;
if (a instanceof Date || b instanceof Date) {
if (!(a instanceof Date && b instanceof Date)) return false;
return a.getTime() === b.getTime();
}
if (a instanceof Set || b instanceof Set) {
if (!(a instanceof Set && b instanceof Set)) return false;
if (a.size !== b.size) return false;
for (const ai of a) if (!b.has(ai)) return false;
return true;
}
if (a instanceof Map || b instanceof Map) {
if (!(a instanceof Map && b instanceof Map)) return false;
if (a.size !== b.size) return false;
for (const [aKey, aValue] of a.entries()) {
if (!b.has(aKey) || !deepEqual(aValue, b.get(aKey))) return false;
}
return true;
}
if (typeof a === 'object') {
if (typeof b !== 'object') return false;
const aObj = a as Record<string, unknown>;
const bObj = b as Record<string, unknown>;
const aKeys = Object.keys(aObj);
const bKeys = Object.keys(bObj);
if (aKeys.length !== bKeys.length) return false;
for (const key of aKeys) {
if (!deepEqual(aObj[key], bObj[key])) return false;
}
return true;
}
return false;
}
/**
* Queries for child element specified with a selector. Copied from Gerrit's common-util.ts.
*/
export function query<E extends Element = Element>(
el: Element | null | undefined,
selector: string
): E | undefined {
if (!el) return undefined;
if (el.shadowRoot) {
const r = el.shadowRoot.querySelector<E>(selector);
if (r) return r;
}
return el.querySelector<E>(selector) ?? undefined;
}