Last active
December 10, 2015 21:48
-
-
Save timReynolds/4496938 to your computer and use it in GitHub Desktop.
JavaScript Increment string function based on @dandoescode php inc function for Scrapyrd. Original inc function can be found here https://github.com/dandoescode/scrapyrd/blob/master/fuel/app/classes/scrapyrd.php
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
function setCharAt(str,index,chr) { | |
if(index > str.length-1) return str; | |
return str.substr(0,index) + chr + str.substr(index+1); | |
} | |
function incString(n, pos) { | |
if(!pos) {pos = 0}; | |
var set = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' | |
, setmax = 61; | |
if(n.length==0){ | |
return set[0]; | |
} | |
var nindex = n.length - 1 - pos | |
, char = n[nindex] | |
, setindex = set.indexOf(char); | |
if (nindex < 0) { return set[0]+n }; | |
if(setindex == setmax) { | |
return incString(setCharAt(n,nindex,set[0]), ++pos); | |
} else { | |
return setCharAt(n,nindex,set[++setindex]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment