|
|
|
@ -1,5 +1,6 @@ |
|
|
|
|
// @flow
|
|
|
|
|
|
|
|
|
|
import GraphemeSplitter from 'grapheme-splitter'; |
|
|
|
|
import _ from 'lodash'; |
|
|
|
|
|
|
|
|
|
const AVATAR_COLORS = [ |
|
|
|
@ -13,6 +14,8 @@ const AVATAR_COLORS = [ |
|
|
|
|
'#2AA076', |
|
|
|
|
'#00A8B3' |
|
|
|
|
]; |
|
|
|
|
const wordSplitRegex = (/\s+|\.+|_+|;+|-+|,+|\|+|\/+|\\+/); |
|
|
|
|
const splitter = new GraphemeSplitter(); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Generates the background color of an initials based avatar. |
|
|
|
@ -40,6 +43,20 @@ export function getAvatarColor(initials: ?string, customAvatarBackgrounds: Array |
|
|
|
|
return colorsBase[colorIndex]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns the first grapheme from a word, uppercased. |
|
|
|
|
* |
|
|
|
|
* @param {string} word - The string to get grapheme from. |
|
|
|
|
* @returns {string} |
|
|
|
|
*/ |
|
|
|
|
function getFirstGraphemeUpper(word) { |
|
|
|
|
if (!word?.length) { |
|
|
|
|
return ''; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return splitter.splitGraphemes(word)[0].toUpperCase(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Generates initials for a simple string. |
|
|
|
|
* |
|
|
|
@ -49,12 +66,7 @@ export function getAvatarColor(initials: ?string, customAvatarBackgrounds: Array |
|
|
|
|
export function getInitials(s: ?string) { |
|
|
|
|
// We don't want to use the domain part of an email address, if it is one
|
|
|
|
|
const initialsBasis = _.split(s, '@')[0]; |
|
|
|
|
const words = _.words(initialsBasis); |
|
|
|
|
let initials = ''; |
|
|
|
|
|
|
|
|
|
for (const w of words) { |
|
|
|
|
(initials.length < 2) && (initials += String.fromCodePoint(w.codePointAt(0)).toUpperCase()); |
|
|
|
|
} |
|
|
|
|
const [ firstWord, secondWord ] = initialsBasis.split(wordSplitRegex); |
|
|
|
|
|
|
|
|
|
return initials; |
|
|
|
|
return getFirstGraphemeUpper(firstWord) + getFirstGraphemeUpper(secondWord); |
|
|
|
|
} |
|
|
|
|