diff --git a/CHANGELOG b/CHANGELOG index 2d19d2d..7be5966 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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] diff --git a/src/ajax.js b/src/ajax.js index 0ee1632..518dedb 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -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); diff --git a/src/string.js b/src/string.js index 8449934..75e72f1 100644 --- a/src/string.js +++ b/src/string.js @@ -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); } }); diff --git a/test/unit/string.html b/test/unit/string.html index 9322932..00829d0 100644 --- a/test/unit/string.html +++ b/test/unit/string.html @@ -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());