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.4KB

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