Added trim, test and removed unnecessary async
Signed-off-by: Taras Mankovski <taras@frontside.com>
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user