2022-02-02 14:21:39 +00:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2022-03-01 04:25:50 +00:00
|
|
|
// This module is browser compatible.
|
|
|
|
|
2020-10-01 09:40:40 +00:00
|
|
|
/**
|
|
|
|
* Converts the byte array to a UUID string
|
|
|
|
* @param bytes Used to convert Byte to Hex
|
|
|
|
*/
|
2020-04-15 14:38:05 +00:00
|
|
|
export function bytesToUuid(bytes: number[] | Uint8Array): string {
|
2021-02-02 03:16:27 +00:00
|
|
|
const bits = [...bytes].map((bit) => {
|
|
|
|
const s = bit.toString(16);
|
2020-04-15 14:38:05 +00:00
|
|
|
return bit < 0x10 ? "0" + s : s;
|
|
|
|
});
|
|
|
|
return [
|
|
|
|
...bits.slice(0, 4),
|
|
|
|
"-",
|
|
|
|
...bits.slice(4, 6),
|
|
|
|
"-",
|
|
|
|
...bits.slice(6, 8),
|
|
|
|
"-",
|
|
|
|
...bits.slice(8, 10),
|
|
|
|
"-",
|
2020-04-27 12:49:34 +00:00
|
|
|
...bits.slice(10, 16),
|
2020-04-15 14:38:05 +00:00
|
|
|
].join("");
|
|
|
|
}
|
2020-04-27 12:49:34 +00:00
|
|
|
|
2020-10-01 09:40:40 +00:00
|
|
|
/**
|
2021-02-02 03:16:27 +00:00
|
|
|
* Converts a string to a byte array by converting the hex value to a number.
|
|
|
|
* @param uuid Value that gets converted.
|
2020-10-01 09:40:40 +00:00
|
|
|
*/
|
2020-04-27 12:49:34 +00:00
|
|
|
export function uuidToBytes(uuid: string): number[] {
|
|
|
|
const bytes: number[] = [];
|
|
|
|
|
|
|
|
uuid.replace(/[a-fA-F0-9]{2}/g, (hex: string): string => {
|
|
|
|
bytes.push(parseInt(hex, 16));
|
|
|
|
return "";
|
|
|
|
});
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
}
|