Created
July 19, 2018 16:43
-
-
Save marutypes/c6b5eebcf98127b1f0490fcc7a749589 to your computer and use it in GitHub Desktop.
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
const arrayEqual = (a: any[], b: any[]) => { | |
if (a.length !== b.length) return false | |
for (const index in a) | |
if (!deepEqual(a[index], b[index])) { | |
return false | |
} | |
return true | |
} | |
const objectEqual = (a: any, b: any) => { | |
const propsA = Object.getOwnPropertyNames(a) | |
const propsB = Object.getOwnPropertyNames(b) | |
if (propsA.length !== propsB.length) { | |
return false | |
} | |
for (const prop of propsA) | |
if ( | |
!Object.prototype.hasOwnProperty.call(b, prop) || | |
!deepEqual(a[prop], b[prop]) | |
) { | |
return false | |
} | |
return true | |
} | |
export const deepEqual = (a: any, b: any) => { | |
if (a === b) { | |
return true | |
} | |
if (typeof a !== typeof b) { | |
return false | |
} | |
if (a === null || b === null) { | |
return false | |
} | |
if (Array.isArray(a)) { | |
return arrayEqual(a, b) | |
} | |
if (typeof a === 'object') { | |
return objectEqual(a, b) | |
} | |
return a.valueOf() === b.valueOf() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice