All files / realm/blong-gateway/adapter/db gatewayUuid.ts

84.61% Statements 22/26
100% Branches 4/4
75% Functions 3/4
84.61% Lines 22/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 271x 1x 1x 1x 38x 38x 1x 1x 1x         1x 1x 1x 9x 9x 1x 1x 1x 9x 9x 9x 9x 9x  
import crypto from 'node:crypto';
 
/** Convert a UUID string to a BINARY(16) Buffer for MySQL. */
export function uuidBuf(uuid: string): Buffer {
    return Buffer.from(uuid.replace(/-/g, ''), 'hex');
}
 
/** Read a BINARY(16) value from MySQL and return a hex UUID string. */
export function bufToUuid(buf: Buffer | string): string {
    if (typeof buf === 'string') return buf;
    const hex = buf.toString('hex');
    return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
}
 
/** Generate a random UUID v4 string. */
export function newUuid(): string {
    return crypto.randomUUID();
}
 
/** Split a comma-separated string of names, trimming whitespace and filtering empty entries. */
export function splitNames(value: string): string[] {
    return value
        .split(',')
        .map(s => s.trim())
        .filter(Boolean);
}