Created
November 13, 2020 15:13
-
-
Save ghengeveld/ca1cff1a0e6dd86ecfe7692843ffe8e5 to your computer and use it in GitHub Desktop.
shallowEqual with logging
This file contains 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
function shallowEqual(objA, objB): boolean { | |
if (Object.is(objA, objB)) { | |
return true; | |
} | |
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) { | |
console.log('not object'); | |
return false; | |
} | |
const keysA = Object.keys(objA); | |
const keysB = Object.keys(objB); | |
if (keysA.length !== keysB.length) { | |
console.log('keys.length', keysA.length, keysB.length); | |
return false; | |
} | |
// Test for A's keys different from B. | |
for (let i = 0; i < keysA.length; i += 1) { | |
if ( | |
!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || | |
!Object.is(objA[keysA[i]], objB[keysA[i]]) | |
) { | |
console.log(keysA[i], objA[keysA[i]], objB[keysA[i]]); | |
return false; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment