Created
August 6, 2025 04:07
-
-
Save orion55/bdb6145b70f134f049fdc2654017b98a to your computer and use it in GitHub Desktop.
isEqual
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export const isEqual = (a, b) => { | |
| if (typeof a !== "object" && typeof b !== "object") { | |
| return Object.is(a, b); | |
| } | |
| // Хотя null — примитивный тип в JavaScript, из-за некоторых | |
| // исторических особенностей тип null — object, поэтому нам требуется | |
| // дополнительная обработка для null. | |
| if (a === null && b === null) { | |
| return true; | |
| } | |
| if (typeof a !== typeof b) { | |
| return false; | |
| } | |
| // Сначала пробуем строгое сравнение. | |
| if (a === b) { | |
| return true; | |
| } | |
| // Отдельно сравниваем объекты типа Date | |
| if (a instanceof Date && b instanceof Date) { | |
| return a.getTime() === b.getTime(); | |
| } | |
| // Проверка отдельно элеменов в массивах | |
| if (Array.isArray(a) && Array.isArray(b)) { | |
| return a.length === b.length && a.every((value, index) => isEqual(value, b[index])); | |
| } | |
| // Убедимся, что переданные значения точно объекты | |
| if (!a || !b || typeof a !== "object" || typeof b !== "object") return false; | |
| let keysA = Object.keys(a), | |
| keysB = Object.keys(b); | |
| // Проверка свойств в объектах | |
| return keysA.every((key) => keysB.includes(key) && isEqual(a[key], b[key])); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment