prototype: Add String.prototype.startsWith, String.prototype.endsWith, and String.prototype.include. Closes #7075.

This commit is contained in:
Sam Stephenson 2007-01-27 18:45:54 +00:00
parent c556c89627
commit 5345085b33
4 changed files with 38 additions and 1 deletions

View File

@ -1,5 +1,7 @@
*SVN*
* Add String.prototype.startsWith, String.prototype.endsWith, and String.prototype.include. Closes #7075. [Tobie Langel]
* Improve performance of String.prototype.escapeHTML by using a cached div and text node. Closes #6937. [altblue]
* Make setStyle() with opacity: 0 in Internet Explorer work correctly. [Thomas Fuchs]

View File

@ -95,7 +95,7 @@ Ajax.Request.prototype = Object.extend(new Ajax.Base(), {
// when GET, append parameters to URL
if (this.method == 'get' && params)
this.url += (this.url.indexOf('?') > -1 ? '&' : '?') + params;
this.url += (this.url.include('?') ? '&' : '?') + params;
try {
Ajax.Responders.dispatch('onCreate', this, this.transport);

View File

@ -140,6 +140,18 @@ Object.extend(String.prototype, {
return '"' + escapedString.replace(/"/g, '\\"') + '"';
else
return "'" + escapedString.replace(/'/g, '\\\'') + "'";
},
include: function(pattern){
return this.indexOf(pattern) > -1;
},
startsWith: function(pattern){
return this.indexOf(pattern) == 0;
},
endsWith: function(pattern){
return this.indexOf(pattern) == (this.length - pattern.length);
}
});

View File

@ -322,6 +322,29 @@
assertEqual('\'test \\\'test\\\' "test"\'', 'test \'test\' "test"'.inspect());
}},
testInclude: function() {with(this) {
assert('hello world'.include('h'));
assert('hello world'.include('hello'));
assert('hello world'.include('llo w'));
assert('hello world'.include('world'));
assert(!'hello world'.include('bye'));
assert(!''.include('bye'));
}},
testStartsWith: function() {with(this) {
assert('hello world'.startsWith('h'));
assert('hello world'.startsWith('hello'));
assert(!'hello world'.startsWith('bye'));
assert(!''.startsWith('bye'));
}},
testEndsWith: function() {with(this) {
assert('hello world'.endsWith('d'));
assert('hello world'.endsWith(' world'));
assert(!'hello world'.endsWith('planet'));
assert(!''.endsWith('planet'));
}},
testSucc: function() {with(this) {
assertEqual('b', 'a'.succ());
assertEqual('B', 'A'.succ());