signal-desktop/ts/util/isShallowEqual.ts

35 lines
649 B
TypeScript
Raw Normal View History

// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2022-12-23 00:32:03 +00:00
export function isShallowEqual(a: unknown, b: unknown): boolean {
if (a === b) {
return true;
}
if (typeof a !== typeof b) {
return false;
}
if (a == null || b == null) {
return false;
}
if (typeof a !== 'object') {
return false;
}
const keys = Object.keys(a);
if (keys.length !== Object.keys(b).length) {
return false;
}
for (const key of keys) {
2022-12-23 00:32:03 +00:00
if (
(a as Record<string | number, unknown>)[key] !==
(b as Record<string | number, unknown>)[key]
) {
return false;
}
}
return true;
}