UNPKG

761 BJavaScriptView Raw
1var randHex = require('./randHex');
2var choice = require('./choice');
3
4 /**
5 * Returns pseudo-random guid (UUID v4)
6 * IMPORTANT: it's not totally "safe" since randHex/choice uses Math.random
7 * by default and sequences can be predicted in some cases. See the
8 * "random/random" documentation for more info about it and how to replace
9 * the default PRNG.
10 */
11 function guid() {
12 return (
13 randHex(8)+'-'+
14 randHex(4)+'-'+
15 // v4 UUID always contain "4" at this position to specify it was
16 // randomly generated
17 '4' + randHex(3) +'-'+
18 // v4 UUID always contain chars [a,b,8,9] at this position
19 choice(8, 9, 'a', 'b') + randHex(3)+'-'+
20 randHex(12)
21 );
22 }
23 module.exports = guid;
24