Last active
June 3, 2024 19:54
-
-
Save waicampos/29b9c32a324d34e2e3a62d8f6a67798b to your computer and use it in GitHub Desktop.
Comparação profunda de objetos JavaScript
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
// Baseado em: https://www.alura.com.br/artigos/vamos-implementar-funcao-comparacao-profunda-js | |
//Foi adicionada a verificação da linha 8 para os casos em que os valores forem null. | |
export function deepStrictEqual(obj_1, obj_2) { | |
const isPrimitive = (element) => !(Object(element) === element) | |
if (isPrimitive(obj_1) && isPrimitive(obj_2)) { | |
return Object.is(obj_1, obj_2) | |
} else if(isPrimitive(obj_1) || isPrimitive(obj_2)){ | |
return false | |
} | |
if (Reflect.ownKeys(obj_1).length !== Reflect.ownKeys(obj_2).length) { | |
return false | |
} | |
return Reflect.ownKeys(obj_1).every((obj_1_key) => { | |
return Reflect.has(obj_2, obj_1_key) | |
? deepStrictEqual(obj_2[obj_1_key], obj_1[obj_1_key]) | |
: false | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment