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.

path.ts 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. export function collapsePath(path: string, limit = 30): string {
  21. if (typeof path !== 'string') {
  22. return '';
  23. }
  24. const tokens = path.split('/');
  25. if (tokens.length <= 2) {
  26. return path;
  27. }
  28. const head = tokens[0];
  29. const tail = tokens[tokens.length - 1];
  30. const middle = tokens.slice(1, -1);
  31. let cut = false;
  32. while (middle.join().length > limit && middle.length > 0) {
  33. middle.shift();
  34. cut = true;
  35. }
  36. const body = [head, ...(cut ? ['...'] : []), ...middle, tail];
  37. return body.join('/');
  38. }
  39. /**
  40. * Return a collapsed path without a file name
  41. * @example
  42. * // returns 'src/.../js/components/navigator/app/models/'
  43. * collapsedDirFromPath('src/main/js/components/navigator/app/models/state.js')
  44. */
  45. export function collapsedDirFromPath(path: string | null): string | null {
  46. const limit = 30;
  47. if (typeof path === 'string') {
  48. const tokens = path.split('/').slice(0, -1);
  49. if (tokens.length > 2) {
  50. const head = tokens[0];
  51. const tail = tokens[tokens.length - 1];
  52. const middle = tokens.slice(1, -1);
  53. let cut = false;
  54. while (middle.join().length > limit && middle.length > 0) {
  55. middle.shift();
  56. cut = true;
  57. }
  58. const body = [head, ...(cut ? ['...'] : []), ...middle, tail];
  59. return body.join('/') + '/';
  60. }
  61. return tokens.join('/') + '/';
  62. }
  63. return null;
  64. }
  65. /**
  66. * Return a file name for a given file path
  67. * * @example
  68. * // returns 'state.js'
  69. * collapsedDirFromPath('src/main/js/components/navigator/app/models/state.js')
  70. */
  71. export function fileFromPath(path: string | null): string | null {
  72. if (typeof path === 'string') {
  73. const tokens = path.split('/');
  74. return tokens[tokens.length - 1];
  75. }
  76. return null;
  77. }
  78. export function splitPath(path: string) {
  79. const tokens = path.split('/');
  80. return {
  81. head: tokens.slice(0, -1).join('/'),
  82. tail: tokens[tokens.length - 1]
  83. };
  84. }
  85. export function cutLongWords(str: string, limit = 30) {
  86. return str
  87. .split(' ')
  88. .map(word => (word.length > limit ? word.substr(0, limit) + '...' : word))
  89. .join(' ');
  90. }
  91. export function limitComponentName(str: string, limit = 30): string {
  92. if (typeof str === 'string') {
  93. return str.length > limit ? str.substr(0, limit) + '...' : str;
  94. }
  95. return '';
  96. }