Skip to content

Instantly share code, notes, and snippets.

@VitorLuizC
Last active February 12, 2019 21:53
Show Gist options
  • Save VitorLuizC/26ebfd6053dcfac467fe7ccda458b20a to your computer and use it in GitHub Desktop.
Save VitorLuizC/26ebfd6053dcfac467fe7ccda458b20a to your computer and use it in GitHub Desktop.
JavaScript function to generate random IDs.
/**
* Generate random IDs.
* @param {{ size?: number, prefix?: string }} [options]
* @returns {string}
*/
export default function generateID ({ size = 8, prefix = 'id-' } = {}) {
const number = Math.floor(Math.random() * Number.MAX_VALUE);
return prefix + number.toString(16).substr(0, size).padEnd(size, '0');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment