Skip to content

Instantly share code, notes, and snippets.

@typehorror
Last active October 13, 2015 15:58
Show Gist options
  • Save typehorror/4220079 to your computer and use it in GitHub Desktop.
Save typehorror/4220079 to your computer and use it in GitHub Desktop.
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