@trellixio/roaster-coffee
Version:
Beans' product component library
22 lines (21 loc) • 673 B
JavaScript
/**
* Generates a unique identifier using a random combination of hexadecimal characters in the format of XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX..
*
* @example
* const myGuid = guid();
*
* @returns A unique identifier string in the format of XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.
*/
export function guid() {
/**
* Returns a random string of 4 hexadecimal characters.
*
* @returns A 4-character string of hexadecimal characters.
*/
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return `${s4() + s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`;
}