Skip to content

Instantly share code, notes, and snippets.

@dschnare
Created September 29, 2011 15:31
Show Gist options
  • Save dschnare/1251001 to your computer and use it in GitHub Desktop.
Save dschnare/1251001 to your computer and use it in GitHub Desktop.
EqualsJS - Deep equality testing for JavaScript.
// Author: Darren Schnare
// Keywords: javascript,equality,testing,equals,object
// License: MIT ( http://www.opensource.org/licenses/mit-license.php )
// Repo: https://gist.github.com/1251001
// Creates an equals function within the specified scope.
(function(scope) {
// Determines if two objects have the same values. This is a
// recursive test that tests all objects through out the object
// tree.
//
// This function uses the built-in valueOf() functionality of
// all JavaScrpt objects to convert a and b to native objects.
// You can use this to your advantage when wanting to compare
// complex objects.
//
// equals(a, b)
scope.equals = function equals(a, b) {
var typeofa, typeofb, i, len, key;
// If a and b refer to the same object then they are equal.
if (a === b) return true;
// Get the native type of both a and b. Use the built-in valueOf()
// function to get the native object of each variable.
typeofa = a === null ? "null" : typeof (a = a ? a.valueOf() : a);
typeofb = b === null ? "null" : typeof (b = b ? b.valueOf() : b);
// If a and b are not the same native type.
if (typeofa !== typeofb) return false;
switch (typeofa) {
case "string":
case "boolean":
case "number":
case "functon":
case "undefined":
case "null":
return a === b;
}
// Convert the native type to a string. This allows us to test
// if either a or b are Arrays and then handle accordingly.
typeofa = ({}).toString.call(a);
typeofb = ({}).toString.call(b);
if (typeofa === typeofb) {
// Compare the items of two arrays
if (typeofa === "[object Array]") {
if (a.length !== b.length) return false;
len = a.length;
for (i = 0; i < len; i++) {
if (!equals(a[i], b[i])) return false;
}
// Compare the keys of two objects
} else {
for (key in a) {
if (!(key in b)) return false;
if (!equals(a[key], b[key])) return false;
}
}
} else {
return false;
}
return true;
}
}(window));
<!--Author: Darren Schnare
Keywords: javascript,equality,testing,equals,object
License: MIT ( http://www.opensource.org/licenses/mit-license.php )
Repo: https://gist.github.com/1251001-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>EqualsJS Example</title>
<script type="text/javascript" src="https://gist.github.com/raw/1251001/equals.js"></script>
</head>
<body>
<script type="text/javascript">
// Handle logging messages.
function log(/*...*/) {
var doc = document, body = doc.getElementsByTagName("body")[0];
for (var i = 0, len = arguments.length; i < len; i++) {
body.appendChild(doc.createTextNode(arguments[i] + ""));
body.appendChild(doc.createElement("br"));
}
}
var a = [1,2,3];
var b = [1,2,3];
log(equals(a, b)); // true
a.pop();
log(equals(a, b)); // false
b.pop();
a.push({name: "John"});
b.push({name: "Dave"});
log(equals(a, b)); // false
b[b.length - 1].name = "John";
log(equals(a, b)); // true
a.push(4,5,6,7,8,9);
a.valueOf = function() {
return "a";
}
log(equals(a, b)); // false
// Give be the same valueOf() as a.
// Now a is equal to b.
b.valueOf = function() {
return "a";
}
log(equals(a, b)); // true
</script>
</body>
</html>
name: EqualsJS - Deep equality testing.
description: Performs recursive equality testing on two objects.
authors:
- Darren Schnare
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment