Skip to content

Instantly share code, notes, and snippets.

@oelna
Last active September 23, 2022 15:46
Show Gist options
  • Save oelna/e5846485f604ae2fe0ecac0e4a6aeef3 to your computer and use it in GitHub Desktop.
Save oelna/e5846485f604ae2fe0ecac0e4a6aeef3 to your computer and use it in GitHub Desktop.
Collection of hash algorithms for frontend/Javascript development

Hash and UID tools

hash

unique id

// arbitrary length numeric value
const uid = (length=16) => {
    return parseInt(Math.ceil(Math.random() * Date.now()).toPrecision(length).toString().replace(".", ""));
}
// long numeric value
const uid = function () {
    return Date.now().toString() + Math.floor(Math.pow(10, 12) + Math.random() * 9*Math.pow(10, 12));
}
// UUID v4 eg. 9f3bbc61-26ca-447a-89ac-f5c14d34d1cb
function uuidv4 () {
	return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
		(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
	);
}

slug

further reading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment