Skip to content

Instantly share code, notes, and snippets.

@timReynolds
Last active December 10, 2015 21:48
Show Gist options
  • Save timReynolds/4496938 to your computer and use it in GitHub Desktop.
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
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