solutaaliquid
Version:
common utils for javascript. Utils to get the global scope (browser, webworker & co.). Customizable string escaper. Utilitize for objects. Extension at base javascript api (usefull extention like String.hashcode()).
17 lines (14 loc) • 491 B
JavaScript
//the solution is found here: https://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid
export const UUID_SCHEMA = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
export const uuid = () => {
const buf = new Uint32Array(4);
window.crypto.getRandomValues(buf);
let idx = -1;
return UUID_SCHEMA.replace(/[xy]/g, (c) => {
idx++;
const r = (buf[idx >> 3] >> ((idx % 8) * 4)) & 15;
const v = c == "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
};
export default { uuid };