uuid.ts 710 B

12345678910111213141516171819202122232425262728
  1. const hexList: string[] = [];
  2. for (let i = 0; i <= 15; i++) {
  3. hexList[i] = i.toString(16);
  4. }
  5. export function buildUUID(): string {
  6. let uuid = '';
  7. for (let i = 1; i <= 36; i++) {
  8. if (i === 9 || i === 14 || i === 19 || i === 24) {
  9. uuid += '-';
  10. } else if (i === 15) {
  11. uuid += 4;
  12. } else if (i === 20) {
  13. uuid += hexList[(Math.random() * 4) | 8];
  14. } else {
  15. uuid += hexList[(Math.random() * 16) | 0];
  16. }
  17. }
  18. return uuid.replace(/-/g, '');
  19. }
  20. let unique = 0;
  21. export function buildShortUUID(prefix = ''): string {
  22. const time = Date.now();
  23. const random = Math.floor(Math.random() * 1000000000);
  24. unique++;
  25. return prefix + '_' + random + unique + String(time);
  26. }