✅♻️🩹 backend: add company_id to editing_session_key (#525)
This commit is contained in:
@@ -12,3 +12,35 @@ export function timeuuidToDate(time_str: string): number {
|
||||
|
||||
return parseInt(time_string, 16);
|
||||
}
|
||||
|
||||
/** Remove `-`s from a formatted UUID to get a hex a string */
|
||||
export function hexFromFormatted(uuid: string) {
|
||||
const result = uuid.replace(/-+/g, "");
|
||||
if (result.length !== 32)
|
||||
throw new Error(`Invalid UUID (${JSON.stringify(uuid)}). Wrong number of digits`);
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Add `-`s back in a hex string to make a formatted UUID */
|
||||
export function formattedFromHex(hex: string) {
|
||||
const idMatch = hex.match(
|
||||
/^([a-z0-f]{8})([a-z0-f]{4})([a-z0-f]{4})([a-z0-f]{4})([a-z0-f]{12})$/i,
|
||||
);
|
||||
if (!idMatch)
|
||||
throw new Error(`Invalid UUID hex (${JSON.stringify(hex)}). Wrong number of digits`);
|
||||
const [, ...parts] = idMatch;
|
||||
return parts.join("-");
|
||||
}
|
||||
|
||||
/** Convert a UUID formatted or hex string into a binary buffer */
|
||||
export function bufferFromUUIDString(uuidOrHex: string) {
|
||||
return Buffer.from(hexFromFormatted(uuidOrHex), "hex");
|
||||
}
|
||||
|
||||
/** In a buffer with concatenated UUIDs in binary, extract the `index`th and return as formatted UUID */
|
||||
export function formattedUUIDInBufferArray(buffer: Buffer, index: number) {
|
||||
if (buffer.length < (index + 1) * 16)
|
||||
throw new Error(`Cannot get UUID ${JSON.stringify(index)} because the buffer is too small`);
|
||||
const slice = buffer.subarray(index * 16, (index + 1) * 16);
|
||||
return formattedFromHex(slice.toString("hex").toLowerCase());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user