Coding style commit
[lhc/web/wiklou.git] / resources / jquery / jquery.mwExtension.js
1 /*
2 * JavaScript backwards-compatibility alternatives and other convenience functions
3 */
4
5 jQuery.extend({
6 trimLeft: function( str ) {
7 return str === null ? '' : str.toString().replace( /^\s+/, '' );
8 },
9 trimRight: function( str ) {
10 return str === null ?
11 '' : str.toString().replace( /\s+$/, '' );
12 },
13 ucFirst: function( str ) {
14 return str.substr( 0, 1 ).toUpperCase() + str.substr( 1 );
15 },
16 escapeRE: function( str ) {
17 return str.replace ( /([\\{}()|.?*+\-^$\[\]])/g, "\\$1" );
18 },
19 isDomElement: function( el ) {
20 return !!el && !!el.nodeType;
21 },
22 isEmpty: function( v ) {
23 if ( v === '' || v === 0 || v === '0' || v === null
24 || v === false || v === undefined )
25 {
26 return true;
27 }
28 // the for-loop could potentially contain prototypes
29 // to avoid that we check it's length first
30 if ( v.length === 0 ) {
31 return true;
32 }
33 if ( typeof v === 'object' ) {
34 for ( var key in v ) {
35 return false;
36 }
37 return true;
38 }
39 return false;
40 },
41 compareArray: function( arrThis, arrAgainst ) {
42 if ( arrThis.length != arrAgainst.length ) {
43 return false;
44 }
45 for ( var i = 0; i < arrThis.length; i++ ) {
46 if ( arrThis[i] instanceof Array ) {
47 if ( !$.compareArray( arrThis[i], arrAgainst[i] ) ) {
48 return false;
49 }
50 } else if ( arrThis[i] !== arrAgainst[i] ) {
51 return false;
52 }
53 }
54 return true;
55 },
56 compareObject: function( objectA, objectB ) {
57
58 // Do a simple check if the types match
59 if ( typeof objectA == typeof objectB ) {
60
61 // Only loop over the contents if it really is an object
62 if ( typeof objectA == 'object' ) {
63 // If they are aliases of the same object (ie. mw and mediaWiki) return now
64 if ( objectA === objectB ) {
65 return true;
66 } else {
67 var prop;
68 // Iterate over each property
69 for ( prop in objectA ) {
70 // Check if this property is also present in the other object
71 if ( prop in objectB ) {
72 // Compare the types of the properties
73 var type = typeof objectA[prop];
74 if ( type == typeof objectB[prop] ) {
75 // Recursively check objects inside this one
76 switch ( type ) {
77 case 'object' :
78 if ( !$.compareObject( objectA[prop], objectB[prop] ) ) {
79 return false;
80 }
81 break;
82 case 'function' :
83 // Functions need to be strings to compare them properly
84 if ( objectA[prop].toString() !== objectB[prop].toString() ) {
85 return false;
86 }
87 break;
88 default:
89 // Strings, numbers
90 if ( objectA[prop] !== objectB[prop] ) {
91 return false;
92 }
93 break;
94 }
95 } else {
96 return false;
97 }
98 } else {
99 return false;
100 }
101 }
102 // Check for properties in B but not in A
103 // This is about 15% faster (tested in Safari 5 and Firefox 3.6)
104 // ...than incrementing a count variable in the above and below loops
105 // See also: http://www.mediawiki.org/wiki/ResourceLoader/Default_modules/compareObject_test#Results
106 for ( prop in objectB ) {
107 if ( !( prop in objectA ) ) {
108 return false;
109 }
110 }
111 }
112 }
113 } else {
114 return false;
115 }
116 return true;
117 }
118 });
119