Last active
January 10, 2018 10:22
-
-
Save adamabernathy/4047f2e79b05af2d5f8b23e0748ac1a2 to your computer and use it in GitHub Desktop.
Generates a random key name such as "yfva-pyxd-qxfv-ftjr"
This file contains hidden or 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
/** | |
* Generates a random key name such as "yfva-pyxd-qxfv-ftjr" | |
* @param {number} quartets | |
* @returns {string} | |
* | |
* (C) 2018 Adam C. Abernathy, [email protected]. Licence MIT | |
* https://gist.github.com/adamabernathy/4047f2e79b05af2d5f8b23e0748ac1a2 | |
*/ | |
const randomKey = (quartets = 4) => { | |
const f = () => Math.floor(Math.random() * 26) + 97; // Lowercase charcodes: 97->122 | |
return Array.apply(null, { length: quartets }) | |
.map(Number.call, Number) | |
.map(k => String.fromCharCode(f(), f(), f(), f())) | |
.join('-'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment