Skip to content

Instantly share code, notes, and snippets.

@adamabernathy
Last active January 10, 2018 10:22
Show Gist options
  • Save adamabernathy/4047f2e79b05af2d5f8b23e0748ac1a2 to your computer and use it in GitHub Desktop.
Save adamabernathy/4047f2e79b05af2d5f8b23e0748ac1a2 to your computer and use it in GitHub Desktop.
Generates a random key name such as "yfva-pyxd-qxfv-ftjr"
/**
* 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