Created
July 24, 2019 11:36
-
-
Save whizkydee/90b2f9b300ce40408dac77778eee82f9 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
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