Skip to content

Instantly share code, notes, and snippets.

@huafu
Created September 28, 2014 07:57
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 huafu/bd73511e18359fe6bf3b to your computer and use it in GitHub Desktop.
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
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