Last active
October 7, 2016 00:18
-
-
Save mikechau/214cea279c63ee8dd698b257a0b1244e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const routes = ( | |
<Router history={hashHistory}> | |
<Route path="/" component={Root}> | |
<IndexRoute component={Splash} /> | |
<Route path="/eslint" component={Root}> | |
<Route path="/check" component={Root} /> | |
</Route> | |
</Route> | |
<Route path="/test" component={Root}> | |
<IndexRoute component={Splash} /> | |
</Route> | |
</Router> | |
); | |
function walkReactComponentTree(component, prependPath = '') { | |
const paths = []; | |
const { path, children } = component.props; | |
if (Array.isArray(children)) { | |
let parentPath; | |
if (path) { | |
if (prependPath) { | |
parentPath = (`${prependPath}/${path}`); | |
} else { | |
parentPath = path; | |
} | |
paths.push(parentPath); | |
} | |
children.forEach((child) => { | |
paths.push(walkReactComponentTree(child, parentPath)); | |
}); | |
const flattenPaths = []; | |
return flattenPaths.concat.apply([], paths); | |
} | |
if (path) { | |
if (prependPath) { | |
paths.push(`${prependPath}/${path}`); | |
} else { | |
paths.push(path); | |
} | |
} | |
if (children && children.props && children.props.path) { | |
let childPath = children.props.path; | |
if (prependPath) { | |
if (path) { | |
paths.push(`${prependPath}/${path}/${childPath}`); | |
} else { | |
paths.push(`${prependPath}/${childPath}`); | |
} | |
} else { | |
paths.push(childPath); | |
} | |
} | |
return paths; | |
} | |
const tree = walkReactComponentTree(routes); | |
console.log(tree); | |
// # => ["/", "///eslint", "///eslint//check", "/test"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment