guid.js 1.2 KB

1234567891011121314151617181920212223242526
  1. /**
  2. * Implementation from http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#answer-2117523
  3. * Be aware Math.random() could cause collisions, but:
  4. * "All but 6 of the 128 bits of the ID are randomly generated, which means that for any two ids, there's a 1 in 2^^122 (or 5.3x10^^36) chance they'll collide"
  5. * @returns a pseudo random id
  6. */
  7. export function RandomGUID() {
  8. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
  9. const r = (Math.random() * 16) | 0, v = c === "x" ? r : (r & 0x3) | 0x8;
  10. return v.toString(16);
  11. });
  12. }
  13. /**
  14. * Class used to manipulate GUIDs
  15. */
  16. export const GUID = {
  17. /**
  18. * Implementation from http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#answer-2117523
  19. * Be aware Math.random() could cause collisions, but:
  20. * "All but 6 of the 128 bits of the ID are randomly generated, which means that for any two ids, there's a 1 in 2^^122 (or 5.3x10^^36) chance they'll collide"
  21. * @returns a pseudo random id
  22. */
  23. // eslint-disable-next-line @typescript-eslint/naming-convention
  24. RandomId: RandomGUID,
  25. };
  26. //# sourceMappingURL=guid.js.map