parse.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import validate from './validate.js';
  2. function parse(uuid) {
  3. if (!validate(uuid)) {
  4. throw TypeError('Invalid UUID');
  5. }
  6. var v;
  7. var arr = new Uint8Array(16); // Parse ########-....-....-....-............
  8. arr[0] = (v = parseInt(uuid.slice(0, 8), 16)) >>> 24;
  9. arr[1] = v >>> 16 & 0xff;
  10. arr[2] = v >>> 8 & 0xff;
  11. arr[3] = v & 0xff; // Parse ........-####-....-....-............
  12. arr[4] = (v = parseInt(uuid.slice(9, 13), 16)) >>> 8;
  13. arr[5] = v & 0xff; // Parse ........-....-####-....-............
  14. arr[6] = (v = parseInt(uuid.slice(14, 18), 16)) >>> 8;
  15. arr[7] = v & 0xff; // Parse ........-....-....-####-............
  16. arr[8] = (v = parseInt(uuid.slice(19, 23), 16)) >>> 8;
  17. arr[9] = v & 0xff; // Parse ........-....-....-....-############
  18. // (Use "/" to avoid 32-bit truncation when bit-shifting high-order bytes)
  19. arr[10] = (v = parseInt(uuid.slice(24, 36), 16)) / 0x10000000000 & 0xff;
  20. arr[11] = v / 0x100000000 & 0xff;
  21. arr[12] = v >>> 24 & 0xff;
  22. arr[13] = v >>> 16 & 0xff;
  23. arr[14] = v >>> 8 & 0xff;
  24. arr[15] = v & 0xff;
  25. return arr;
  26. }
  27. export default parse;