Yuki Takei 10 месяцев назад
Родитель
Сommit
476da6a02d
1 измененных файлов с 14 добавлено и 77 удалено
  1. 14 77
      packages/core/src/utils/path-utils.ts

+ 14 - 77
packages/core/src/utils/path-utils.ts

@@ -1,51 +1,25 @@
-/**
- * @private
- *
- *
- * @param {string} path
- * @returns {RegExpMatchArray}
- * @memberof pathUtils
- */
-function matchSlashes(path) {
+function matchSlashes(path: string): RegExpMatchArray | null {
   // https://regex101.com/r/Z21fEd/5
   // https://regex101.com/r/Z21fEd/5
   return path.match(/^((\/+)?(.+?))(\/+)?$/);
   return path.match(/^((\/+)?(.+?))(\/+)?$/);
 }
 }
 
 
-/**
- *
- * @param {string} path
- * @returns {boolean}
- * @memberof pathUtils
- */
-export function hasHeadingSlash(path) {
+export function hasHeadingSlash(path: string): boolean {
   if (path === '') {
   if (path === '') {
     return false;
     return false;
   }
   }
   const match = matchSlashes(path);
   const match = matchSlashes(path);
-  return (match[2] != null);
+  return (match?.[2] != null);
 }
 }
 
 
-/**
- *
- * @param {string} path
- * @returns {boolean}
- * @memberof pathUtils
- */
-export function hasTrailingSlash(path) {
+export function hasTrailingSlash(path: string): boolean {
   if (path === '') {
   if (path === '') {
     return false;
     return false;
   }
   }
   const match = matchSlashes(path);
   const match = matchSlashes(path);
-  return (match[4] != null);
+  return (match?.[4] != null);
 }
 }
 
 
-/**
- *
- * @param {string} path
- * @returns {string}
- * @memberof pathUtils
- */
-export function addHeadingSlash(path) {
+export function addHeadingSlash(path: string): string {
   if (path === '/') {
   if (path === '/') {
     return path;
     return path;
   }
   }
@@ -56,13 +30,7 @@ export function addHeadingSlash(path) {
   return path;
   return path;
 }
 }
 
 
-/**
- *
- * @param {string} path
- * @returns {string}
- * @memberof pathUtils
- */
-export function addTrailingSlash(path) {
+export function addTrailingSlash(path: string): string {
   if (path === '/') {
   if (path === '/') {
     return path;
     return path;
   }
   }
@@ -73,13 +41,7 @@ export function addTrailingSlash(path) {
   return path;
   return path;
 }
 }
 
 
-/**
- *
- * @param {string} path
- * @returns {string}
- * @memberof pathUtils
- */
-export function removeHeadingSlash(path) {
+export function removeHeadingSlash(path: string): string {
   if (path === '/') {
   if (path === '/') {
     return path;
     return path;
   }
   }
@@ -89,29 +51,19 @@ export function removeHeadingSlash(path) {
     : path;
     : path;
 }
 }
 
 
-/**
- *
- * @param {string} path
- * @returns {string}
- * @memberof pathUtils
- */
-export function removeTrailingSlash(path) {
+export function removeTrailingSlash(path: string): string {
   if (path === '/') {
   if (path === '/') {
     return path;
     return path;
   }
   }
 
 
   const match = matchSlashes(path);
   const match = matchSlashes(path);
-  return match[1];
+  return match != null ? match[1] : path;
 }
 }
 
 
 /**
 /**
  * A short-hand method to add heading slash and remove trailing slash.
  * A short-hand method to add heading slash and remove trailing slash.
- *
- * @param {string} path
- * @returns {string}
- * @memberof pathUtils
  */
  */
-export function normalizePath(path) {
+export function normalizePath(path: string): string {
   if (path === '' || path === '/') {
   if (path === '' || path === '/') {
     return '/';
     return '/';
   }
   }
@@ -124,25 +76,14 @@ export function normalizePath(path) {
 }
 }
 
 
 
 
-/**
- *
- * @param {string} path
- * @returns {string}
- * @memberof pathUtils
- */
-export function attachTitleHeader(path) {
+export function attachTitleHeader(path: string): string {
   return `# ${path}`;
   return `# ${path}`;
 }
 }
 
 
 /**
 /**
  * If the pagePath is top page path, eliminate the pageId from the url path.
  * If the pagePath is top page path, eliminate the pageId from the url path.
- *
- * @param {string} path
- * @param {string} id
- * @returns {string}
- * @memberof pathUtils
  */
  */
-export function returnPathForURL(path, id) {
+export function returnPathForURL(path: string, id: string): string {
   if (path === '/') {
   if (path === '/') {
     return path;
     return path;
   }
   }
@@ -152,11 +93,7 @@ export function returnPathForURL(path, id) {
 
 
 /**
 /**
  * Get the parent path of the specified path.
  * Get the parent path of the specified path.
- *
- * @param {string} path
- * @returns {string}
- * @memberof pathUtils
  */
  */
-export function getParentPath(path) {
+export function getParentPath(path: string): string {
   return normalizePath(path.split('/').slice(0, -1).join('/'));
   return normalizePath(path.split('/').slice(0, -1).join('/'));
 }
 }