moy-router
Version:
Give a solution for moy-dom router management.
54 lines (53 loc) • 1.29 kB
JavaScript
import {
curry,
ifElse,
split,
prop,
compose,
map,
Maybe,
tap,
always,
and,
length
} from 'moy-fp'
export default curry((current, routes) => props => {
const currentPathname = current.route.pathname.split('?').map(split('#'))[0][0]
const maybeRouteNode = compose(
map(
route => route.component({
...props,
route: {
pathname: current.route.pathname,
params: current.route.params,
query: current.route.query,
hash: current.route.hash,
state: current.route.state,
}
})
),
map(
tap(
ifElse(
currentRoute => {
const matched = currentPathname.match(currentRoute.pathnameRegExp)
current.route.params = {}
currentRoute.paramKeys.forEach((key, index) => {
current.route.params[key.name] = decodeURIComponent(matched[index + 1])
})
}
)(
always(undefined)
)(
compose(
and(!current.route.params),
length,
prop('paramKeys'),
)
)
)
),
Maybe.of,
)(routes.find(route => route.pathnameRegExp.test(currentPathname)))
return maybeRouteNode.isJust ? maybeRouteNode.join() : ''
})