2021-11-17 08:49:08 +00:00
|
|
|
import GraphemeSplitter from 'grapheme-splitter';
|
2019-06-26 14:08:23 +00:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
|
|
const AVATAR_COLORS = [
|
2021-10-08 12:38:28 +00:00
|
|
|
'#6A50D3',
|
|
|
|
'#FF9B42',
|
|
|
|
'#DF486F',
|
|
|
|
'#73348C',
|
|
|
|
'#B23683',
|
|
|
|
'#F96E57',
|
|
|
|
'#4380E2',
|
|
|
|
'#2AA076',
|
|
|
|
'#00A8B3'
|
2019-06-26 14:08:23 +00:00
|
|
|
];
|
2021-11-26 08:14:20 +00:00
|
|
|
const wordSplitRegex = (/\s+|\.+|_+|;+|-+|,+|\|+|\/+|\\+|"+|'+|\(+|\)+|#+|&+/);
|
2021-11-17 08:49:08 +00:00
|
|
|
const splitter = new GraphemeSplitter();
|
2019-06-26 14:08:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the background color of an initials based avatar.
|
|
|
|
*
|
|
|
|
* @param {string?} initials - The initials of the avatar.
|
2022-07-14 07:10:08 +00:00
|
|
|
* @param {Array<string>} customAvatarBackgrounds - Custom avatar background values.
|
2019-06-26 14:08:23 +00:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
export function getAvatarColor(initials: string | undefined, customAvatarBackgrounds: Array<string>) {
|
|
|
|
const hasCustomAvatarBackgronds = customAvatarBackgrounds?.length;
|
2021-07-20 11:37:22 +00:00
|
|
|
const colorsBase = hasCustomAvatarBackgronds ? customAvatarBackgrounds : AVATAR_COLORS;
|
|
|
|
|
2019-06-26 14:08:23 +00:00
|
|
|
let colorIndex = 0;
|
|
|
|
|
|
|
|
if (initials) {
|
|
|
|
let nameHash = 0;
|
|
|
|
|
|
|
|
for (const s of initials) {
|
2022-08-08 08:12:22 +00:00
|
|
|
nameHash += Number(s.codePointAt(0));
|
2019-06-26 14:08:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 11:37:22 +00:00
|
|
|
colorIndex = nameHash % colorsBase.length;
|
2019-06-26 14:08:23 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 12:38:28 +00:00
|
|
|
return colorsBase[colorIndex];
|
2019-06-26 14:08:23 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 08:49:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the first grapheme from a word, uppercased.
|
|
|
|
*
|
|
|
|
* @param {string} word - The string to get grapheme from.
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2022-08-08 08:12:22 +00:00
|
|
|
function getFirstGraphemeUpper(word: string) {
|
2021-11-17 08:49:08 +00:00
|
|
|
if (!word?.length) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return splitter.splitGraphemes(word)[0].toUpperCase();
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:08:23 +00:00
|
|
|
/**
|
|
|
|
* Generates initials for a simple string.
|
|
|
|
*
|
|
|
|
* @param {string?} s - The string to generate initials for.
|
|
|
|
* @returns {string?}
|
|
|
|
*/
|
2022-08-08 08:12:22 +00:00
|
|
|
export function getInitials(s?: string) {
|
2019-06-26 14:08:23 +00:00
|
|
|
// We don't want to use the domain part of an email address, if it is one
|
|
|
|
const initialsBasis = _.split(s, '@')[0];
|
2021-11-26 08:14:20 +00:00
|
|
|
const [ firstWord, secondWord ] = initialsBasis.split(wordSplitRegex).filter(Boolean);
|
2019-06-26 14:08:23 +00:00
|
|
|
|
2022-04-11 15:57:26 +00:00
|
|
|
return getFirstGraphemeUpper(firstWord) + getFirstGraphemeUpper(secondWord);
|
2019-06-26 14:08:23 +00:00
|
|
|
}
|
2021-11-30 21:03:50 +00:00
|
|
|
|
|
|
|
/**
|
2021-12-17 00:16:24 +00:00
|
|
|
* Checks if the passed URL should be loaded with CORS.
|
2021-11-30 21:03:50 +00:00
|
|
|
*
|
|
|
|
* @param {string} url - The URL.
|
2021-12-17 00:16:24 +00:00
|
|
|
* @param {Array<string>} corsURLs - The URL pattern that matches a URL that needs to be handled with CORS.
|
2021-11-30 21:03:50 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-09-30 14:51:16 +00:00
|
|
|
export function isCORSAvatarURL(url: string, corsURLs: Array<string> = []): boolean {
|
2021-12-17 00:16:24 +00:00
|
|
|
return corsURLs.some(pattern => url.startsWith(pattern));
|
2021-11-30 21:03:50 +00:00
|
|
|
}
|