Skip to content

Commit

Permalink
Add support for paths that are arrays of paths
Browse files Browse the repository at this point in the history
  • Loading branch information
webcarrot authored Sep 2, 2016
1 parent f667013 commit 138396d
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,39 +146,50 @@ Route.prototype.match = function (url, options) {
*/
Route.prototype.makePath = function (params, query) {
var routePath = this.config.path;
var compiler;
var err;
var url;
var strQuery;
var i;
var len;

if (Array.isArray(routePath)) {
routePath = routePath[0];
}

if (typeof routePath === 'string') {
compiler = cachedCompilers[routePath] || pathToRegexp.compile(routePath);
cachedCompilers[routePath] = compiler;

try {
url = compiler(params);
if (query) {
strQuery = this._queryLib.stringify(query);
if (strQuery) {
url += '?' + strQuery;
}
for (i = 0, len = routePath.length; i < len; i++) {
try {
return this._makePath(routePath[i], params, query);
} catch (pathErr) {
err = pathErr;
}
return url;
} catch (e) {
err = e;
}
} else {
err = new TypeError('route path must be a string:' + routePath);
try {
return this._makePath(routePath, params, query)
} catch (pathErr) {
err = pathErr;
}
}

debug('Route.makePath failed, e = ', err);
return null;
};

Route.prototype._makePath = function(routePath, params, query) {
var compiler;
var url;
var strQuery;
if (typeof routePath === "string") {
compiler = cachedCompilers[routePath] || pathToRegexp.compile(routePath);
cachedCompilers[routePath] = compiler;
url = compiler(params);
if (query) {
strQuery = this._queryLib.stringify(query);
if (strQuery) {
url += "?" + strQuery;
}
}
return url;
} else {
throw new TypeError("route path must be a string:" + routePath);
}
}

/**
* A Router class that provides route matching and route generation functionalities.
* @class Router
Expand Down

0 comments on commit 138396d

Please sign in to comment.