Skip to content

Commit d3634a9

Browse files
committed
Updated README
1 parent 802bf29 commit d3634a9

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

README.md

+46
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,49 @@ Once we have found the first character, if the given string has `length === 1` i
1515

1616
If the next character to check is different than the character in `this` we leave from that second loop and we go for another match with the first character of the given word.
1717

18+
``` Javascript
19+
String.prototype.indexOfNew = function(searchValue, from = 0) {
20+
// Truncate `from` because we can't secure that it's an integer
21+
var _fromTruncated = Math.trunc(from);
22+
if (searchValue === '' && _fromTruncated < 0) {
23+
return 0;
24+
}
25+
26+
if (searchValue === '' && _fromTruncated >= this.length) {
27+
return this.length;
28+
}
29+
30+
if (searchValue === '') {
31+
return _fromTruncated;
32+
}
33+
34+
// GIVEN:
35+
// * A string `this`
36+
// * A string to find in `this`
37+
// * An initial position to start searching
38+
//
39+
// THEN:
40+
// * Returns -1 if the string to find has not been matched in `this`.
41+
// * Returns the position of the first occurence of the string in `this`
42+
for (var i = _fromTruncated; i < this.length; i++) {
43+
var thisCharsLeft = this.length - i;
44+
if (this[i] === searchValue[0]) {
45+
var initial = i;
46+
if (searchValue.length === 1) {
47+
return initial;
48+
49+
} else if (thisCharsLeft > searchValue.length - 1) {
50+
for (var j = 1; j < searchValue.length; j++) {
51+
if (this[++i] !== searchValue[j]) {
52+
break;
53+
} else if (j === (searchValue.length - 1)) {
54+
return initial;
55+
}
56+
}
57+
}
58+
}
59+
}
60+
61+
return -1;
62+
};
63+
```

0 commit comments

Comments
 (0)