1/**
2 * Convert array of 16 byte values to UUID string format of the form:
3 * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
4 */
5const byteToHex: string[] = [];
6for (let i = 0; i < 256; ++i) {
7  byteToHex[i] = (i + 0x100).toString(16).substr(1);
8}
9
10function bytesToUuid(buf: number[], offset?: number) {
11  let i = offset || 0;
12  const bth = byteToHex;
13  // join used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4
14  return [
15    bth[buf[i++]],
16    bth[buf[i++]],
17    bth[buf[i++]],
18    bth[buf[i++]],
19    '-',
20    bth[buf[i++]],
21    bth[buf[i++]],
22    '-',
23    bth[buf[i++]],
24    bth[buf[i++]],
25    '-',
26    bth[buf[i++]],
27    bth[buf[i++]],
28    '-',
29    bth[buf[i++]],
30    bth[buf[i++]],
31    bth[buf[i++]],
32    bth[buf[i++]],
33    bth[buf[i++]],
34    bth[buf[i++]],
35  ].join('');
36}
37
38export default bytesToUuid;
39