Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firefox 18+ supports String#startsWith, String#endsWith #96

Merged
merged 3 commits into from Mar 26, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/prototype/lang/string.js
Expand Up @@ -770,33 +770,47 @@ Object.extend(String.prototype, (function() {
}

/**
* String#startsWith(substring) -> Boolean
* String#startsWith(substring[, position]) -> Boolean
* - substring (String): The characters to be searched for at the start of this string.
* - [position] (Number): The position in this string at which to begin searching for `substring`; defaults to 0.
*
* Checks if the string starts with `substring`.
*
* ##### Example
*
* 'Prototype JavaScript'.startsWith('Pro');
* //-> true
* 'Prototype JavaScript'.startsWith('Java', 10);
* //-> true
**/
function startsWith(pattern) {
function startsWith(pattern, position) {
position = Object.isNumber(position) ? position : 0;
// We use `lastIndexOf` instead of `indexOf` to avoid tying execution
// time to string length when string doesn't start with pattern.
return this.lastIndexOf(pattern, 0) === 0;
return this.lastIndexOf(pattern, position) === position;
}

/**
* String#endsWith(substring) -> Boolean
* String#endsWith(substring[, position]) -> Boolean
* - substring (String): The characters to be searched for at the end of this string.
* - [position] (Number): Search within this string as if this string were only this long;
* defaults to this string's actual length, clamped within the range established by this string's length.
*
* Checks if the string ends with `substring`.
*
* ##### Example
*
* 'slaughter'.endsWith('laughter')
* // -> true
* 'slaughter'.endsWith('laugh', 6)
* // -> true
**/
function endsWith(pattern) {
var d = this.length - pattern.length;
function endsWith(pattern, position) {
pattern = String(pattern);
position = Object.isNumber(position) ? position : this.length;
if (position < 0) position = 0;
if (position > this.length) position = this.length;
var d = position - pattern.length;
// We use `indexOf` instead of `lastIndexOf` to avoid tying execution
// time to string length when string doesn't end with pattern.
return d >= 0 && this.indexOf(pattern, d) === d;
Expand Down Expand Up @@ -878,8 +892,9 @@ Object.extend(String.prototype, (function() {
isJSON: isJSON,
evalJSON: NATIVE_JSON_PARSE_SUPPORT ? parseJSON : evalJSON,
include: include,
startsWith: startsWith,
endsWith: endsWith,
// Firefox 18+ supports String.prototype.startsWith, String.prototype.endsWith
startsWith: String.prototype.startsWith || startsWith,
endsWith: String.prototype.endsWith || endsWith,
empty: empty,
blank: blank,
interpolate: interpolate
Expand Down
26 changes: 25 additions & 1 deletion test/unit/string_test.js
Expand Up @@ -430,6 +430,11 @@ new Test.Unit.Runner({
this.assert(!'hello world'.startsWith('bye'));
this.assert(!''.startsWith('bye'));
this.assert(!'hell'.startsWith('hello'));

var str = "To be, or not to be, that is the question";
this.assert(str.startsWith("To be"), 'str.startsWith("To be")');
this.assert(!str.startsWith("not to be"), 'str.startsWith("not to be")');
this.assert(str.startsWith("not to be", 10), 'str.startsWith("not to be", 10)');
},

testEndsWith: function() {
Expand All @@ -439,6 +444,25 @@ new Test.Unit.Runner({
this.assert(!''.endsWith('planet'));
this.assert('hello world world'.endsWith(' world'));
this.assert(!'z'.endsWith('az'));

var str = "To be, or not to be, that is the question";
this.assert(str.endsWith("question"), 'str.endsWith("question")');
this.assert(!str.endsWith("to be"), 'str.endsWith("to be")');
this.assert(str.endsWith("to be", 19), 'str.endsWith("to be", 19)');

str = "12345";
this.assert(str.endsWith("5"));
this.assert(str.endsWith("5", 6));
this.assert(str.endsWith("5", 5));
this.assert(!str.endsWith("5", 4));
this.assert(!str.endsWith("5", 1));
this.assert(!str.endsWith("5", 0));

this.assert(str.endsWith("1", 1));
this.assert(!str.endsWith("1", 0));
this.assert(!str.endsWith("1", -1));

this.assert(str.endsWith("", 0));
},

testBlank: function() {
Expand Down Expand Up @@ -551,4 +575,4 @@ new Test.Unit.Runner({
this.assertIdentical(false, 'false'.evalJSON());
this.assertEqual('"', '"\\""'.evalJSON());
}
});
});