Created
December 4, 2015 18:27
-
-
Save download13/af727f8fdb071b8e1a4d 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
import element from 'virtual-element'; // Using deku | |
// import React from 'react'; // Switch to this if you are using React | |
import pathToRegexp from 'path-to-regexp'; | |
export function renderRoutes(path, routes) { | |
return Object.keys(routes).map(routePath => { | |
let paramsInfo = []; | |
let re = pathToRegexp(routePath, paramsInfo); | |
let paramNames = paramsInfo.map(p => p.name); | |
let match = re.exec(path); | |
if(match) { | |
let Component = routes[routePath]; | |
let params = pairsToObj(zip(paramNames, match.slice(1))); | |
return <Component params={params} /> | |
} else { | |
return <noscript/>; | |
} | |
}); | |
} | |
function zip(a1, a2) { | |
return a1.map((v, i) => [v, a2[i]]); | |
} | |
function pairsToObj(arr) { | |
let o = {}; | |
arr.forEach(pair => { | |
o[pair[0]] = pair[1] | |
}); | |
return o; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Linked to redux-simple-router's issue: reactjs/react-router-redux#65 (comment)