Skip characters that are not in any alphabet or number system.

pull/2223/head
Ian Macdonald 3 years ago
parent afad9f823a
commit 59d45b69a9
No known key found for this signature in database
GPG Key ID: AE4C20556BA626FA

@ -8,11 +8,14 @@ export function getInitials(name?: string): string {
return upperAndShorten(name[2]); return upperAndShorten(name[2]);
} }
if (name.indexOf(' ') === -1) { if (name.split(/[-\s]/).length === 1) {
// there is no space, just return the first 2 chars of the name // there is one word, so just return the first 2 alphanumeric chars of the name
if (name.length > 1) { if (name.length > 1) {
return upperAndShorten(name.slice(0, 2)); const alphanum = name.match(/[\p{L}\p{N}]+/u);
if (alphanum) {
return upperAndShorten(alphanum[0].slice(0, 2));
}
} }
return upperAndShorten(name[0]); return upperAndShorten(name[0]);
} }
@ -20,11 +23,12 @@ export function getInitials(name?: string): string {
// name has a space, just extract the first char of each words // name has a space, just extract the first char of each words
return upperAndShorten( return upperAndShorten(
name name
.split(' ') .split(/[-\s]/)
.slice(0, 2) .slice(0, 2)
.map(n => { .map(n =>
return n[0]; // Allow a letter or a digit from any alphabet.
}) n.match(/^[\p{L}\p{N}]/u)
)
.join('') .join('')
); );
} }

Loading…
Cancel
Save