UtilAdded in v15.6Optional

wrInitials

Up to two initials of a name, one grapheme each. What wr-avatar draws for its name.

Usage

The first two words give one initial each. A one-word name gives one. Punctuation, extra whitespace, null and undefined never throw.

import { wrInitials } from 'ngwr/utils';

wrInitials('Roman Khegay');        // 'RK'
wrInitials('  ada   lovelace  ');  // 'AL'
wrInitials('Ada King Lovelace');   // 'AK'
wrInitials('Jean-Luc Picard');     // 'JP'
wrInitials('Madonna');             // 'M'
wrInitials(null);                  // ''

Scripts

A grapheme is what a reader sees as one character, so an emoji, a letter with marks or a surrogate pair is one initial, never half of one.

wrInitials('👩🏽‍🚀 Valentina');   // '👩🏽‍🚀V', one grapheme each
wrInitials('émile zola');         // 'ÉZ'
wrInitials('ßen müller');         // 'ßM', an upper case of two letters is kept as written
wrInitials('王小明');              // '王', one character for Han, Hangul and kana
wrInitials('محمد علي');           // 'مع'
wrInitials('ismail', 'tr');       // 'İ', the locale's mapping

Rules

InputResult
Three or more wordsThe first two: Ada King Lovelace gives AK.
One wordOne initial: Madonna gives M.
A hyphenInside a word: Jean-Luc Picard gives JP.
Punctuation firstSkipped: (Ada) gives A. A word of punctuation alone yields nothing.
DigitsCount: Agent 007 gives A0.
Han, Hangul, kana One character, the first: 王小明 gives 王, 김 철수 gives 김.
Upper case The default mapping, or the locale given. A letter whose upper case is two (ß) is kept as written.
Empty, blank, null''

API

NameDescriptionTypeDefault
wrInitials(name, locale?)Up to two initials, one grapheme each, upper-cased. A word is a run between whitespace; its initial is its first letter, digit or emoji. locale picks the upper-case mapping; without it the default mapping, the same on the server and the client.(name: string | null | undefined, locale?: string) => string-

See also