Skip to content

Instantly share code, notes, and snippets.

@marutypes
Created July 19, 2018 16:43
Show Gist options
  • Save marutypes/c6b5eebcf98127b1f0490fcc7a749589 to your computer and use it in GitHub Desktop.
Save marutypes/c6b5eebcf98127b1f0490fcc7a749589 to your computer and use it in GitHub Desktop.
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()
}
@krishnaprajapat
Copy link

Nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment