uuid.ts 629 B

1234567891011121314151617181920212223
  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 'mol-task'
  7. type UUID = string & { '@type': 'uuid' }
  8. namespace UUID {
  9. export function create(): UUID {
  10. let d = (+new Date()) + now();
  11. const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  12. const r = (d + Math.random()*16)%16 | 0;
  13. d = Math.floor(d/16);
  14. return (c==='x' ? r : (r&0x3|0x8)).toString(16);
  15. });
  16. return uuid as any;
  17. }
  18. }
  19. export default UUID;