UNPKG

react-router-to-array-of-object

Version:

Convert your react router component to an array of objet {name: 'route name', path: 'route path'}

1 lines 1.21 kB
'use strict';Object.defineProperty(exports,"__esModule",{value:true});exports.default=routesToArray;function extractChildRoutes(route,prefix){var paths=[];var childRoutes=route.props&&route.props.children?route.props.children:route.childRoutes;if(childRoutes){if(Array.isArray(childRoutes)){childRoutes.forEach(function(r){paths=paths.concat(extractRoute(r,prefix));});}else{paths=paths.concat(extractRoute(childRoutes,prefix));}}return paths;}function extractRoute(route,prefix){var path=route.props&&route.props.path?route.props.path:route.path;var name=route.props&&route.props.name?route.props.name:route.name;var paths=[];if(!path){if(Array.isArray(route)){route.forEach(function(r){paths=paths.concat(extractRoute(r,prefix));});return paths;}else{return extractChildRoutes(route,prefix);}}var currentPath=''+(prefix||'')+path;if(currentPath&&name){var p=''+(currentPath.startsWith('/')?'':'/')+currentPath;paths.push({path:p,name:name});paths=paths.concat(extractChildRoutes(route,currentPath+'/'));}return paths;}function routesToArray(route){var paths=[];if(Array.isArray(route)){route.forEach(function(r){paths=paths.concat(extractRoute(r));});}else{paths=paths.concat(extractRoute(route));}return paths;}