Coded Geekery

BlogAboutContact
BlogAboutContact

Tiny ID

A tiny, secure, URL-friendly, unique string ID generator

Tiny ID
const getRandomValues = (array) => { for (let i = 0, l = array.length; i < l; i++) { array[i] = Math.floor(Math.random() * 256); } return array; } const tinyid = (size=21) => { let e = ""; const r = getRandomValues( new Uint8Array(size) ); while ( size-- ) { let n = 63 & r[size]; e += n < 36 ? n.toString(36) : n < 62 ? (n - 26).toString(36).toUpperCase() : n < 63 ? "_" : "-" } return e }; console.log( tinyid() ) console.log( tinyid() ) console.log( tinyid() ) console.log( tinyid() ) console.log( tinyid() ) console.log( tinyid() )

Coded Geekery

© 2021 Roger Stringer. All rights reserved.

TwitterGitHubInstagram

Stuff

HomeAbout MeContact Me

Not Playing

Spotify