Răsfoiți Sursa

refactor Path.normalizePath -> path-utils.normalizePath

Yuki Takei 7 ani în urmă
părinte
comite
3c55fc41a2

+ 29 - 6
src/lib/util/path-utils.js

@@ -18,16 +18,32 @@ function encodePagePath(path) {
   return paths.join('/');
 }
 
-function matchTrailingSlash(path) {
-  // https://regex101.com/r/Z21fEd/1
-  return path.match(/(.+?)(\/)?$/);
+function matchSlashes(path) {
+  // https://regex101.com/r/Z21fEd/3
+  return path.match(/^((\/)?(.+?))(\/)?$$/);
 }
 
-function hasTrailingSlash(path) {
-  const match = matchTrailingSlash(path);
+function hasHeadingSlash(path) {
+  const match = matchSlashes(path);
   return (match[2] != null);
 }
 
+function hasTrailingSlash(path) {
+  const match = matchSlashes(path);
+  return (match[4] != null);
+}
+
+function addHeadingSlash(path) {
+  if (path === '/') {
+    return path;
+  }
+
+  if (!hasHeadingSlash(path)) {
+    return `/${path}`;
+  }
+  return path;
+}
+
 function addTrailingSlash(path) {
   if (path === '/') {
     return path;
@@ -44,14 +60,21 @@ function removeTrailingSlash(path) {
     return path;
   }
 
-  const match = matchTrailingSlash(path);
+  const match = matchSlashes(path);
   return match[1];
 }
 
+function normalizePath(path) {
+  return this.addHeadingSlash(this.removeTrailingSlash(path));
+}
+
 module.exports = {
   encodePagePath,
   encodePagesPath,
+  hasHeadingSlash,
   hasTrailingSlash,
+  addHeadingSlash,
   addTrailingSlash,
   removeTrailingSlash,
+  normalizePath,
 };

+ 0 - 10
src/server/models/page.js

@@ -489,16 +489,6 @@ module.exports = function(crowi) {
     return grantLabels;
   };
 
-  pageSchema.statics.normalizePath = function(path) {
-    if (!path.match(/^\//)) {
-      path = '/' + path;
-    }
-
-    path = path.replace(/\/\s+?/g, '/').replace(/\s+\//g, '/');
-
-    return path;
-  };
-
   pageSchema.statics.getUserPagePath = function(user) {
     return '/user/' + user.username;
   };

+ 2 - 2
src/server/routes/page.js

@@ -960,7 +960,7 @@ module.exports = function(crowi, app) {
   api.rename = async function(req, res) {
     const pageId = req.body.page_id;
     const previousRevision = req.body.revision_id || null;
-    const newPagePath = Page.normalizePath(req.body.new_path);
+    const newPagePath = pathUtils.normalizePath(req.body.new_path);
     const options = {
       createRedirectPage: req.body.create_redirect || 0,
       moveUnderTrees: req.body.move_trees || 0,
@@ -1024,7 +1024,7 @@ module.exports = function(crowi, app) {
    */
   api.duplicate = async function(req, res) {
     const pageId = req.body.page_id;
-    const newPagePath = Page.normalizePath(req.body.new_path);
+    const newPagePath = pathUtils.normalizePath(req.body.new_path);
 
     const page = await Page.findByIdAndViewer(pageId, req.user);
 

+ 0 - 14
src/test/models/page.test.js

@@ -267,20 +267,6 @@ describe('Page', () => {
     });
   });
 
-  describe('Normalize path', () => {
-    context('Normalize', () => {
-      it('should start with slash', done => {
-        expect(Page.normalizePath('hoge/fuga')).to.equal('/hoge/fuga');
-        done();
-      });
-
-      it('should trim spaces of slash', done => {
-        expect(Page.normalizePath('/ hoge / fuga')).to.equal('/hoge/fuga');
-        done();
-      });
-    });
-  });
-
   describe('.findPage', () => {
     context('findByIdAndViewer', () => {
       it('should find page (public)', async() => {

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

@@ -0,0 +1,22 @@
+const chai = require('chai')
+  , expect = chai.expect
+  , sinonChai = require('sinon-chai')
+  ;
+chai.use(sinonChai);
+
+const pathUtils = require('@commons/util/path-utils');
+
+describe('page-utils', () => {
+
+  describe('.normalizePath', () => {
+    it('should add heading slash', done => {
+      expect(pathUtils.normalizePath('hoge/fuga')).to.equal('/hoge/fuga');
+      done();
+    });
+
+    it('should remove trailing slash', done => {
+      expect(pathUtils.normalizePath('/hoge/fuga/')).to.equal('/hoge/fuga');
+      done();
+    });
+  });
+});