$.equals
HTML
<p>a paragraph</p>
<div>a div with <span>a span</span> inside</div>
JavaScript
jQuery.equals = function( a, b, options ) {
options = $.extend({
// if true, in case of inequality, a trace of the first difference is logged to the console
verbose: false,
// use 'strict' for ===, 'abstract' for ==
comparison: 'strict',
// if true, an item is compared, otherwise it is ignored
// receives a value and a key, returns true or false
filter: function () {
return true;
}
}, options || {});
var initial_step = false;
if (typeof options.trace == 'undefined') {
initial_step = true;
options.trace = [];
}
if (options.comparison == 'strict' ? a === b : a == b) {
return true;
}
var a_type = $.type(a);
var b_type = $.type(b);
if (! (a_type == b_type)) {
log(['types', a_type, b_type, a, b]);
return false;
}
var a_keys = decompose(a, options.filter).keys;
var b_keys = decompose(b, options.filter).keys;
var a_length = a_keys.length;
var b_length = b_keys.length;
if (! (a_length == b_length)) {
log(['lengths', a_length, b_length, a, b]);
return false;
}
var length = a_length; // == b_length
if (length == 0) { // a and b are scalars
return a === b;
}
// compare corresponding elements
for (var i = 0; i < length; i++) {
var key = a_keys[i];
var a_value = a[key];
var b_value = b[key];
var equal = $.equals( a_value, b_value, options );
if (! equal) {
log(['values at key "' + key + '"', a_value, b_value, a, b]);
return false;
}
}
if (initial_step) {
log(['values', null, null, a, b]);
}
return true;
//---
// returns true if the given object is a scalar (not in JavaScript terms, though...)
function isScalarValue(object) {
return object === null || /undefined|boolean|number|string/.test(typeof object);
}
//...