From 4200ad111c6a14af22466b3d30c0cd26120f540e Mon Sep 17 00:00:00 2001 From: Danny Liu Date: Thu, 4 Apr 2024 19:56:23 -0700 Subject: [PATCH] handle names with trailing whitespace --- src/lib/utils/index.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index f5bd778b..636f68ee 100644 --- a/src/lib/utils/index.ts +++ b/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);