extractInitials without Regex lookbehind
Signed-off-by: Taras Mankovski <taras@frontside.com>
This commit is contained in:
@@ -36,6 +36,6 @@ describe('extractInitials', () => {
|
||||
});
|
||||
|
||||
it('limit the initials to two letters', async () => {
|
||||
expect(extractInitials('John Jonathan Doe')).toEqual('JJ');
|
||||
expect(extractInitials('John Jonathan Doe')).toEqual('JD');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -27,9 +27,11 @@ export function stringToColor(str: string) {
|
||||
return color;
|
||||
}
|
||||
|
||||
export function extractInitials(value: string) {
|
||||
return value
|
||||
.match(/(?<!\p{L})\p{L}/gu)
|
||||
?.join('')
|
||||
.slice(0, 2);
|
||||
export function extractInitials(name: string) {
|
||||
const names = name.split(' ');
|
||||
const firstName = names[0] ?? '';
|
||||
const lastName = names.length > 1 ? names[names.length - 1] : '';
|
||||
return firstName && lastName
|
||||
? `${firstName.charAt(0)}${lastName.charAt(0)}`
|
||||
: firstName.charAt(0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user