Skip to content

Commit

Permalink
Merge pull request #30 from yahoo/arrayRoutes
Browse files Browse the repository at this point in the history
[resolves #28] Allow array as route definition and document as standard
  • Loading branch information
mridgway committed Mar 16, 2016
2 parents a176e0f + b516339 commit 21a01a6
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 240 deletions.
7 changes: 2 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
sudo: false
language: node_js
matrix:
allow_failures:
- node_js: "0.13"
node_js:
- "iojs"
- "0.13"
- 5
- 4
- "0.12"
- "0.10"
after_success:
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ For more detailed examples, please check out [example applications](https://gith
```javascript
var Router = require('routr');

var router = new Router({
view_user: {
var router = new Router([
{
name: 'view_user',
path: '/user/:id',
method: 'get',
foo: {
bar: 'baz'
}
},
view_user_post: {
{
name: 'view_user_post',
path: '/user/:id/post/:post',
method: 'get'
}
});
]);

// match route
var route = router.getRoute('/user/garfield');
Expand Down
6 changes: 5 additions & 1 deletion docs/routr.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

Creates a new routr plugin instance with the following parameters:

* `routes` (optional): Route table, which is a name to router config map.
* `routes` (optional): Ordered list of routes used for matching.
** `route.name`: Name of the route (used for path making)
** `route.path`: The matching pattern of the route. Follows rules of [path-to-regexp](https://github
.com/pillarjs/path-to-regexp)
** `route.method=GET`: The method that the path should match to.

## Instance Methods

Expand Down
25 changes: 21 additions & 4 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,32 @@ Route.prototype.makePath = function (params) {
function Router(routes) {
var self = this;
self._routes = {};
self._routeOrder = [];
debug('new Router, routes = ', routes);
if (routes) {
Object.keys(routes).forEach(function createRoute(name) {

if (!Array.isArray(routes)) {
// Support handling route config object as an ordered map (legacy)
self._routeOrder = Object.keys(routes);
self._routeOrder.forEach(function createRoute(name) {
self._routes[name] = new Route(name, routes[name]);
});
} else if (routes) {
routes.forEach(function createRouteFromArrayValue(route) {
if (!route.name) {
throw new Error('Undefined route name for route ' + route.path);
}
// Route name already exists
if (self._routes[route.name]) {
throw new Error('Duplicate route with name ' + route.name);
}
self._routeOrder.push(route.name);
self._routes[route.name] = new Route(route.name, route);
});
}

if (process.env.NODE_ENV !== 'production') {
if ('function' === typeof Object.freeze) {
Object.keys(self._routes).forEach(function freezeRoute(name) {
self._routeOrder.forEach(function freezeRoute(name) {
var route = self._routes[name];
Object.freeze(route.config);
Object.freeze(route.keys);
Expand All @@ -199,7 +216,7 @@ function Router(routes) {
* @return {Object|null} The matched route info if path/method matches to a route; null otherwise.
*/
Router.prototype.getRoute = function (url, options) {
var keys = Object.keys(this._routes);
var keys = this._routeOrder;
var route;
var match;

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"scripts": {
"cover": "node node_modules/istanbul/lib/cli.js cover --dir artifacts -- ./node_modules/mocha/bin/_mocha tests/unit/ --recursive --reporter spec",
"lint": "./node_modules/.bin/jshint lib tests",
"test": "./node_modules/.bin/mocha tests/unit/ --recursive --reporter spec"
"test": "./node_modules/.bin/mocha tests/unit/ --recursive --compilers js:babel-register --require babel-polyfill --reporter spec"
},
"author": "Lingyan Zhu <lingyan@yahoo-inc.com",
"licenses": [
Expand All @@ -24,6 +24,9 @@
"path-to-regexp": "^1.1.1"
},
"devDependencies": {
"babel-polyfill": "^6.7.2",
"babel-preset-es2015": "^6.6.0",
"babel-register": "^6.7.2",
"chai": "^2.0.0",
"coveralls": "^2.11.1",
"istanbul": "^0.3.2",
Expand Down
4 changes: 4 additions & 0 deletions tests/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["es2015"],
"plugins": []
}

0 comments on commit 21a01a6

Please sign in to comment.