Created
September 28, 2014 07:57
-
-
Save huafu/bd73511e18359fe6bf3b to your computer and use it in GitHub Desktop.
Deeply compare 2 variables and returns true if they are the same, else false
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
isSame = (obj1, obj2) -> | |
if obj1 is obj2 | |
yes | |
else if obj1? and obj2? | |
if (t = typeof obj1) isnt typeof obj2 | |
no | |
else | |
if t isnt 'object' | |
no | |
else | |
if obj1 instanceof Array | |
# compare arrays | |
if obj1.length isnt obj2.length | |
no | |
else | |
for v, i in obj1 when not isSame(obj2[i], v) | |
return no | |
yes | |
else | |
# compare objects own properties | |
keys1 = (k for own k of obj1) | |
keys2 = (k for own k of obj2) | |
if isSame(keys1, keys2) | |
for k in keys1 when not isSame(obj1[k], obj2[k]) | |
return no | |
yes | |
else | |
no | |
else | |
# one or the other isn't null/undefined | |
no |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment