Răsfoiți Sursa

create convertToNewAffiliationPath

itizawa 5 ani în urmă
părinte
comite
f267e30f04

+ 4 - 5
src/client/js/components/ComparePathsTable.jsx

@@ -2,17 +2,17 @@ import React from 'react';
 import PropTypes from 'prop-types';
 
 import { withTranslation } from 'react-i18next';
-import escapeStringRegexp from 'escape-string-regexp';
 import { withUnstatedContainers } from './UnstatedUtils';
 
 import PageContainer from '../services/PageContainer';
+import { convertToNewAffiliationPath } from '../../../lib/util/path-utils';
 
 function ComparePathsTable(props) {
   const { subordinatedPages, pageContainer } = props;
   const { path } = pageContainer.state;
 
   // Dummy
-  const newPagePathPrefix = 'huga';
+  const newPagePath = 'huga';
 
   return (
     <table className="table table-bordered">
@@ -39,11 +39,10 @@ function ComparePathsTable(props) {
           <td>
             <ul className="list-unstyled">
               {subordinatedPages.map((subordinatedPage) => {
-                const pathRegExp = new RegExp(`^${escapeStringRegexp(path)}`, 'i');
-                const newPagePath = subordinatedPage.path.replace(pathRegExp, newPagePathPrefix);
+                const convertedPath = convertToNewAffiliationPath(path, newPagePath, subordinatedPage.path);
                 return (
                   <li key={subordinatedPage._id}>
-                    /{newPagePath}
+                    {convertedPath}
                   </li>
                 );
               })}

+ 16 - 0
src/lib/util/path-utils.js

@@ -1,3 +1,5 @@
+const escapeStringRegexp = require('escape-string-regexp');
+
 /**
  * Whether path is the top page
  * @param {string} path
@@ -47,9 +49,23 @@ const userPageRoot = (user) => {
   return `/user/${user.username}`;
 };
 
+/**
+ * return user path
+ * @param {string} parentPath
+ * @param {string} childPath
+ * @param {string} newPath
+ *
+ * @return {string}
+ */
+const convertToNewAffiliationPath = (oldPath, newPath, childPath) => {
+  const pathRegExp = new RegExp(`^${escapeStringRegexp(oldPath)}`, 'i');
+  return `/${childPath.replace(pathRegExp, newPath)}`;
+};
+
 module.exports = {
   isTopPage,
   isTrashPage,
   isUserPage,
   userPageRoot,
+  convertToNewAffiliationPath,
 };