Created
March 28, 2014 10:49
-
-
Save detomon/9830030 to your computer and use it in GitHub Desktop.
JavaScript implementation of the PHP function `strspn`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.strspn = function (chars, start, length) { | |
var end; | |
if (start == undefined) | |
start = 0; | |
else if (start < 0) | |
start += this.length; | |
if (length == undefined) | |
length = this.length; | |
start = Math.max(0, Math.min(start, this.length)); | |
end = Math.max(start, Math.min(start + length, this.length)); | |
for (var i = start; i < end; i ++) { | |
if (chars.indexOf(this[i]) == -1) | |
return i - start; | |
} | |
return end - start; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks