mout
Version:
Modular Utilities
30 lines (28 loc) • 799 B
text/typescript
import choice from './choice';
import randHex from './randHex';
/**
* Returns pseudo-random guid (UUID v4)
* IMPORTANT: it's not totally "safe" since randHex/choice uses Math.random
* by default and sequences can be predicted in some cases. See the
* "random/random" documentation for more info about it and how to replace
* the default PRNG.
*/
function guid(): string {
return (
randHex(8) +
'-' +
randHex(4) +
'-' +
// v4 UUID always contain "4" at this position to specify it was
// randomly generated
'4' +
randHex(3) +
'-' +
// v4 UUID always contain chars [a,b,8,9] at this position
choice(8, 9, 'a', 'b') +
randHex(3) +
'-' +
randHex(12)
);
}
export default guid;