uuid.ts 571 B

12345678910111213141516171819
  1. /**
  2. * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import now from './time'
  7. export default interface UUID extends String { '@type': 'uuid' }
  8. export function newUUID(): UUID {
  9. let d = (+new Date()) + now();
  10. const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  11. const r = (d + Math.random()*16)%16 | 0;
  12. d = Math.floor(d/16);
  13. return (c==='x' ? r : (r&0x3|0x8)).toString(16);
  14. });
  15. return uuid as any;
  16. }