Last active
May 14, 2016 10:53
-
-
Save rsoury/53aa923833c79574ce379c2470397b98 to your computer and use it in GitHub Desktop.
This function allows you to pass in a string, and it will increment it in a URL friendly manner. It can be useful for link shorteners, new chat string identifiers, etc.
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 incUrlString(str){ | |
var singles = str.split(''); | |
var incNext = false; | |
var extendIt = false; | |
for(var i = singles.length - 1; i > -1; i --){ | |
if(incNext || i == singles.length - 1){ | |
incNext = false; | |
var ascii = singles[i].charCodeAt(0); | |
if(ascii == 57){ | |
ascii = 65; | |
}else if(ascii == 90){ | |
ascii = 97; | |
}else if(ascii == 122){ | |
ascii = 48; | |
incNext = true; | |
}else{ | |
ascii++; | |
} | |
if(i == 0 && incNext){ | |
extendIt = true; | |
} | |
singles[i] = String.fromCharCode(ascii); | |
} | |
} | |
var newStr = singles.join(''); | |
if(extendIt == true){ | |
newStr = 'A' + newStr; | |
} | |
return newStr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment