Added trim, test and removed unnecessary async

Signed-off-by: Taras Mankovski <taras@frontside.com>
This commit is contained in:
Taras Mankovski
2023-10-13 15:58:23 -04:00
parent bdd6f9487a
commit a1c0ffd533
2 changed files with 10 additions and 6 deletions
@@ -17,25 +17,29 @@
import { extractInitials, stringToColor } from './utils';
describe('stringToColor', () => {
it('extract color', async () => {
it('extract color', () => {
expect(stringToColor('Jenny Doe')).toEqual('#7809fa');
});
});
describe('extractInitials', () => {
it('extract initials', async () => {
it('extract initials', () => {
expect(extractInitials('Jenny Doe')).toEqual('JD');
});
it('extract unicode initials', async () => {
it('extract unicode initials', () => {
expect(extractInitials('Petr Čech')).toEqual('PČ');
});
it('extract single letter for short name', async () => {
it('extract single letter for short name', () => {
expect(extractInitials('Doe')).toEqual('D');
});
it('limit the initials to two letters', async () => {
it('limit the initials to two letters', () => {
expect(extractInitials('John Jonathan Doe')).toEqual('JD');
});
it('removes spaces from beginning or the end', () => {
expect(extractInitials(' John Jonathan Doe ')).toEqual('JD');
});
});
@@ -28,7 +28,7 @@ export function stringToColor(str: string) {
}
export function extractInitials(name: string) {
const names = name.split(' ');
const names = name.trim().split(' ');
const firstName = names[0] ?? '';
const lastName = names.length > 1 ? names[names.length - 1] : '';
return firstName && lastName