Merge remote-tracking branch 'i/pr9' into get-initials-skip-non-alphabet-chars

pull/2269/head
Audric Ackermann 3 years ago
commit 5c9c7173f0
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -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