2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2018 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
export function getInitials(name?: string): string | undefined {
|
2018-09-27 00:23:17 +00:00
|
|
|
if (!name) {
|
2020-09-14 21:56:35 +00:00
|
|
|
return undefined;
|
2018-09-27 00:23:17 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 20:30:49 +00:00
|
|
|
const parsedName = name
|
|
|
|
// remove all chars that are not letters or separators
|
|
|
|
.replace(/[^\p{L}\p{Z}]+/gu, '')
|
|
|
|
// replace all chars that are separators with a single ASCII space
|
|
|
|
.replace(/\p{Z}+/gu, ' ')
|
|
|
|
.trim();
|
|
|
|
|
|
|
|
if (!parsedName) {
|
2020-09-14 21:56:35 +00:00
|
|
|
return undefined;
|
2018-09-27 00:23:17 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 20:30:49 +00:00
|
|
|
// check if chars in the parsed string are initials
|
|
|
|
if (parsedName.length === 2 && parsedName === parsedName.toUpperCase()) {
|
|
|
|
return parsedName;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parts = parsedName.toUpperCase().split(' ');
|
|
|
|
const partsLen = parts.length;
|
|
|
|
|
|
|
|
return partsLen === 1
|
|
|
|
? parts[0].charAt(0)
|
|
|
|
: parts[0].charAt(0) + parts[partsLen - 1].charAt(0);
|
2018-09-27 00:23:17 +00:00
|
|
|
}
|