Prototype: Add String.prototype.empty and String.prototype.blank (tests if empty or whitespace-only). Closes #7016. [Jonathan Viney, Thomas Fuchs]

This commit is contained in:
Thomas Fuchs 2007-02-19 21:32:37 +00:00
parent ab59de3949
commit 5224f0fc4f
4 changed files with 32 additions and 6 deletions

View File

@ -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]

View File

@ -282,7 +282,7 @@ Element.Methods = {
},
empty: function(element) {
return $(element).innerHTML.match(/^\s*$/);
return $(element).innerHTML.blank();
},
descendantOf: function(element, ancestor) {

View File

@ -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);
}
});

View File

@ -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());