Skip to content

Commit

Permalink
fix(getPages): fixed error on root path
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitri Kopriwa committed Feb 28, 2019
1 parent 32a878d commit 8989f2d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/getPages.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,36 +114,48 @@ const defaultOptions = {
* @return {object} pages - The pages of your applications
*/
export default function getPages(routesConfig, pages = {}, options = defaultOptions) {
// options
const { filters, childKey, home } = { ...defaultOptions, ...options };
// define input mode
const isRoutesMap = routesConfig instanceof Map;
[...routesConfig].forEach((routeOrMap) => {
// choose from input mode
const routeUnfiltered = isRoutesMap ? routeOrMap[1] : routeOrMap;
// retrieve page information
const { path, page: pageAliases, ...route } = omit(routeUnfiltered, filters);
// set the page content
const page = { path, ...route };
// set a list of aliases
const pageAliasList = pageAliases instanceof Array ? pageAliases : [pageAliases].filter((f) => f);
// dotted string to traverse object
let pageDepth = '';
// routes without path can be redirection or nested route config case
if (path) {
// convert the path into dotted camel case
const camelCasePath = camelizePath(path);
const camelCasePathList = camelCasePath.split('.');
camelCasePathList.forEach((onePathDepth, i) => {
// this is special case to map homepage
if (path === home.path) {
if (pageAliasList.length) {
// default to home if no page alias are set
if (!pageAliasList.length) {
getMergeSet(pages, home.page, page);
} else {
pageAliasList.forEach((depth) => {
getMergeSet(pages, depth, page);
});
} else {
getMergeSet(pages, home.page, page);
}
} else if (!pageDepth.length && i === camelCasePathList.length - 1) {
[onePathDepth, pageAliasList].filter((f) => f).forEach((depth) => getMergeSet(pages, depth, page));
[onePathDepth, ...pageAliasList].filter((f) => f).forEach((depth) => getMergeSet(pages, depth, page));
} else if (pageDepth.length && i === camelCasePathList.length - 1) {
const targetList = [camelCasePath].concat(pageAliasList).filter((f) => f);
const targetList = [camelCasePath, ...pageAliasList].filter((f) => f);
targetList.forEach((target) => getMergeSet(pages, target, page));
} else {
pageDepth = pageDepth.length === 0 ? onePathDepth : `${pageDepth}.${onePathDepth}`;
}
});
}
// this support routes configuration nesting
routeUnfiltered[childKey] && getPages(routeUnfiltered[childKey], pages, childKey); // eslint-disable-line no-unused-expressions
});
return pages;
Expand All @@ -155,13 +167,13 @@ export default function getPages(routesConfig, pages = {}, options = defaultOpti
* Traverse get merge and set a value within an object
* @param {object} object - object to use
* @param {string} path - The path to get and set
* @param {string} value - The value to merge and set
* @param {*} value - The value to merge and set
*/
function getMergeSet(object, path, value) {
if (path.includes('.')) {
const val = get(object, path) || {};
set(object, path, merge(val, value));
} else {
Object.assign(object, merge({ [path]: value }, object[path] || {}));
Object.assign(object, merge(object[path] || {}, { [path]: value }));
}
}
22 changes: 22 additions & 0 deletions src/tests/getPages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,26 @@ describe('getPages', () => {
const p = getPages(routesConfig);
expect(p.test.path).toEqual('/test');
});

it('should work', () => {
const routesConfig = [
{
path: '/auth',
role: 1,
},
{
path: '/users',
},
];
const p = getPages(routesConfig);
expect(p).toEqual({
auth: {
path: '/auth',
role: 1,
},
users: {
path: '/users',
},
});
});
});

0 comments on commit 8989f2d

Please sign in to comment.