Skip to content

Instantly share code, notes, and snippets.

@justin-schroeder
Created January 25, 2021 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justin-schroeder/78581bc2e892801c954aa3ccb4cf8eb9 to your computer and use it in GitHub Desktop.
Save justin-schroeder/78581bc2e892801c954aa3ccb4cf8eb9 to your computer and use it in GitHub Desktop.
Random id, token, uuid generator (or whatever you want to call it)
/**
* Prouces a random token like token(5) = 1a4c9
* @param n {number} The length of the token
* @returns {string}
*/
const token = (n: number) =>
String.fromCharCode(
...new Array(n)
.fill(0)
.map(
(_n, i) => [48, 97][i % 2] + Math.floor(Math.random() * [10, 25][i % 2])
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment