UNPKG

@motorcycle/router

Version:

Standard Router Driver for Motorcycle.js

44 lines 1.86 kB
var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; import { getFilteredPath, isStrictlyInScope, pathJoin, splitPath } from './helpers'; import { hold } from 'most-subject'; import { default as switchPath } from 'switch-path'; var RouterSource = (function () { function RouterSource(history$, previousRoutes) { this._history$ = history$; this._previousRoutes = previousRoutes; } RouterSource.prototype.history = function () { return this._history$; }; RouterSource.prototype.path = function (path) { var scopedNamespace = this._previousRoutes.concat(splitPath(path)); var scopedHistory$ = this._history$.filter(isStrictlyInScope(scopedNamespace)).thru(hold(1)); return new RouterSource(scopedHistory$, scopedNamespace); }; RouterSource.prototype.define = function (routes) { var previousRoutes = this._previousRoutes; var createHref = this.createHref.bind(this); return this._history$ .map(function matchRoute(location) { var filteredPath = getFilteredPath(previousRoutes, location.path); return __assign({}, switchPath(filteredPath, routes), { location: location, createHref: createHref }); }) .thru(hold(1)); }; RouterSource.prototype.createHref = function (path) { var previousRoutes = this._previousRoutes; if (previousRoutes.length === 0) return pathJoin(['/', path]); return pathJoin(['/'].concat(previousRoutes, [path])); }; return RouterSource; }()); export { RouterSource }; //# sourceMappingURL=RouterSource.js.map