Просмотр исходного кода

improve path-utils.normalizePath

Yuki Takei 7 лет назад
Родитель
Сommit
bbbaf2dd08
2 измененных файлов с 9 добавлено и 3 удалено
  1. 4 3
      src/lib/util/path-utils.js
  2. 5 0
      src/test/util/path-utils.test.js

+ 4 - 3
src/lib/util/path-utils.js

@@ -19,8 +19,8 @@ function encodePagePath(path) {
 }
 
 function matchSlashes(path) {
-  // https://regex101.com/r/Z21fEd/3
-  return path.match(/^((\/)?(.+?))(\/)?$$/);
+  // https://regex101.com/r/Z21fEd/5
+  return path.match(/^((\/+)?(.+?))(\/+)?$/);
 }
 
 function hasHeadingSlash(path) {
@@ -65,7 +65,8 @@ function removeTrailingSlash(path) {
 }
 
 function normalizePath(path) {
-  return this.addHeadingSlash(this.removeTrailingSlash(path));
+  const match = matchSlashes(path);
+  return `/${match[3]}`;
 }
 
 module.exports = {

+ 5 - 0
src/test/util/path-utils.test.js

@@ -18,5 +18,10 @@ describe('page-utils', () => {
       expect(pathUtils.normalizePath('/hoge/fuga/')).to.equal('/hoge/fuga');
       done();
     });
+
+    it('should remove unnecessary slashes', done => {
+      expect(pathUtils.normalizePath('//hoge/fuga//')).to.equal('/hoge/fuga');
+      done();
+    });
   });
 });