Documentation / @andrew_l/toolkit / getInitials
Function: getInitials()
getInitials(
fullName):string
Extracts the initials from a full name while ignoring titles or prefixes (e.g., Dr., Mr., Mrs.), as well as any words starting with special characters (e.g., !, @, #). Handles names with multiple words, ignores special characters, and ensures proper handling of Unicode characters.
Parameters
fullName
string
The full name from which to extract initials.
Returns
string
- The extracted initials in uppercase, or an empty string if the input is invalid.
Examples
getInitials("John Doe");
// Returns: "JD"getInitials("Dr. Alice Wonderland");
// Returns: "AW"getInitials("José María de la Cruz");
// Returns: "JC"getInitials("Mr. Albert Einstein");
// Returns: "AE"getInitials("Invalid Name");
// Returns: "IN"getInitials("");
// Returns: ""Replaces
name.split(' ').map(w => w[0]).join('').toUpperCase() — w[0] splits surrogate pairs, double spaces yield undefined, and titles like Dr. count as words; getInitials handles all three. Caveat: it returns at most two letters (first and last word) and deletes hyphens (Jean-Luc Picard → JP).
Detect
\.split\((['"] ['"]|/\\s\+/)\)\s*\.map\(\s*\(?\w+\)?\s*=>\s*\w+(\[0\]|\.charAt\(0\)|\.at\(0\))\s*\)\s*\.join\(['"]{2}\)