Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for paths that are arrays of paths #37

Merged
merged 5 commits into from
Sep 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 26 additions & 0 deletions tests/unit/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ var routesObject = {
method: 'GET',
page: 'arrayPathNameCollision'
},
array_path_with_different_props: {
path: [
'/array/path/with/different/props/foo/:foo',
'/array/path/with/different/props/bar/:bar'
]
},
invalid_path: {
path: 123
},
Expand All @@ -82,6 +88,8 @@ var routesArray = Object.keys(routesObject).map(function (routeName) {
});
});
var encodingConsistencyPath = '/path/with/some/json_value/%7B%22keyword%22%3A%22foo%22%7D';
var arrayPathWithDifferentPropsFoo = '/array/path/with/different/props/foo/foo';
var arrayPathWithDifferentPropsBar = '/array/path/with/different/props/bar/bar';

describe('Router', function () {
[routesObject, routesArray].forEach(function (routes, key) {
Expand Down Expand Up @@ -292,6 +300,12 @@ describe('Router', function () {
var route = router.getRoute(encodingConsistencyPath);
expect(route.params.json).to.equal('{"keyword":"foo"}');
});
it('route with array path with different props', function () {
var routeFoo = router.getRoute(arrayPathWithDifferentPropsFoo);
expect(routeFoo.params.foo).to.equal('foo');
var routeBar = router.getRoute(arrayPathWithDifferentPropsBar);
expect(routeBar.params.bar).to.equal('bar');
});
it('should handle a hash fragment with a question-mark', function () {
var route = router.getRoute('/finance/news/test.html#?', {method: 'get'});
expect(route.name).to.equal('article');
Expand Down Expand Up @@ -409,6 +423,18 @@ describe('Router', function () {
});
expect(path).to.equal(encodingConsistencyPath);
});
it('array path with different props', function () {
var pathFoo = router.makePath('array_path_with_different_props', {
foo: 'foo'
});
expect(pathFoo).to.equal(arrayPathWithDifferentPropsFoo);
var pathBar = router.makePath('array_path_with_different_props', {
bar: 'bar'
});
expect(pathBar).to.equal(arrayPathWithDifferentPropsBar);
var pathInvalid = router.makePath('array_path_with_different_props', {});
expect(pathInvalid).to.equal(null);
});
});

it('should throw if route name is not defined', function () {
Expand Down