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 .asRegExp() method to return regexp for route #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Route = require('route-parser');
var route = new Route('/my/fancy/route/page/:page');
route.match('/my/fancy/route/page/7') // { page: 7 }
route.reverse({page: 3}) // -> '/my/fancy/route/page/3'
route.asRegExp() // new RegExp(/^\/my\/fancy\/route\/page\/([^/\?]+))?(?=\?|$)/)
```
## What can I use in my routes?

Expand Down
11 changes: 11 additions & 0 deletions lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,16 @@ Route.prototype.reverse = function (params) {
return ReverseVisitor.visit(this.ast, params);
};

/**
* Returns a regular expression for matching the route
* @example
* var route = new Route('/route/:one/(:two)')
* route.asRegExp() // -> /\/route\/([^\\/\\?]+)\/(?:([^\\/\\?]+))?/
* @return {RegExp} The route expressed as a regular expression
*/
Route.prototype.asRegExp = function () {
var matcher = RegexpVisitor.visit(this.ast);
return matcher.re;
};

module.exports = Route;
43 changes: 43 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,47 @@ describe('Route', function () {
);
});
});

describe('asRegExp', function () {
it('returns regexp for route without params', function () {
var route = RouteParser('/foo');
var regexp = route.asRegExp();
assert.typeOf(regexp, 'regexp');
assert.match('/foo', regexp);
assert.match('/foo?', regexp);
assert.notMatch('/foo/', regexp);
});

it('returns regexp for route with simple params', function () {
var route = RouteParser('/:foo/:bar');
var regexp = route.asRegExp();
assert.typeOf(regexp, 'regexp');
assert.match('/red/green', regexp);
});

it('returns regexp for route with optional params', function () {
var route = RouteParser('/things/(option/:first)');
var regexp = route.asRegExp();
assert.typeOf(regexp, 'regexp');
assert.match('/things/', regexp);
assert.notMatch('/things/option/', regexp);
assert.match('/things/option/blue', regexp);
});

it('returns regexp for route with nested optional params', function () {
var route = RouteParser('/things/(option/:first(/second/:second))');
var regexp = route.asRegExp();
assert.typeOf(regexp, 'regexp');
assert.match('/things/', regexp);
assert.match('/things/option/blue', regexp);
assert.match('/things/option/blue/second/red', regexp);
});

it('returns regexp for route with splat', function () {
var route = RouteParser('/*a/foo/*b');
var regexp = route.asRegExp();
assert.typeOf(regexp, 'regexp');
assert.match('/zoo/woo/foo/bar/baz', regexp);
});
});
});