Skip to content

Commit

Permalink
chore(all): prepare release 1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
EisenbergEffect committed Nov 20, 2018
1 parent a31f2fb commit df53f34
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 144 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aurelia-route-recognizer",
"version": "1.2.0",
"version": "1.3.0",
"description": "A lightweight JavaScript library that matches paths against registered routes. It includes support for dynamic and star segments and nested handlers.",
"keywords": [
"aurelia",
Expand Down
46 changes: 0 additions & 46 deletions build/tasks/doc.js

This file was deleted.

1 change: 0 additions & 1 deletion build/tasks/prepare-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ gulp.task('prepare-release', function(callback){
'build',
'lint',
'bump-version',
'doc',
'changelog',
callback
);
Expand Down
31 changes: 18 additions & 13 deletions dist/amd/aurelia-route-recognizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ define(['exports', 'aurelia-path'], function (exports, _aureliaPath) {

this.rootState = new State();
this.names = {};
this.routes = new Map();
}

RouteRecognizer.prototype.add = function add(route) {
Expand Down Expand Up @@ -239,13 +240,13 @@ define(['exports', 'aurelia-path'], function (exports, _aureliaPath) {

var handlers = [{ handler: route.handler, names: names }];

this.routes.set(route.handler, { segments: segments, handlers: handlers });
if (routeName) {
var routeNames = Array.isArray(routeName) ? routeName : [routeName];
for (var _i2 = 0; _i2 < routeNames.length; _i2++) {
this.names[routeNames[_i2]] = {
segments: segments,
handlers: handlers
};
if (!(routeNames[_i2] in this.names)) {
this.names[routeNames[_i2]] = { segments: segments, handlers: handlers };
}
}
}

Expand All @@ -263,23 +264,27 @@ define(['exports', 'aurelia-path'], function (exports, _aureliaPath) {
return currentState;
};

RouteRecognizer.prototype.handlersFor = function handlersFor(name) {
var route = this.names[name];
RouteRecognizer.prototype.getRoute = function getRoute(nameOrRoute) {
return typeof nameOrRoute === 'string' ? this.names[nameOrRoute] : this.routes.get(nameOrRoute);
};

RouteRecognizer.prototype.handlersFor = function handlersFor(nameOrRoute) {
var 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 [].concat(route.handlers);
};

RouteRecognizer.prototype.hasRoute = function hasRoute(name) {
return !!this.names[name];
RouteRecognizer.prototype.hasRoute = function hasRoute(nameOrRoute) {
return !!this.getRoute(nameOrRoute);
};

RouteRecognizer.prototype.generate = function generate(name, params) {
var route = this.names[name];
RouteRecognizer.prototype.generate = function generate(nameOrRoute, params) {
var route = this.getRoute(nameOrRoute);
if (!route) {
throw new Error('There is no route named ' + name);
throw new Error('There is no route named ' + nameOrRoute);
}

var handler = route.handlers[0].handler;
Expand All @@ -302,7 +307,7 @@ define(['exports', 'aurelia-path'], function (exports, _aureliaPath) {
var 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 += '/';
Expand Down
39 changes: 29 additions & 10 deletions dist/aurelia-route-recognizer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ export declare interface CharSpec {
repeat?: boolean;
}

/**
* An object that is indexed and used for route generation, particularly for dynamic routes.
*/
/**
* An object that is indexed and used for route generation, particularly for dynamic routes.
*/
export declare interface RouteGenerator {
segments: Segment[];
handlers: HandlerEntry[];
}

// A State has a character specification and (`charSpec`) and a list of possible
// subsequent states (`nextStates`).
//
Expand Down Expand Up @@ -113,30 +124,38 @@ export declare class RouteRecognizer {
add(route: ConfigurableRoute | ConfigurableRoute[]): State;

/**
* Retrieve the handlers registered for the named route.
* Retrieve a RouteGenerator for a route by name or RouteConfig (RouteHandler).
*
* @param nameOrRoute The name of the route or RouteConfig object.
* @returns The RouteGenerator for that route.
*/
getRoute(nameOrRoute: string | RouteHandler): RouteGenerator;

/**
* Retrieve the handlers registered for the route by name or RouteConfig (RouteHandler).
*
* @param name The name of the route.
* @param nameOrRoute The name of the route or RouteConfig object.
* @returns The handlers.
*/
handlersFor(name: string): HandlerEntry[];
handlersFor(nameOrRoute: string | RouteHandler): HandlerEntry[];

/**
* Check if this RouteRecognizer recognizes a named route.
* Check if this RouteRecognizer recognizes a route by name or RouteConfig (RouteHandler).
*
* @param name The name of the route.
* @param nameOrRoute The name of the route or RouteConfig object.
* @returns True if the named route is recognized.
*/
hasRoute(name: string): boolean;
hasRoute(nameOrRoute: string | RouteHandler): boolean;

/**
* Generate a path and query string from a route name and params object.
* Generate a path and query string from a route name or RouteConfig (RouteHandler) and params object.
*
* @param name The name of the route.
* @param nameOrRoute The name of the route or RouteConfig object.
* @param params The route params to use when populating the pattern.
* 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;
generate(nameOrRoute: string | RouteHandler, params: object): string;

/**
* Match a path string against registered route patterns.
Expand All @@ -146,5 +165,5 @@ export declare 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;
}
60 changes: 40 additions & 20 deletions dist/aurelia-route-recognizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ interface CharSpec {
repeat?: boolean;
}

type Segment = StaticSegment | DynamicSegment | StarSegment | EpsilonSegment;
/**
* An object that is indexed and used for route generation, particularly for dynamic routes.
*/
interface RouteGenerator {
segments: Segment[];
handlers: HandlerEntry[];
}

/**
* Class that parses route patterns and matches path strings.
*
Expand All @@ -222,6 +231,7 @@ export class RouteRecognizer {
constructor() {
this.rootState = new State();
this.names = {};
this.routes = new Map();
}

/**
Expand Down Expand Up @@ -284,13 +294,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 };
}
}
}
Expand All @@ -311,42 +321,52 @@ export class RouteRecognizer {
}
/**
* Retrieve the handlers registered for the named route.
* Retrieve a RouteGenerator for a route by name or RouteConfig (RouteHandler).
*
* @param nameOrRoute The name of the route or RouteConfig object.
* @returns The RouteGenerator for that route.
*/
getRoute(nameOrRoute: string | RouteHandler): RouteGenerator {
return typeof(nameOrRoute) === 'string' ? this.names[nameOrRoute] : this.routes.get(nameOrRoute);
}
/**
* Retrieve the handlers registered for the route by name or RouteConfig (RouteHandler).
*
* @param name The name of the route.
* @param nameOrRoute The name of the route or RouteConfig object.
* @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];
}
/**
* Check if this RouteRecognizer recognizes a named route.
* Check if this RouteRecognizer recognizes a route by name or RouteConfig (RouteHandler).
*
* @param name The name of the route.
* @param nameOrRoute The name of the route or RouteConfig object.
* @returns True if the named route is recognized.
*/
hasRoute(name: string): boolean {
return !!this.names[name];
hasRoute(nameOrRoute: string | RouteHandler): boolean {
return !!this.getRoute(nameOrRoute);
}
/**
* Generate a path and query string from a route name and params object.
* Generate a path and query string from a route name or RouteConfig (RouteHandler) and params object.
*
* @param name The name of the route.
* @param nameOrRoute The name of the route or RouteConfig object.
* @param params The route params to use when populating the pattern.
* 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 | RouteHandler, 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;
Expand All @@ -369,7 +389,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 += '/';
Expand Down Expand Up @@ -400,7 +420,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;
Expand Down
Loading

0 comments on commit df53f34

Please sign in to comment.