UNPKG

@yagni-js/yagni-router

Version:
47 lines (38 loc) 1.06 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var yagni = require('@yagni-js/yagni'); function url(path, handler) { return {path: path, handler: handler, match: yagni.match(path)}; } const getHash = yagni.pickPath(['target', 'location', 'hash']); function matcher(routes) { return function (hash) { return routes.reduce( function (acc, route) { const match = route.match(hash); return match ? acc.concat({match: match, handler: route.handler}) : acc; }, [] ); }; } const callHandler = yagni.call(yagni.pick('handler'), yagni.pick('match')); function hashRouter(routes) { return yagni.pipe([ getHash, yagni.ifElse( yagni.or(yagni.isNil, yagni.isEmpty), yagni.always('#'), yagni.identity ), yagni.slice(1), // strip # from hash matcher(routes), yagni.ifElse( yagni.isEmpty, yagni.identity, yagni.pipe([yagni.first, callHandler]) ) ]); } exports.hashRouter = hashRouter; exports.url = url;