@motorcycle/router
Version:
Standard Router Driver for Motorcycle.js
46 lines • 1.99 kB
JavaScript
;
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
var helpers_1 = require("./helpers");
var most_subject_1 = require("most-subject");
var switch_path_1 = require("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(helpers_1.splitPath(path));
var scopedHistory$ = this._history$.filter(helpers_1.isStrictlyInScope(scopedNamespace)).thru(most_subject_1.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 = helpers_1.getFilteredPath(previousRoutes, location.path);
return __assign({}, switch_path_1.default(filteredPath, routes), { location: location, createHref: createHref });
})
.thru(most_subject_1.hold(1));
};
RouterSource.prototype.createHref = function (path) {
var previousRoutes = this._previousRoutes;
if (previousRoutes.length === 0)
return helpers_1.pathJoin(['/', path]);
return helpers_1.pathJoin(['/'].concat(previousRoutes, [path]));
};
return RouterSource;
}());
exports.RouterSource = RouterSource;
//# sourceMappingURL=RouterSource.js.map