diff --git a/CHANGELOG b/CHANGELOG index f1cab33..8e7a79a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Add String.prototype.empty and String.prototype.blank (tests if empty or whitespace-only). Closes #7016. [Jonathan Viney, Thomas Fuchs] + * Update README to reflect new URLs, update LICENSE copyright years. Closes #7426. [Tobie Langel] * Array.prototype.uniq optimization. Closes #7417. [Christophe Porteneuve] diff --git a/src/dom.js b/src/dom.js index c02cddd..046f96f 100644 --- a/src/dom.js +++ b/src/dom.js @@ -282,7 +282,7 @@ Element.Methods = { }, empty: function(element) { - return $(element).innerHTML.match(/^\s*$/); + return $(element).innerHTML.blank(); }, descendantOf: function(element, ancestor) { diff --git a/src/string.js b/src/string.js index e3f10b3..5a575e0 100644 --- a/src/string.js +++ b/src/string.js @@ -1,4 +1,4 @@ -String.interpret = function(value){ +String.interpret = function(value) { return value == null ? '' : String(value); } @@ -122,7 +122,7 @@ Object.extend(String.prototype, { return camelized; }, - capitalize: function(){ + capitalize: function() { return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase(); }, @@ -142,16 +142,24 @@ Object.extend(String.prototype, { return "'" + escapedString.replace(/'/g, '\\\'') + "'"; }, - include: function(pattern){ + include: function(pattern) { return this.indexOf(pattern) > -1; }, - startsWith: function(pattern){ + startsWith: function(pattern) { return this.indexOf(pattern) == 0; }, - endsWith: function(pattern){ + endsWith: function(pattern) { return this.lastIndexOf(pattern) == (this.length - pattern.length); + }, + + empty: function() { + return this == ''; + }, + + blank: function() { + return /^\s*$/.test(this); } }); diff --git a/test/unit/string.html b/test/unit/string.html index 80c9ddb..1200890 100644 --- a/test/unit/string.html +++ b/test/unit/string.html @@ -346,6 +346,22 @@ assert('hello world world'.endsWith(' world')); }}, + testBlank: function() { with(this) { + assert(''.blank()); + assert(' '.blank()); + assert('\t\r\n '.blank()); + assert(!'a'.blank()); + assert(!'\t y \n'.blank()); + }}, + + testEmpty: function() { with(this) { + assert(''.empty()); + assert(!' '.empty()); + assert(!'\t\r\n '.empty()); + assert(!'a'.empty()); + assert(!'\t y \n'.empty()); + }}, + testSucc: function() {with(this) { assertEqual('b', 'a'.succ()); assertEqual('B', 'A'.succ());