Skip to content

Commit

Permalink
prototype: Add String.prototype.startsWith, String.prototype.endsWith…
Browse files Browse the repository at this point in the history
…, and String.prototype.include. Closes #7075.
  • Loading branch information
sstephenson committed Jan 27, 2007
1 parent c556c89 commit 5345085
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions src/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
23 changes: 23 additions & 0 deletions test/unit/string.html
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,29 @@ <h1>Prototype Unit test file</h1>
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());
Expand Down

0 comments on commit 5345085

Please sign in to comment.