All files / packages / utils/src string.ts

100% Statements 4/4
100% Branches 3/3
100% Functions 1/1
100% Lines 4/4

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    6x 6x   6x   6x    
// "Nina Park" → "NP"; one word → its first two letters.
export function initials(name: string): string {
  const [first = '', ...rest] = name.trim().split(/\s+/).filter(Boolean)
  const last = rest.at(-1)
  // Spread, not index: a letter outside the basic plane is two UTF-16 units.
  const letters = last ? [[...first][0], [...last][0]] : [...first].slice(0, 2)
 
  return letters.join('').toUpperCase()
}