@@ -770,33 +770,47 @@ Object.extend(String.prototype, (function() {
770
770
}
771
771
772
772
/**
773
- * String#startsWith(substring) -> Boolean
773
+ * String#startsWith(substring[, position]) -> Boolean
774
+ * - substring (String): The characters to be searched for at the start of this string.
775
+ * - [position] (Number): The position in this string at which to begin searching for `substring`; defaults to 0.
774
776
*
775
777
* Checks if the string starts with `substring`.
776
778
*
777
779
* ##### Example
778
780
*
779
781
* 'Prototype JavaScript'.startsWith('Pro');
780
782
* //-> true
783
+ * 'Prototype JavaScript'.startsWith('Java', 10);
784
+ * //-> true
781
785
**/
782
- function startsWith ( pattern ) {
786
+ function startsWith ( pattern , position ) {
787
+ position = Object . isNumber ( position ) ? position : 0 ;
783
788
// We use `lastIndexOf` instead of `indexOf` to avoid tying execution
784
789
// time to string length when string doesn't start with pattern.
785
- return this . lastIndexOf ( pattern , 0 ) === 0 ;
790
+ return this . lastIndexOf ( pattern , position ) === position ;
786
791
}
787
792
788
793
/**
789
- * String#endsWith(substring) -> Boolean
794
+ * String#endsWith(substring[, position]) -> Boolean
795
+ * - substring (String): The characters to be searched for at the end of this string.
796
+ * - [position] (Number): Search within this string as if this string were only this long;
797
+ * defaults to this string's actual length, clamped within the range established by this string's length.
790
798
*
791
799
* Checks if the string ends with `substring`.
792
800
*
793
801
* ##### Example
794
802
*
795
803
* 'slaughter'.endsWith('laughter')
796
804
* // -> true
805
+ * 'slaughter'.endsWith('laugh', 6)
806
+ * // -> true
797
807
**/
798
- function endsWith ( pattern ) {
799
- var d = this . length - pattern . length ;
808
+ function endsWith ( pattern , position ) {
809
+ pattern = String ( pattern ) ;
810
+ position = Object . isNumber ( position ) ? position : this . length ;
811
+ if ( position < 0 ) position = 0 ;
812
+ if ( position > this . length ) position = this . length ;
813
+ var d = position - pattern . length ;
800
814
// We use `indexOf` instead of `lastIndexOf` to avoid tying execution
801
815
// time to string length when string doesn't end with pattern.
802
816
return d >= 0 && this . indexOf ( pattern , d ) === d ;
0 commit comments