Last active
April 30, 2020 05:19
-
-
Save ambantis/d0c2997cc85796727c8def7d5b4fcad0 to your computer and use it in GitHub Desktop.
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 deepEquals(thing1, thing2) { | |
let typeOfFun = (x) => (x) => (x == null ? "null" : typeof x); | |
const type1 = typeOfFun(thing1); | |
const type2 = typeOfFun(thing2); | |
let isNull1 = type1 == "null"; | |
let isNull2 = type2 == "null"; | |
if (isNull1 && isNull2) { | |
return true; | |
} else if (isNull1 || isNull2 || type1 != type2) { | |
return false; | |
} else { | |
switch (typeof thing1) { | |
case "object": | |
let keys1 = Object.keys(thing1); | |
let keys2 = Object.keys(thing2); | |
let s2 = new Set(keys2); | |
let sameKeys = | |
keys1.length == keys2.length && | |
keys1.reduce((acc, next) => acc && s2.has(next), true); | |
let reducer = (acc, key) => acc && deepEquals(thing1[key], thing2[key]); | |
return sameKeys && keys1.reduce(reducer, true); | |
break; | |
default: | |
return thing1 == thing2; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment