Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion package/src/utils/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatMsToMinSec, getUrlWithoutParams } from '../utils';
import { formatMsToMinSec, getInitialsFromName, getUrlWithoutParams } from '../utils';

describe('getUrlWithoutParams', () => {
const testUrlMap: Record<string, string> = {
Expand Down Expand Up @@ -37,3 +37,19 @@ describe('formatMsToMinSec', () => {
expect(formatMsToMinSec(7_200_000)).toBe('2h');
});
});

describe('getInitialsFromName', () => {
it('should return the uppercased first letter of each word', () => {
expect(getInitialsFromName('john doe')).toBe('JD');
expect(getInitialsFromName(' Jane Mary Doe ')).toBe('JM');
expect(getInitialsFromName('Jane Mary Doe', 3)).toBe('JMD');
expect(getInitialsFromName('Jane', 1)).toBe('J');
expect(getInitialsFromName(' ')).toBe('');
});

it('should not split characters outside the BMP into lone surrogates', () => {
expect(getInitialsFromName('😀 Smiley')).toBe('😀S');
expect(getInitialsFromName('𝒜lice 𐐨bob')).toBe('𝒜𐐀');
expect(getInitialsFromName('🚀', 1)).toBe('🚀');
});
});
4 changes: 3 additions & 1 deletion package/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,12 @@ export const getInitialsFromName = (name: string, numberOfInitials: number = 2)
const trimmed = name.trim();

if (!trimmed) return '';
// Take the first code point of each word, not the first UTF-16 code unit, so names that
// start with an emoji or another non-BMP character do not render a lone surrogate.
return trimmed
.split(/\s+/)
.slice(0, numberOfInitials)
.map((n) => n.charAt(0).toUpperCase())
.map((n) => Array.from(n)[0].toUpperCase())
.join('');
};

Expand Down