extractInitials without Regex lookbehind

Signed-off-by: Taras Mankovski <taras@frontside.com>
This commit is contained in:
Taras Mankovski
2023-10-10 18:05:23 -04:00
parent e1bf2a5531
commit 30d601f152
2 changed files with 8 additions and 6 deletions
@@ -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);
}