Fix avatar from name to support lowercase

This commit is contained in:
ayumi-signal 2023-09-28 16:31:58 -04:00 committed by GitHub
parent e3750ef471
commit 72d1695612
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 9 deletions

View file

@ -78,7 +78,6 @@
display: flex;
justify-content: center;
text-align: center;
text-transform: uppercase;
transition: font-size 100ms ease-out;
}

View file

@ -9,7 +9,6 @@
outline: none;
padding: 0;
text-align: center;
text-transform: uppercase;
transition: font-size 30ms linear;
width: 100%;
}
@ -17,7 +16,6 @@
&__measure {
inset-inline-start: -9999;
position: fixed;
text-transform: uppercase;
top: -9999;
touch-action: none;
visibility: hidden;

View file

@ -23,9 +23,18 @@ describe('getInitials', () => {
assert.strictEqual(getInitials('Bo'), 'B');
});
it('returns lowercase initials for lowercase names', () => {
assert.strictEqual(getInitials('alice'), 'a');
assert.strictEqual(getInitials('foo bar'), 'fb');
});
it('returns initials for lowercase with uppercase names', () => {
assert.strictEqual(getInitials('foo Bar'), 'fB');
assert.strictEqual(getInitials('Foo bar'), 'Fb');
});
[
'Foo Bar',
'foo bar',
'F Bar',
'Foo B',
'FB',
@ -38,7 +47,6 @@ describe('getInitials', () => {
'Foo "Qux" Bar',
'Foo-Qux Bar',
'Foo Bar-Qux',
"Foo b'Arr",
].forEach(name => {
it(`returns 'FB' for '${name}'`, () => {
assert.strictEqual(getInitials(name), 'FB');

View file

@ -101,15 +101,14 @@ export async function avatarDataToBytes(
);
} else if (color && text) {
const { bg, fg } = getAvatarColor(color);
const textToWrite = text.toLocaleUpperCase();
setCanvasBackground(bg, context, canvas);
context.fillStyle = fg;
const font = await getFont(textToWrite);
const font = await getFont(text);
context.font = font;
context.textBaseline = 'middle';
context.textAlign = 'center';
context.fillText(textToWrite, CANVAS_SIZE / 2, CANVAS_SIZE / 2 + 30);
context.fillText(text, CANVAS_SIZE / 2, CANVAS_SIZE / 2 + 30);
} else if (color && icon) {
const iconPath = `images/avatars/avatar_${icon}.svg`;
await drawImage(iconPath, context, canvas);

View file

@ -22,7 +22,7 @@ export function getInitials(name?: string): string | undefined {
return parsedName;
}
const parts = parsedName.toUpperCase().split(' ');
const parts = parsedName.split(' ');
const partsLen = parts.length;
return partsLen === 1