Permalink
Browse files

feat(route-recognizer): Support generating a route by route config in…

… addition to name
  • Loading branch information...
davismj committed Nov 12, 2018
1 parent a50adc2 commit ba9eb61982cdcff8c8bd0f4cb5150c503a218db5
Showing with 19 additions and 14 deletions.
  1. +19 −14 src/route-recognizer.js
@@ -17,6 +17,7 @@ export class RouteRecognizer {
constructor() {
this.rootState = new State();
this.names = {};
this.routes = new Map();
}
/**
@@ -79,13 +80,13 @@ export class RouteRecognizer {
let handlers = [{ handler: route.handler, names: names }];
this.routes.set(route.handler, { segments, handlers });
if (routeName) {
let routeNames = Array.isArray(routeName) ? routeName : [routeName];
for (let i = 0; i < routeNames.length; i++) {
this.names[routeNames[i]] = {
segments: segments,
handlers: handlers
};
if (!(routeNames[i] in this.names)) {
this.names[routeNames[i]] = { segments, handlers };
}
}
}
@@ -105,16 +106,20 @@ export class RouteRecognizer {
return currentState;
}
getRoute(nameOrRoute: string | RouteHandler): any {
return typeof(nameOrRoute) === 'string' ? this.names[nameOrRoute] : this.routes.get(nameOrRoute);
}
/**
* Retrieve the handlers registered for the named route.
*
* @param name The name of the route.
* @returns The handlers.
*/
handlersFor(name: string): HandlerEntry[] {
let route = this.names[name];
handlersFor(nameOrRoute: string | RouteHandler): HandlerEntry[] {
let route = this.getRoute(nameOrRoute);
if (!route) {
throw new Error(`There is no route named ${name}`);
throw new Error(`There is no route named ${nameOrRoute}`);
}
return [...route.handlers];
@@ -126,8 +131,8 @@ export class RouteRecognizer {
* @param name The name of the route.
* @returns True if the named route is recognized.
*/
hasRoute(name: string): boolean {
return !!this.names[name];
hasRoute(nameOrRoute: string | ConfigurableRoute): boolean {
return !!this.getRoute(nameOrRoute);
}
/**
@@ -138,10 +143,10 @@ export class RouteRecognizer {
* Properties not required by the pattern will be appended to the query string.
* @returns The generated absolute path and query string.
*/
generate(name: string, params: Object): string {
let route = this.names[name];
generate(nameOrRoute: string, params: Object): string {
let route = this.getRoute(nameOrRoute);
if (!route) {
throw new Error(`There is no route named ${name}`);
throw new Error(`There is no route named ${nameOrRoute}`);
}
let handler = route.handlers[0].handler;
@@ -164,7 +169,7 @@ export class RouteRecognizer {
let segmentValue = segment.generate(routeParams, consumed);
if (segmentValue === null || segmentValue === undefined) {
if (!segment.optional) {
throw new Error(`A value is required for route parameter '${segment.name}' in route '${name}'.`);
throw new Error(`A value is required for route parameter '${segment.name}' in route '${nameOrRoute}'.`);
}
} else {
output += '/';
@@ -195,7 +200,7 @@ export class RouteRecognizer {
* `isDynanic` values for the matched route(s), or undefined if no match
* was found.
*/
recognize(path: string): RecognizedRoute[] | void {
recognize(path: string): RecognizeResults {
let states = [this.rootState];
let queryParams = {};
let isSlashDropped = false;

0 comments on commit ba9eb61

Please sign in to comment.