adding isEmpty to jQuery prototype
authorKrinkle <krinkle@users.mediawiki.org>
Tue, 23 Nov 2010 01:36:58 +0000 (01:36 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Tue, 23 Nov 2010 01:36:58 +0000 (01:36 +0000)
resources/mediawiki.util/mediawiki.util.js
resources/mediawiki.util/mediawiki.util.test.js
resources/mediawiki/mediawiki.js

index fc948db..f7beea4 100644 (file)
                        return wgServer + wgArticlePath.replace( '$1', this.wikiUrlencode( str ) );
                },
 
-               /**
-                * Check is a variable is empty. Supports strings, booleans, arrays and
-                * objects. The string "0" is considered empty. A string containing only
-                * whitespace (ie. " ") is considered not empty.
-                *
-                * @param v The variable to check for emptyness
-                */
-               'isEmpty' : function( v ) {
-                       var key;
-                       if ( v === "" || v === 0 || v === "0" || v === null
-                               || v === false || typeof v === 'undefined' )
-                       {
-                               return true;
-                       }
-                       if ( v.length === 0 ) {
-                               return true;
-                       }
-                       if ( typeof v === 'object' ) {
-                               for ( key in v ) {
-                                       return false;
-                               }
-                               return true;
-                       }
-                       return false;
-               },
-
-
                /**
                 * Grab the URL parameter value for the given parameter.
                 * Returns null if not found.
index dc4eab6..3520db3 100644 (file)
                                                        'function (string)');
                                                mw.test.addTest('$.trimRight(\'  foo bar  \')',
                                                        '  foo bar (string)');
+                                               mw.test.addTest('typeof $.isEmpty',
+                                                       'function (string)');
+                                               mw.test.addTest('$.isEmpty(\'string\')',
+                                                       'false (boolean)');
+                                               mw.test.addTest('$.isEmpty(\'0\')',
+                                                       'true (boolean)');
+                                               mw.test.addTest('$.isEmpty([])',
+                                                       'true (boolean)');
                                                mw.test.addTest('typeof $.compareArray',
                                                        'function (string)');
                                                mw.test.addTest('$.compareArray( [1, "a", [], [2, \'b\'] ], [1, \'a\', [], [2, "b"] ] )',
index 6e285ac..5793dfc 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * JavaScript backwards-compatibility alternatives and convenience functions
+ * JavaScript backwards-compatibility alternatives and other convenience functions
  */
 
 jQuery.extend({
@@ -16,6 +16,26 @@ jQuery.extend({
        escapeRE : function( str ) {
                return str.replace ( /([\\{}()|.?*+^$\[\]])/g, "\\$1" );
        },
+       isEmpty : function( v ) {
+               var key;
+               if ( v === "" || v === 0 || v === "0" || v === null
+                       || v === false || typeof v === 'undefined' )
+               {
+                       return true;
+               }
+               // the for-loop could potentially contain prototypes
+               // to avoid that we check it's length first
+               if ( v.length === 0 ) {
+                       return true;
+               }
+               if ( typeof v === 'object' ) {
+                       for ( key in v ) {
+                               return false;
+                       }
+                       return true;
+               }
+               return false;
+       },
        compareArray : function( arrThis, arrAgainst ) {
                if ( arrThis.length != arrAgainst.length ) {
                        return false;