From: Krinkle Date: Mon, 13 Dec 2010 23:52:45 +0000 (+0000) Subject: added compareObject function to mediaWiki core jQuery prototyping utilities X-Git-Tag: 1.31.0-rc.0~33328 X-Git-Url: http://git.cyclocoop.org/%24href?a=commitdiff_plain;h=19dc4e5a31b32e86fa50082080c2499aa2008364;p=lhc%2Fweb%2Fwiklou.git added compareObject function to mediaWiki core jQuery prototyping utilities --- diff --git a/resources/mediawiki/mediawiki.js b/resources/mediawiki/mediawiki.js index 0f3a10b27e..fc7aa10c8d 100644 --- a/resources/mediawiki/mediawiki.js +++ b/resources/mediawiki/mediawiki.js @@ -50,6 +50,67 @@ jQuery.extend({ } } return true; + }, + compareObject : function( objectA, objectB ) { + + // Do a simple check if the types match + if ( typeof( objectA ) == typeof( objectB ) ) { + + // Only loop over the contents if it really is an object + if ( typeof( objectA ) == 'object' ) { + // If they are aliases of the same object (ie. mw and mediaWiki) return now + if ( objectA === objectB ) { + return true; + } else { + // Iterate over each property + for ( var prop in objectA ) { + // Check if this property is also present in the other object + if ( prop in objectB ) { + // Compare the types of the properties + var type = typeof( objectA[prop] ); + if ( type == typeof( objectB[prop] ) ) { + // Recursively check objects inside this one + switch ( type ) { + case 'object' : + if ( !$.compareObject( objectA[prop], objectB[prop] ) ) { + return false; + } + break; + case 'function' : + // Functions need to be strings to compare them properly + if ( objectA[prop].toString() !== objectB[prop].toString() ) { + return false; + } + break; + default: + // Strings, numbers + if ( objectA[prop] !== objectB[prop] ) { + return false; + } + break; + } + } else { + return false; + } + } else { + return false; + } + } + // Check for properties in B but not in A + // This is about 15% faster (tested in Safari 5 and Firefox 3.6) + // ...than incrementing a count variable in the above and below loops + // See also: http://www.mediawiki.org/wiki/ResourceLoader/Default_modules/compareObject_test#Results + for ( var prop in objectB ) { + if ( !( prop in objectA ) ) { + return false; + } + } + } + } + } else { + return false; + } + return true; } });