Fixes for rendering initials in the avatar component
Signed-off-by: Stephen Glass <stephen@stephen.glass>
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/core-components': patch
|
||||
---
|
||||
|
||||
Fixes for rendering initials in the avatar component.
|
||||
@@ -42,4 +42,24 @@ describe('extractInitials', () => {
|
||||
it('removes spaces from beginning or the end', () => {
|
||||
expect(extractInitials(' John Jonathan Doe ')).toEqual('JD');
|
||||
});
|
||||
|
||||
it('handles any sequence of whitespace between words', () => {
|
||||
expect(extractInitials('John\tJonathan Doe')).toEqual('JD');
|
||||
expect(extractInitials(' John\nDoe ')).toEqual('JD');
|
||||
expect(extractInitials('John\r\nDoe')).toEqual('JD');
|
||||
});
|
||||
|
||||
it('removes bracketed content from initials', () => {
|
||||
expect(extractInitials('John Doe (jd1234)')).toEqual('JD');
|
||||
expect(extractInitials('Jane Smith [js5678]')).toEqual('JS');
|
||||
expect(extractInitials('Alice (admin) Johnson')).toEqual('AJ');
|
||||
expect(extractInitials('(admin) Alice Johnson')).toEqual('AJ');
|
||||
});
|
||||
|
||||
it('removes non-letter characters from initials', () => {
|
||||
expect(extractInitials('John D0e!')).toEqual('JD');
|
||||
expect(extractInitials("Ann-Marie O'Neil")).toEqual('AO');
|
||||
expect(extractInitials('Élodie Brûlé!')).toEqual('ÉB');
|
||||
expect(extractInitials('Doe1231*')).toEqual('D');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -28,7 +28,13 @@ export function stringToColor(str: string) {
|
||||
}
|
||||
|
||||
export function extractInitials(name: string) {
|
||||
const names = name.trim().split(' ');
|
||||
const names = name
|
||||
.replace(/\([^)]{0,1000}\)/g, '')
|
||||
.replace(/\[[^\]]{0,1000}\]/g, '')
|
||||
.trim()
|
||||
.split(/\s+/)
|
||||
.map(word => word.replace(/[^A-Za-z\u00C0-\u017F]/g, ''))
|
||||
.filter(Boolean);
|
||||
const firstName = names[0] ?? '';
|
||||
const lastName = names.length > 1 ? names[names.length - 1] : '';
|
||||
return firstName && lastName
|
||||
|
||||
Reference in New Issue
Block a user