path-utils.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @private
  3. *
  4. *
  5. * @param {string} path
  6. * @returns {RegExpMatchArray}
  7. * @memberof pathUtils
  8. */
  9. function matchSlashes(path) {
  10. // https://regex101.com/r/Z21fEd/5
  11. return path.match(/^((\/+)?(.+?))(\/+)?$/);
  12. }
  13. /**
  14. *
  15. * @param {string} path
  16. * @returns {boolean}
  17. * @memberof pathUtils
  18. */
  19. export function hasHeadingSlash(path) {
  20. if (path === '') {
  21. return false;
  22. }
  23. const match = matchSlashes(path);
  24. return (match[2] != null);
  25. }
  26. /**
  27. *
  28. * @param {string} path
  29. * @returns {boolean}
  30. * @memberof pathUtils
  31. */
  32. export function hasTrailingSlash(path) {
  33. if (path === '') {
  34. return false;
  35. }
  36. const match = matchSlashes(path);
  37. return (match[4] != null);
  38. }
  39. /**
  40. *
  41. * @param {string} path
  42. * @returns {string}
  43. * @memberof pathUtils
  44. */
  45. export function addHeadingSlash(path) {
  46. if (path === '/') {
  47. return path;
  48. }
  49. if (!hasHeadingSlash(path)) {
  50. return `/${path}`;
  51. }
  52. return path;
  53. }
  54. /**
  55. *
  56. * @param {string} path
  57. * @returns {string}
  58. * @memberof pathUtils
  59. */
  60. export function addTrailingSlash(path) {
  61. if (path === '/') {
  62. return path;
  63. }
  64. if (!hasTrailingSlash(path)) {
  65. return `${path}/`;
  66. }
  67. return path;
  68. }
  69. /**
  70. *
  71. * @param {string} path
  72. * @returns {string}
  73. * @memberof pathUtils
  74. */
  75. export function removeHeadingSlash(path) {
  76. if (path === '/') {
  77. return path;
  78. }
  79. return hasHeadingSlash(path)
  80. ? path.substring(1)
  81. : path;
  82. }
  83. /**
  84. *
  85. * @param {string} path
  86. * @returns {string}
  87. * @memberof pathUtils
  88. */
  89. export function removeTrailingSlash(path) {
  90. if (path === '/') {
  91. return path;
  92. }
  93. const match = matchSlashes(path);
  94. return match[1];
  95. }
  96. /**
  97. * A short-hand method to add heading slash and remove trailing slash.
  98. *
  99. * @param {string} path
  100. * @returns {string}
  101. * @memberof pathUtils
  102. */
  103. export function normalizePath(path) {
  104. if (path === '' || path === '/') {
  105. return '/';
  106. }
  107. const match = matchSlashes(path);
  108. if (match == null) {
  109. return '/';
  110. }
  111. return `/${match[3]}`;
  112. }
  113. /**
  114. *
  115. * @param {string} path
  116. * @returns {string}
  117. * @memberof pathUtils
  118. */
  119. export function attachTitleHeader(path) {
  120. return `# ${path}`;
  121. }