trimSlug.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * Trim slug name with a preference for preserving whole words. Only cut up
  3. * whole words if the last remaining words are still too long.
  4. *
  5. * For example:
  6. *
  7. * javascript-project-backend -> javascript…backend
  8. * my-long-sentry-project-name -> my-long…project-name
  9. * javascriptproject-backend -> javascriptproj…ackend
  10. */
  11. export function trimSlug(slug: string, maxLength: number = 20) {
  12. // Return the original slug if it's already shorter than maxLength
  13. if (slug.length <= maxLength) {
  14. return slug;
  15. }
  16. /**
  17. * Array of words inside the slug.
  18. * E.g. "my-project-name" becomes ["my", "project", "name"]
  19. */
  20. const words: string[] = slug.split('-');
  21. // If the string is too long but not hyphenated, return an end-trimmed
  22. // string. E.g. "javascriptfrontendproject" --> "javascriptfrontendp…"
  23. if (words.length === 1) {
  24. return `${slug.slice(0, maxLength - 1)}\u2026`;
  25. }
  26. /**
  27. * Returns the length (total number of letters plus hyphens in between
  28. * words) of the current words array.
  29. */
  30. function getLength(arr: string[]): number {
  31. return arr.reduce((acc, cur) => acc + cur.length + 1, 0) - 1;
  32. }
  33. // Progressively remove words in the middle until we're below maxLength,
  34. // or when only two words are left
  35. while (getLength(words) > maxLength && words.length > 2) {
  36. words.splice(Math.floor(words.length / 2 - 0.5), 1);
  37. }
  38. // If the remaining words array satisfies the maxLength requirement,
  39. // return the trimmed result.
  40. if (getLength(words) <= maxLength) {
  41. const divider = Math.floor(words.length / 2);
  42. return `${words.slice(0, divider).join('-')}\u2026${words.slice(divider).join('-')}`;
  43. }
  44. // If the remaining 2 words are still too long, trim those words starting
  45. // from the middle.
  46. const debt = getLength(words) - maxLength;
  47. const toTrimFromLeftWord = Math.ceil(debt / 2);
  48. const leftWordLength = Math.max(words[0].length - toTrimFromLeftWord, 3);
  49. const leftWord = words[0].slice(0, leftWordLength);
  50. const rightWordLength = maxLength - leftWord.length;
  51. const rightWord = words[1].slice(-rightWordLength);
  52. return `${leftWord}\u2026${rightWord}`;
  53. }