2 * JavaScript backwards-compatibility alternatives and other convenience functions
7 trimLeft: function( str ) {
8 return str === null ? '' : str.toString().replace( /^\s+/, '' );
10 trimRight: function( str ) {
12 '' : str.toString().replace( /\s+$/, '' );
14 ucFirst: function( str ) {
15 return str.charAt( 0 ).toUpperCase() + str.substr( 1 );
17 escapeRE: function( str ) {
18 return str.replace ( /([\\{}()|.?*+\-^$\[\]])/g, "\\$1" );
20 isDomElement: function( el ) {
21 return !!el && !!el.nodeType;
23 isEmpty: function( v ) {
24 if ( v === '' || v === 0 || v === '0' || v === null
25 || v === false || v === undefined )
29 // the for-loop could potentially contain prototypes
30 // to avoid that we check it's length first
31 if ( v.length === 0 ) {
34 if ( typeof v === 'object' ) {
35 for ( var key in v ) {
42 compareArray: function( arrThis, arrAgainst ) {
43 if ( arrThis.length != arrAgainst.length ) {
46 for ( var i = 0; i < arrThis.length; i++ ) {
47 if ( $.isArray( arrThis[i] ) ) {
48 if ( !$.compareArray( arrThis[i], arrAgainst[i] ) ) {
51 } else if ( arrThis[i] !== arrAgainst[i] ) {
57 compareObject: function( objectA, objectB ) {
59 // Do a simple check if the types match
60 if ( typeof objectA == typeof objectB ) {
62 // Only loop over the contents if it really is an object
63 if ( typeof objectA == 'object' ) {
64 // If they are aliases of the same object (ie. mw and mediaWiki) return now
65 if ( objectA === objectB ) {
69 // Iterate over each property
70 for ( prop in objectA ) {
71 // Check if this property is also present in the other object
72 if ( prop in objectB ) {
73 // Compare the types of the properties
74 var type = typeof objectA[prop];
75 if ( type == typeof objectB[prop] ) {
76 // Recursively check objects inside this one
79 if ( !$.compareObject( objectA[prop], objectB[prop] ) ) {
84 // Functions need to be strings to compare them properly
85 if ( objectA[prop].toString() !== objectB[prop].toString() ) {
91 if ( objectA[prop] !== objectB[prop] ) {
103 // Check for properties in B but not in A
104 // This is about 15% faster (tested in Safari 5 and Firefox 3.6)
105 // ...than incrementing a count variable in the above and below loops
106 // See also: http://www.mediawiki.org/wiki/ResourceLoader/Default_modules/compareObject_test#Results
107 for ( prop in objectB ) {
108 if ( !( prop in objectA ) ) {