Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

functions.js 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. * @returns {string}
  16. */
  17. export function getAvatarColor(initials: ?string) {
  18. let colorIndex = 0;
  19. if (initials) {
  20. let nameHash = 0;
  21. for (const s of initials) {
  22. nameHash += s.codePointAt(0);
  23. }
  24. colorIndex = nameHash % AVATAR_COLORS.length;
  25. }
  26. return `rgba(${AVATAR_COLORS[colorIndex]}, ${AVATAR_OPACITY})`;
  27. }
  28. /**
  29. * Generates initials for a simple string.
  30. *
  31. * @param {string?} s - The string to generate initials for.
  32. * @returns {string?}
  33. */
  34. export function getInitials(s: ?string) {
  35. // We don't want to use the domain part of an email address, if it is one
  36. const initialsBasis = _.split(s, '@')[0];
  37. const words = _.words(initialsBasis);
  38. let initials = '';
  39. for (const w of words) {
  40. (initials.length < 2) && (initials += w.substr(0, 1).toUpperCase());
  41. }
  42. return initials;
  43. }