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 ) {
25 if ( v === '' || v === 0 || v === '0' || v === null
26 || v === false || v === undefined )
30 // the for-loop could potentially contain prototypes
31 // to avoid that we check it's length first
32 if ( v.length === 0 ) {
35 if ( typeof v === 'object' ) {
43 compareArray: function ( arrThis, arrAgainst ) {
44 if ( arrThis.length !== arrAgainst.length ) {
47 for ( var i = 0; i < arrThis.length; i++ ) {
48 if ( $.isArray( arrThis[i] ) ) {
49 if ( !$.compareArray( arrThis[i], arrAgainst[i] ) ) {
52 } else if ( arrThis[i] !== arrAgainst[i] ) {
58 compareObject: function ( objectA, objectB ) {
61 // Do a simple check if the types match
62 if ( typeof objectA === typeof objectB ) {
64 // Only loop over the contents if it really is an object
65 if ( typeof objectA === 'object' ) {
66 // If they are aliases of the same object (ie. mw and mediaWiki) return now
67 if ( objectA === objectB ) {
70 // Iterate over each property
71 for ( prop in objectA ) {
72 // Check if this property is also present in the other object
73 if ( prop in objectB ) {
74 // Compare the types of the properties
75 type = typeof objectA[prop];
76 if ( type === typeof objectB[prop] ) {
77 // Recursively check objects inside this one
80 if ( !$.compareObject( objectA[prop], objectB[prop] ) ) {
85 // Functions need to be strings to compare them properly
86 if ( objectA[prop].toString() !== objectB[prop].toString() ) {
92 if ( objectA[prop] !== objectB[prop] ) {
104 // Check for properties in B but not in A
105 // This is about 15% faster (tested in Safari 5 and Firefox 3.6)
106 // ...than incrementing a count variable in the above and below loops
107 // See also: http://www.mediawiki.org/wiki/ResourceLoader/Default_modules/compareObject_test#Results
108 for ( prop in objectB ) {
109 if ( !( prop in objectA ) ) {