Skip to content

Commit

Permalink
feat: include path-parser npm module
Browse files Browse the repository at this point in the history
  • Loading branch information
troch committed Jun 26, 2015
1 parent 692040d commit 8034208
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 26 deletions.
44 changes: 35 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ Object.defineProperty(exports, '__esModule', {

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }

var _pathParser = require('path-parser');

var _pathParser2 = _interopRequireDefault(_pathParser);

var RouteNode = (function () {
function RouteNode() {
var name = arguments[0] === undefined ? '' : arguments[0];
Expand All @@ -18,7 +24,11 @@ var RouteNode = (function () {

this.name = name;
this.path = path;
if (path) {
this.parser = new _pathParser2['default'](path);
}
this.children = [];

this.add(childRoutes);
}

Expand All @@ -43,19 +53,24 @@ var RouteNode = (function () {
}
route = new RouteNode(route.name, route.path, route.children);
}
// Check duplicated routes
if (this.children.map(function (child) {
return child.name;
}).indexOf(route.name) !== -1) {
throw new Error('Alias "' + route.name + '" is already defined in route node');
}
// Check duplicated paths
if (this.children.map(function (child) {
return child.path;
}).indexOf(route.path) !== -1) {
throw new Error('Path "' + route.path + '" is already defined in route node');
}

// if (this.nameMap[route.name]) {
// throw new Error(`Alias $route.name is already defined in route node`)
// }

// if (this.pathMap[route.name]) {
// throw new Error(`Path $route.path is already defined in route node`)
// }
this.children.push(route);
}
}, {
key: 'findRoute',
value: function findRoute(path) {}
key: 'findRouteByPath',
value: function findRouteByPath(path) {}
}, {
key: 'findRouteByName',
value: function findRouteByName(routeName) {
Expand Down Expand Up @@ -90,6 +105,17 @@ var RouteNode = (function () {
return segment.path;
}).join('');
}
}, {
key: 'buildPath',
value: function buildPath(routeName) {
var params = arguments[1] === undefined ? {} : arguments[1];

var segments = this.findRouteByName(routeName);

return segments.map(function (segment) {
return segment.parser.build(params);
}).join('');
}
}]);

return RouteNode;
Expand Down
47 changes: 30 additions & 17 deletions lib/RouteNode.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import Path from 'path-parser'

export default class RouteNode {
constructor(name = '', path = '', childRoutes = []) {
this.name = name
this.name = name
this.path = path
if (path) {
this.parser = new Path(path)
}
this.children = []

this.add(childRoutes)
}

Expand All @@ -21,32 +27,33 @@ export default class RouteNode {
}
route = new RouteNode(route.name, route.path, route.children)
}
// Check duplicated routes
if (this.children.map(child => child.name).indexOf(route.name) !== -1) {
throw new Error(`Alias "${route.name}" is already defined in route node`)
}
// Check duplicated paths
if (this.children.map(child => child.path).indexOf(route.path) !== -1) {
throw new Error(`Path "${route.path}" is already defined in route node`)
}

// if (this.nameMap[route.name]) {
// throw new Error(`Alias $route.name is already defined in route node`)
// }

// if (this.pathMap[route.name]) {
// throw new Error(`Path $route.path is already defined in route node`)
// }
this.children.push(route)
}

findRoute(path) {
findRouteByPath(path) {

}

findRouteByName(routeName) {
var findSegmentByName = (name, routes) => {
var filteredRoutes = routes.filter(r => r.name === name)
let findSegmentByName = (name, routes) => {
let filteredRoutes = routes.filter(r => r.name === name)
return filteredRoutes.length ? filteredRoutes[0] : undefined
}
var segments = []
var names = routeName.split('.');
var routes = this.children
let segments = []
let names = routeName.split('.');
let routes = this.children

var matched = names.every(name => {
var segment = findSegmentByName(name, routes)
let matched = names.every(name => {
let segment = findSegmentByName(name, routes)
if (segment) {
routes = segment.children
segments.push(segment)
Expand All @@ -59,8 +66,14 @@ export default class RouteNode {
}

getPath(routeName) {
var segments = this.findRouteByName(routeName)
let segments = this.findRouteByName(routeName)

return segments.map(segment => segment.path).join('')
}

buildPath(routeName, params = {}) {
let segments = this.findRouteByName(routeName)

return segments.map(segment => segment.parser.build(params)).join('')
}
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
"babel": "^5.5.8",
"should": "^6.0.3",
"mocha": "^2.2.5"
},
"dependencies": {
"path-parser": "0.0.2"
}
}
15 changes: 15 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ describe('RouteNode', function () {
}).should.throw();
});

it('should throw an error when trying to add a route to a node with an already existing alias or path', function () {
var root = new RouteNode('', '', [
{name: 'home', path: '/home'}
]);

(function () {
root.add({name: 'home', path: '/profile'})
}).should.throw('Alias "home" is already defined in route node');

(function () {
root.add({name: 'profile', path: '/home'})
}).should.throw('Path "/home" is already defined in route node');
});

it('should instanciate a RouteNode object from RouteNode objects', function () {
var node = new RouteNode('', '', [
new RouteNode('home', '/home'),
Expand All @@ -59,5 +73,6 @@ describe('RouteNode', function () {
node.getPath('users').should.equal('/users');
node.getPath('users.list').should.equal('/users/list');
node.getPath('users.view').should.equal('/users/view/:id');
node.buildPath('users.view', {id: 1}).should.equal('/users/view/1');
});
});

0 comments on commit 8034208

Please sign in to comment.