import { h } from 'hyperapp'; export function router(routes) { return function (app) { return props => { return app(enhance(props)); function enhance(props) { props.state.router = {}; props.actions.router = { set: (state, actions, data) => { window.onpopstate = () => actions.set({}); return { router: data }; }, go: (state, actions, path) => { if (location.pathname + location.search !== path) { history.pushState({}, '', path); actions.set({ path }); } } }; props.view = (state, actions) => { const m = match(location.pathname, routes); const v = routes[m.index][1]; return v(state, actions); } return props; } } } } // just copied from hyperapp/router function match(pathname, routes) { var match var index var params = {} for (var i = 0; i < routes.length && !match; i++) { var route = routes[i][0] var keys = [] pathname.replace( RegExp( route === "*" ? ".*" : "^" + route.replace(/\//g, "\\/").replace(/:([\w]+)/g, function (_, key) { keys.push(key) return "([-\\.%\\w\\(\\)]+)" }) + "/?$", "g" ), function () { for (var j = 1; j < arguments.length - 2;) { var value = arguments[j++] try { value = decodeURI(value) } catch (_) { } params[keys.shift()] = value } match = route index = i } ) } return { match: match, index: index, params: params } }