2 * JavaScript backwards-compatibility alternatives and other convenience functions
6 trimLeft: function( str
) {
7 return str
=== null ? '' : str
.toString().replace( /^\s+/, '' );
9 trimRight: function( str
) {
11 '' : str
.toString().replace( /\s+$/, '' );
13 ucFirst: function( str
) {
14 return str
.substr( 0, 1 ).toUpperCase() + str
.substr( 1 );
16 escapeRE: function( str
) {
17 return str
.replace ( /([\\{}()|.?*+\-^$\[\]])/g, "\\$1" );
19 isDomElement: function( el
) {
20 return !!el
&& !!el
.nodeType
;
22 isEmpty: function( v
) {
23 if ( v
=== '' || v
=== 0 || v
=== '0' || v
=== null
24 || v
=== false || v
=== undefined )
28 // the for-loop could potentially contain prototypes
29 // to avoid that we check it's length first
30 if ( v
.length
=== 0 ) {
33 if ( typeof v
=== 'object' ) {
34 for ( var key
in v
) {
41 compareArray: function( arrThis
, arrAgainst
) {
42 if ( arrThis
.length
!= arrAgainst
.length
) {
45 for ( var i
= 0; i
< arrThis
.length
; i
++ ) {
46 if ( arrThis
[i
] instanceof Array
) {
47 if ( !$.compareArray( arrThis
[i
], arrAgainst
[i
] ) ) {
50 } else if ( arrThis
[i
] !== arrAgainst
[i
] ) {
56 compareObject: function( objectA
, objectB
) {
58 // Do a simple check if the types match
59 if ( typeof objectA
== typeof objectB
) {
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
) {
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
78 if ( !$.compareObject( objectA
[prop
], objectB
[prop
] ) ) {
83 // Functions need to be strings to compare them properly
84 if ( objectA
[prop
].toString() !== objectB
[prop
].toString() ) {
90 if ( objectA
[prop
] !== objectB
[prop
] ) {
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
) ) {