-
-
Save chilts/1141448 to your computer and use it in GitHub Desktop.
Generate unique alphanumeric incrementing string keys
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
#!/usr/bin/env node | |
var base_str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_"; | |
console.log( number_to_base(process.argv[2], base_str) ); | |
function number_to_base(n, str) { | |
if ( n === 0 ) return "0"; | |
var b = ''; | |
while ( n > 0 ) { | |
b = str[n%str.length] + b; | |
n = Math.floor(n / str.length); | |
} | |
return b; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment