Skip to content

Instantly share code, notes, and snippets.

@debrice

debrice/equal.js

Last active Oct 13, 2015
Embed
What would you like to do?
A javascript function to compare (by inference) object, strings, number
// 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