You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.js 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // @flow
  2. import _ from 'lodash';
  3. const AVATAR_COLORS = [
  4. '232, 105, 156',
  5. '255, 198, 115',
  6. '128, 128, 255',
  7. '105, 232, 194',
  8. '234, 255, 128'
  9. ];
  10. const AVATAR_OPACITY = 0.4;
  11. /**
  12. * Generates the background color of an initials based avatar.
  13. *
  14. * @param {string?} initials - The initials of the avatar.
  15. * @param {Array<strig>} customAvatarBackgrounds - Custom avatar background values.
  16. * @returns {string}
  17. */
  18. export function getAvatarColor(initials: ?string, customAvatarBackgrounds: Array<string>) {
  19. const hasCustomAvatarBackgronds = customAvatarBackgrounds && customAvatarBackgrounds.length;
  20. const colorsBase = hasCustomAvatarBackgronds ? customAvatarBackgrounds : AVATAR_COLORS;
  21. let colorIndex = 0;
  22. if (initials) {
  23. let nameHash = 0;
  24. for (const s of initials) {
  25. nameHash += s.codePointAt(0);
  26. }
  27. colorIndex = nameHash % colorsBase.length;
  28. }
  29. return hasCustomAvatarBackgronds ? colorsBase[colorIndex] : `rgba(${colorsBase[colorIndex]}, ${AVATAR_OPACITY})`;
  30. }
  31. /**
  32. * Generates initials for a simple string.
  33. *
  34. * @param {string?} s - The string to generate initials for.
  35. * @returns {string?}
  36. */
  37. export function getInitials(s: ?string) {
  38. // We don't want to use the domain part of an email address, if it is one
  39. const initialsBasis = _.split(s, '@')[0];
  40. const words = _.words(initialsBasis);
  41. let initials = '';
  42. for (const w of words) {
  43. (initials.length < 2) && (initials += w.substr(0, 1).toUpperCase());
  44. }
  45. return initials;
  46. }