Last active
February 12, 2019 21:53
-
-
Save VitorLuizC/26ebfd6053dcfac467fe7ccda458b20a to your computer and use it in GitHub Desktop.
JavaScript function to generate random IDs.
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
/** | |
* 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