Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #96 from victor-homyakov/patch-33

Use native `String#startsWith` and `String#endsWith` if present. Turn existing implementation into a true polyfill.
  • Loading branch information...
commit adb6479cd07f35cb610d4f0a1644bf20aefdcb5d 2 parents 4ce0b0f + bc3ac95
Andrew Dupont savetheclocktower authored
Showing with 48 additions and 9 deletions.
  1. +23 −8 src/prototype/lang/string.js
  2. +25 −1 test/unit/string_test.js
31 src/prototype/lang/string.js
View
@@ -770,7 +770,9 @@ 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`.
*
@@ -778,15 +780,21 @@ Object.extend(String.prototype, (function() {
*
* '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`.
*
@@ -794,9 +802,15 @@ Object.extend(String.prototype, (function() {
*
* '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;
@@ -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
26 test/unit/string_test.js
View
@@ -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() {
@@ -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() {
@@ -551,4 +575,4 @@ new Test.Unit.Runner({
this.assertIdentical(false, 'false'.evalJSON());
this.assertEqual('"', '"\\""'.evalJSON());
}
-});
+});
Please sign in to comment.
Something went wrong with that request. Please try again.