getUuid.ts 852 B

12345678910111213141516171819202122232425262728
  1. export function uuid(len = 5, radix = 62) {
  2. const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  3. let uuid = [], i;
  4. radix = radix || chars.length;
  5. if (len) {
  6. // Compact form
  7. for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  8. } else {
  9. // rfc4122, version 4 form
  10. let r;
  11. // rfc4122 requires these characters
  12. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  13. uuid[14] = '4';
  14. // Fill in random data. At i==19 set the high bits of clock sequence as
  15. // per rfc4122, sec. 4.1.5
  16. for (i = 0; i < 36; i++) {
  17. if (!uuid[i]) {
  18. r = 0 | Math.random() * 16;
  19. uuid[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r];
  20. }
  21. }
  22. }
  23. return uuid.join('');
  24. }