diff --git a/src/getPages.js b/src/getPages.js index f09d4b9..aa0a156 100644 --- a/src/getPages.js +++ b/src/getPages.js @@ -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; @@ -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 })); } } diff --git a/src/tests/getPages.test.js b/src/tests/getPages.test.js index d1a4f49..d5a2c33 100644 --- a/src/tests/getPages.test.js +++ b/src/tests/getPages.test.js @@ -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', + }, + }); + }); });