Last active
October 13, 2015 15:58
-
-
Save typehorror/4220079 to your computer and use it in GitHub Desktop.
A javascript function to compare (by inference) object, strings, number
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
// test that every value of A exists in B and are equal | |
// true equality: eq(a,b,1) | |
// true: eq({b:[1,2]}, {a:1, b:[1,2]}) | |
// false: eq({a:1, b:[1,2]},{b:[1,2]}) | |
// false: eq({b:[1,2]}, {a:1, b:[1,2,3]}) | |
// false: eq({b:[1,2,3]}, {a:1, b:[1,2]}) | |
function eq(a, b, bidirectional){ | |
if (typeof(a) != typeof(b)) return false; | |
if (typeof(a) in {string: 1, number: 1}) return a == b; | |
if (a.length != b.length) return false; | |
for (var i in a){ | |
if (!eq(a[i], b[i])) return false; | |
} | |
if (bidirectional) return eq(b,a); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment