Tiny ID

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
};

Why?

Tiny ID is a tiny, secure, URL-friendly, unique string ID generator for JavaScript.

Usage

const id = tinyid()
console.log( id ) //=> "YJ7WY29qTaZQy25UxyAxS"
console.log( tinyid() ) //=> "5MEdYGh_pRqU5wVZrRwm2"
console.log( tinyid() ) //=> "UsJf5JEtBmjV2ggs87yx0"
console.log( tinyid() ) //=> "QA33rKAF_SNyTeTFWplD7"
console.log( tinyid() ) //=> "_yxCIwNzmwJO_tn-ToaJL"
console.log( tinyid() ) //=> "zwkWoOFLQZEV3SBoaX_g3"

Do you like my content?

Sponsor Me On Github