All files / core/blong-browser/src/lib initials.ts

62.5% Statements 5/8
37.5% Branches 3/8
100% Functions 1/1
60% Lines 3/5

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                28x 7x 7x        
/**
 * The two letters a nameless picture falls back to.
 *
 * One behaviour, two callers: the account menu's avatar and a blob that has no photograph yet. A name of
 * two or more words answers its first and last initial, a single long word answers its first two letters
 * — which is what makes `Jane Doe` read `JD` and `blong` read `BL` (@@D-842).
 */
export function deriveInitials(name: string | undefined | null): string | undefined {
    if (!name) return undefined;
    const parts = name.trim().split(/\s+/).filter(Boolean);
    Eif (parts.length >= 2) return (parts[0][0] + parts[parts.length - 1][0]).toUpperCase();
    if (parts[0] && parts[0].length >= 2) return parts[0].slice(0, 2).toUpperCase();
    return parts[0]?.[0]?.toUpperCase();
}