Skip to content

Instantly share code, notes, and snippets.

@whizkydee
Created July 24, 2019 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whizkydee/90b2f9b300ce40408dac77778eee82f9 to your computer and use it in GitHub Desktop.
Save whizkydee/90b2f9b300ce40408dac77778eee82f9 to your computer and use it in GitHub Desktop.
function isObject(obj) {
return obj !== null && typeof obj === 'object'
}
export default function looseEqual(a, b) {
if (a === b) return true
const isObjectA = isObject(a)
const isObjectB = isObject(b)
if (isObjectA && isObjectB) {
try {
const isArrayA = Array.isArray(a)
const isArrayB = Array.isArray(b)
if (isArrayA && isArrayB) {
return (
a.length === b.length &&
a.every((e, i) => {
return looseEqual(e, b[i])
})
)
} else if (a instanceof Date && b instanceof Date) {
return a.getTime() === b.getTime()
} else if (!isArrayA && !isArrayB) {
const keysA = Object.keys(a)
const keysB = Object.keys(b)
return (
keysA.length === keysB.length &&
keysA.every(key => {
return looseEqual(a[key], b[key])
})
)
} else {
return false
}
} catch (e) {
return false
}
} else if (!isObjectA && !isObjectB) {
return String(a) === String(b)
} else {
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment