signal-desktop/ts/util/getInitials.ts

22 lines
497 B
TypeScript
Raw Normal View History

const BAD_CHARACTERS = /[^A-Za-z\s]+/g;
const WHITESPACE = /\s+/g;
function removeNonInitials(name: string) {
return name.replace(BAD_CHARACTERS, '').replace(WHITESPACE, ' ');
}
2019-01-14 21:49:58 +00:00
export function getInitials(name?: string): string | undefined {
if (!name) {
2019-01-14 21:49:58 +00:00
return;
}
const cleaned = removeNonInitials(name);
const parts = cleaned.split(' ');
const initials = parts.map(part => part.trim()[0]);
if (!initials.length) {
2019-01-14 21:49:58 +00:00
return;
}
return initials.slice(0, 2).join('');
}