guid.js 761 B

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