guid.ts 738 B

1234567891011121314151617
  1. /**
  2. * Class used to manipulate GUIDs
  3. */
  4. export class GUID {
  5. /**
  6. * Implementation from http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#answer-2117523
  7. * Be aware Math.random() could cause collisions, but:
  8. * "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"
  9. * @returns a pseudo random id
  10. */
  11. public static RandomId(): string {
  12. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
  13. var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
  14. return v.toString(16);
  15. });
  16. }
  17. }