Skip to content

Instantly share code, notes, and snippets.

@tobiamos
Last active January 15, 2019 21:57
Show Gist options
  • Save tobiamos/396ec5b5fd77a935fbe43ec64892b83e to your computer and use it in GitHub Desktop.
Save tobiamos/396ec5b5fd77a935fbe43ec64892b83e to your computer and use it in GitHub Desktop.
generate unique strings
const HashID = {};
HashID.generate = () => {
const alphabet = '23456789abdegjkmnpqrvwxyz';
const idLength = 9;
let result = '';
for (let index = 0; index < idLength; index += 1) {
result += alphabet.charAt(Math.floor(Math.random() * alphabet.length));
}
return result;
};
HashID.generateUnique = (previous = []) => {
let retries = 0;
const maxRetries = 9999;
let id;
while (!id && retries < maxRetries) {
id = HashID.generate();
if (previous.includes(id)) {
id = null;
retries += 1;
}
}
return id;
};
@karosi12
Copy link

Thank man

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment