r77127 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r77126‎ | r77127 | r77128 >
Date:01:36, 23 November 2010
Author:krinkle
Status:ok
Tags:
Comment:
adding isEmpty to jQuery prototype
Modified paths:
  • /trunk/phase3/resources/mediawiki.util/mediawiki.util.js (modified) (history)
  • /trunk/phase3/resources/mediawiki.util/mediawiki.util.test.js (modified) (history)
  • /trunk/phase3/resources/mediawiki/mediawiki.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/mediawiki.util/mediawiki.util.test.js
@@ -60,6 +60,14 @@
6161 'function (string)');
6262 mw.test.addTest('$.trimRight(\' foo bar \')',
6363 ' foo bar (string)');
 64+ mw.test.addTest('typeof $.isEmpty',
 65+ 'function (string)');
 66+ mw.test.addTest('$.isEmpty(\'string\')',
 67+ 'false (boolean)');
 68+ mw.test.addTest('$.isEmpty(\'0\')',
 69+ 'true (boolean)');
 70+ mw.test.addTest('$.isEmpty([])',
 71+ 'true (boolean)');
6472 mw.test.addTest('typeof $.compareArray',
6573 'function (string)');
6674 mw.test.addTest('$.compareArray( [1, "a", [], [2, \'b\'] ], [1, \'a\', [], [2, "b"] ] )',
Index: trunk/phase3/resources/mediawiki.util/mediawiki.util.js
@@ -128,33 +128,6 @@
129129 },
130130
131131 /**
132 - * Check is a variable is empty. Supports strings, booleans, arrays and
133 - * objects. The string "0" is considered empty. A string containing only
134 - * whitespace (ie. " ") is considered not empty.
135 - *
136 - * @param v The variable to check for emptyness
137 - */
138 - 'isEmpty' : function( v ) {
139 - var key;
140 - if ( v === "" || v === 0 || v === "0" || v === null
141 - || v === false || typeof v === 'undefined' )
142 - {
143 - return true;
144 - }
145 - if ( v.length === 0 ) {
146 - return true;
147 - }
148 - if ( typeof v === 'object' ) {
149 - for ( key in v ) {
150 - return false;
151 - }
152 - return true;
153 - }
154 - return false;
155 - },
156 -
157 -
158 - /**
159132 * Grab the URL parameter value for the given parameter.
160133 * Returns null if not found.
161134 *
Index: trunk/phase3/resources/mediawiki/mediawiki.js
@@ -1,5 +1,5 @@
22 /*
3 - * JavaScript backwards-compatibility alternatives and convenience functions
 3+ * JavaScript backwards-compatibility alternatives and other convenience functions
44 */
55
66 jQuery.extend({
@@ -16,6 +16,26 @@
1717 escapeRE : function( str ) {
1818 return str.replace ( /([\\{}()|.?*+^$\[\]])/g, "\\$1" );
1919 },
 20+ isEmpty : function( v ) {
 21+ var key;
 22+ if ( v === "" || v === 0 || v === "0" || v === null
 23+ || v === false || typeof v === 'undefined' )
 24+ {
 25+ return true;
 26+ }
 27+ // the for-loop could potentially contain prototypes
 28+ // to avoid that we check it's length first
 29+ if ( v.length === 0 ) {
 30+ return true;
 31+ }
 32+ if ( typeof v === 'object' ) {
 33+ for ( key in v ) {
 34+ return false;
 35+ }
 36+ return true;
 37+ }
 38+ return false;
 39+ },
2040 compareArray : function( arrThis, arrAgainst ) {
2141 if ( arrThis.length != arrAgainst.length ) {
2242 return false;

Status & tagging log