Skip to content

Commit

Permalink
Merge pull request #23 from jasisk/remove-reverend
Browse files Browse the repository at this point in the history
remove reverend in favor of path-to-regexp.compile
  • Loading branch information
lingyan committed May 12, 2015
2 parents 9090dd3 + 8a831b0 commit 97d14ff
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
29 changes: 22 additions & 7 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

var debug = require('debug')('Routr:router');
var pathToRegexp = require('path-to-regexp');
var reverend = require('reverend');
var METHODS = {
GET: 'get'
};
Expand Down Expand Up @@ -134,12 +133,28 @@ Route.prototype.match = function (url, options) {
* @for Route
*/
Route.prototype.makePath = function (params) {
try {
return reverend(this.config.path, params);
} catch (e) {
debug('Route.makePath failed, e = ', e);
return null;
var route = this.config.path,
compiler,
err;

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

if (typeof route === 'string') {
compiler = pathToRegexp.compile(route);

try {
return compiler(params);
} catch (e) {
err = e;
}
} else {
err = new TypeError('route must be a String path');
}

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

/**
Expand Down Expand Up @@ -238,4 +253,4 @@ Router.prototype.makePath = function (name, params) {
return (name && this._routes[name] && this._routes[name].makePath(params)) || null;
};

module.exports = Router;
module.exports = Router;
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
],
"dependencies": {
"debug": "^2.0.0",
"path-to-regexp": "^1.0.0",
"reverend": "^0.3.0"
"path-to-regexp": "^1.1.1"
},
"devDependencies": {
"chai": "^2.0.0",
Expand Down
34 changes: 29 additions & 5 deletions tests/unit/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ var routes = {
path: '/case_insensitive',
method: 'GET',
page: 'viewCaseInsensitive'
},
array_path: {
path: ['/array_path']
},
invalid_path: {
path: 123
}
};
var router;
Expand All @@ -55,7 +61,7 @@ describe('Router', function () {

describe('#constructor', function () {
it('should init correctly', function () {
expect(Object.keys(router._routes).length).to.equal(7);
expect(Object.keys(router._routes).length).to.equal(9);

expect(router._routes.article.name).to.equal('article');
expect(router._routes.article.config.path).to.equal('/:site/:category?/:subcategory?/:alias');
Expand Down Expand Up @@ -98,16 +104,26 @@ describe('Router', function () {
expect(router._routes.case_insensitive.config.page).to.equal('viewCaseInsensitive');
expect(router._routes.case_insensitive.keys.length).to.equal(0);
expect(router._routes.case_insensitive.regexp).to.be.a('RegExp');

expect(router._routes.array_path.name).to.equal('array_path');
expect(router._routes.array_path.config.path[0]).to.equal('/array_path');
expect(router._routes.array_path.keys.length).to.equal(0);
expect(router._routes.array_path.regexp).to.be.a('RegExp');

expect(router._routes.invalid_path.name).to.equal('invalid_path');
expect(router._routes.invalid_path.config.path).to.equal(123);
expect(router._routes.invalid_path.keys.length).to.equal(0);
expect(router._routes.invalid_path.regexp).to.be.a('RegExp');
});
it('should not freeze in production env', function () {
var origEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';
var notFrozen = new Router(routes);

expect(Object.keys(notFrozen._routes).length).to.equal(7);
expect(Object.keys(notFrozen._routes).length).to.equal(9);
notFrozen._routes.foo = null;
expect(notFrozen._routes.foo).to.equal(null);
expect(Object.keys(notFrozen._routes).length).to.equal(8);
expect(Object.keys(notFrozen._routes).length).to.equal(10);

var homeRoute = notFrozen._routes.home;
expect(homeRoute.name).to.equal('home');
Expand All @@ -120,7 +136,7 @@ describe('Router', function () {
process.env.NODE_ENV = 'development';
var frozen = new Router(routes);
var homeRoute = frozen._routes.home;
expect(Object.keys(frozen._routes).length).to.equal(7);
expect(Object.keys(frozen._routes).length).to.equal(9);
expect(homeRoute.name).to.equal('home');
expect(homeRoute.config.path).to.equal('/');
expect(homeRoute.config.method).to.equal('get');
Expand All @@ -145,7 +161,7 @@ describe('Router', function () {
expect(function () {
homeRoute.config.regexp = null;
}).to.throw(TypeError);
expect(Object.keys(frozen._routes).length).to.equal(7);
expect(Object.keys(frozen._routes).length).to.equal(9);
expect(frozen._routes.foo).to.equal(undefined);
expect(homeRoute.keys.length).to.equal(0);
expect(homeRoute.name).to.equal('home');
Expand Down Expand Up @@ -289,6 +305,14 @@ describe('Router', function () {
});
expect(path).to.equal(null);
});
it('array route', function () {
var path = router.makePath('array_path', {});
expect(path).to.equal('/array_path');
});
it('invalid route', function () {
var path = router.makePath('invalid_path', {});
expect(path).to.equal(null);
});
});
});

Expand Down

0 comments on commit 97d14ff

Please sign in to comment.