Skip to content

Instantly share code, notes, and snippets.

@negarjf
Last active July 21, 2020 20:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save negarjf/65cc7263f6d485a06e8d286d2d51abcd to your computer and use it in GitHub Desktop.
Save negarjf/65cc7263f6d485a06e8d286d2d51abcd to your computer and use it in GitHub Desktop.
Generating unique random strings with optional prefix and postfix.
/**
* Generates random string id
*
* @param prefix
* @param postfix
* @returns {string}
*/
function generateId(prefix, postfix) {
prefix = prefix || "";
postfix = postfix || "";
let hashId = Date.now().toString(36);
return prefix + hashId + postfix;
}
@saeedseyfi
Copy link

if (generateId() === generateId()) {
    throw new Error('generateId does not generate unique random string')
}

😉

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