r78345 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r78344‎ | r78345 | r78346 >
Date:23:52, 13 December 2010
Author:krinkle
Status:ok (Comments)
Tags:
Comment:
added compareObject function to mediaWiki core jQuery prototyping utilities
Modified paths:
  • /trunk/phase3/resources/mediawiki/mediawiki.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/mediawiki/mediawiki.js
@@ -50,6 +50,67 @@
5151 }
5252 }
5353 return true;
 54+ },
 55+ compareObject : function( objectA, objectB ) {
 56+
 57+ // Do a simple check if the types match
 58+ if ( typeof( objectA ) == typeof( objectB ) ) {
 59+
 60+ // Only loop over the contents if it really is an object
 61+ if ( typeof( objectA ) == 'object' ) {
 62+ // If they are aliases of the same object (ie. mw and mediaWiki) return now
 63+ if ( objectA === objectB ) {
 64+ return true;
 65+ } else {
 66+ // Iterate over each property
 67+ for ( var prop in objectA ) {
 68+ // Check if this property is also present in the other object
 69+ if ( prop in objectB ) {
 70+ // Compare the types of the properties
 71+ var type = typeof( objectA[prop] );
 72+ if ( type == typeof( objectB[prop] ) ) {
 73+ // Recursively check objects inside this one
 74+ switch ( type ) {
 75+ case 'object' :
 76+ if ( !$.compareObject( objectA[prop], objectB[prop] ) ) {
 77+ return false;
 78+ }
 79+ break;
 80+ case 'function' :
 81+ // Functions need to be strings to compare them properly
 82+ if ( objectA[prop].toString() !== objectB[prop].toString() ) {
 83+ return false;
 84+ }
 85+ break;
 86+ default:
 87+ // Strings, numbers
 88+ if ( objectA[prop] !== objectB[prop] ) {
 89+ return false;
 90+ }
 91+ break;
 92+ }
 93+ } else {
 94+ return false;
 95+ }
 96+ } else {
 97+ return false;
 98+ }
 99+ }
 100+ // Check for properties in B but not in A
 101+ // This is about 15% faster (tested in Safari 5 and Firefox 3.6)
 102+ // ...than incrementing a count variable in the above and below loops
 103+ // See also: http://www.mediawiki.org/wiki/ResourceLoader/Default_modules/compareObject_test#Results
 104+ for ( var prop in objectB ) {
 105+ if ( !( prop in objectA ) ) {
 106+ return false;
 107+ }
 108+ }
 109+ }
 110+ }
 111+ } else {
 112+ return false;
 113+ }
 114+ return true;
54115 }
55116 });
56117

Comments

#Comment by Catrope (talk | contribs)   09:26, 30 December 2010

Why is this in here again? It appears to be unused.

#Comment by Krinkle (talk | contribs)   17:05, 30 December 2010

It's not used in core, however it's usage is encouraged in the documentation.

Across lots of wikis (including numourus WMF wikis) I see functions that intent to compare objects. Most of them only do it partially, but all with the goal to compare a variable result to a static object.

An example usecase to see if the current user matches a set of preferences: http://www.mediawiki.org/wiki/ResourceLoader/Default_modules#options

If you believe the use of it is not as common, we could put this in a seperate plugin and load it on demand only (mw.loader.using()) into jquery.mwPrototypes or jquery.compareObject.

Status & tagging log