소스 검색

handle names with trailing whitespace

Danny Liu 1 년 전
부모
커밋
4200ad111c
1개의 변경된 파일2개의 추가작업 그리고 3개의 파일을 삭제
  1. 2 3
      src/lib/utils/index.ts

+ 2 - 3
src/lib/utils/index.ts

@@ -109,9 +109,8 @@ export const generateInitialsImage = (name) => {
 	ctx.font = '40px Helvetica';
 	ctx.textAlign = 'center';
 	ctx.textBaseline = 'middle';
-	const firstNameInitial = name[0];
-	const lastNameInitial = name.lastIndexOf(' ') > -1 ? name[name.lastIndexOf(' ') + 1] : '';
-	const initials = `${firstNameInitial}${lastNameInitial}`.toUpperCase();
+
+	const initials = name.trim().length > 0 ? name[0] + (name.trim().split(' ').length > 1 ? name[name.lastIndexOf(' ') + 1] : '') : '';
 
 	ctx.fillText(initials.toUpperCase(), canvas.width / 2, canvas.height / 2);