Skip to content

Instantly share code, notes, and snippets.

@amirnissim
Last active December 28, 2015 19:48
Show Gist options
  • Save amirnissim/7552416 to your computer and use it in GitHub Desktop.
Save amirnissim/7552416 to your computer and use it in GitHub Desktop.
String.prototype.indexOf = function(query, start) {
var streak = 0;
start = Math.min(start || 0, this.length - 1);
// TODO stop at this.length - query.length
for (var i = start; i < this.length; i++) {
if (this[i] === query[streak])
streak++;
else
streak = 0;
if (streak === query.length)
return i - streak + 1;
if (i === this.length - 1)
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment