Skip to content

Commit 8034208

Browse files
committed
feat: include path-parser npm module
1 parent 692040d commit 8034208

4 files changed

Lines changed: 83 additions & 26 deletions

File tree

index.js

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ Object.defineProperty(exports, '__esModule', {
66

77
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; }; })();
88

9+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
10+
911
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
1012

13+
var _pathParser = require('path-parser');
14+
15+
var _pathParser2 = _interopRequireDefault(_pathParser);
16+
1117
var RouteNode = (function () {
1218
function RouteNode() {
1319
var name = arguments[0] === undefined ? '' : arguments[0];
@@ -18,7 +24,11 @@ var RouteNode = (function () {
1824

1925
this.name = name;
2026
this.path = path;
27+
if (path) {
28+
this.parser = new _pathParser2['default'](path);
29+
}
2130
this.children = [];
31+
2232
this.add(childRoutes);
2333
}
2434

@@ -43,19 +53,24 @@ var RouteNode = (function () {
4353
}
4454
route = new RouteNode(route.name, route.path, route.children);
4555
}
56+
// Check duplicated routes
57+
if (this.children.map(function (child) {
58+
return child.name;
59+
}).indexOf(route.name) !== -1) {
60+
throw new Error('Alias "' + route.name + '" is already defined in route node');
61+
}
62+
// Check duplicated paths
63+
if (this.children.map(function (child) {
64+
return child.path;
65+
}).indexOf(route.path) !== -1) {
66+
throw new Error('Path "' + route.path + '" is already defined in route node');
67+
}
4668

47-
// if (this.nameMap[route.name]) {
48-
// throw new Error(`Alias $route.name is already defined in route node`)
49-
// }
50-
51-
// if (this.pathMap[route.name]) {
52-
// throw new Error(`Path $route.path is already defined in route node`)
53-
// }
5469
this.children.push(route);
5570
}
5671
}, {
57-
key: 'findRoute',
58-
value: function findRoute(path) {}
72+
key: 'findRouteByPath',
73+
value: function findRouteByPath(path) {}
5974
}, {
6075
key: 'findRouteByName',
6176
value: function findRouteByName(routeName) {
@@ -90,6 +105,17 @@ var RouteNode = (function () {
90105
return segment.path;
91106
}).join('');
92107
}
108+
}, {
109+
key: 'buildPath',
110+
value: function buildPath(routeName) {
111+
var params = arguments[1] === undefined ? {} : arguments[1];
112+
113+
var segments = this.findRouteByName(routeName);
114+
115+
return segments.map(function (segment) {
116+
return segment.parser.build(params);
117+
}).join('');
118+
}
93119
}]);
94120

95121
return RouteNode;

lib/RouteNode.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
import Path from 'path-parser'
2+
13
export default class RouteNode {
24
constructor(name = '', path = '', childRoutes = []) {
3-
this.name = name
5+
this.name = name
46
this.path = path
7+
if (path) {
8+
this.parser = new Path(path)
9+
}
510
this.children = []
11+
612
this.add(childRoutes)
713
}
814

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

25-
// if (this.nameMap[route.name]) {
26-
// throw new Error(`Alias $route.name is already defined in route node`)
27-
// }
28-
29-
// if (this.pathMap[route.name]) {
30-
// throw new Error(`Path $route.path is already defined in route node`)
31-
// }
3239
this.children.push(route)
3340
}
3441

35-
findRoute(path) {
42+
findRouteByPath(path) {
3643

3744
}
3845

3946
findRouteByName(routeName) {
40-
var findSegmentByName = (name, routes) => {
41-
var filteredRoutes = routes.filter(r => r.name === name)
47+
let findSegmentByName = (name, routes) => {
48+
let filteredRoutes = routes.filter(r => r.name === name)
4249
return filteredRoutes.length ? filteredRoutes[0] : undefined
4350
}
44-
var segments = []
45-
var names = routeName.split('.');
46-
var routes = this.children
51+
let segments = []
52+
let names = routeName.split('.');
53+
let routes = this.children
4754

48-
var matched = names.every(name => {
49-
var segment = findSegmentByName(name, routes)
55+
let matched = names.every(name => {
56+
let segment = findSegmentByName(name, routes)
5057
if (segment) {
5158
routes = segment.children
5259
segments.push(segment)
@@ -59,8 +66,14 @@ export default class RouteNode {
5966
}
6067

6168
getPath(routeName) {
62-
var segments = this.findRouteByName(routeName)
69+
let segments = this.findRouteByName(routeName)
6370

6471
return segments.map(segment => segment.path).join('')
6572
}
73+
74+
buildPath(routeName, params = {}) {
75+
let segments = this.findRouteByName(routeName)
76+
77+
return segments.map(segment => segment.parser.build(params)).join('')
78+
}
6679
}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@
2626
"babel": "^5.5.8",
2727
"should": "^6.0.3",
2828
"mocha": "^2.2.5"
29+
},
30+
"dependencies": {
31+
"path-parser": "0.0.2"
2932
}
3033
}

test/main.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ describe('RouteNode', function () {
3535
}).should.throw();
3636
});
3737

38+
it('should throw an error when trying to add a route to a node with an already existing alias or path', function () {
39+
var root = new RouteNode('', '', [
40+
{name: 'home', path: '/home'}
41+
]);
42+
43+
(function () {
44+
root.add({name: 'home', path: '/profile'})
45+
}).should.throw('Alias "home" is already defined in route node');
46+
47+
(function () {
48+
root.add({name: 'profile', path: '/home'})
49+
}).should.throw('Path "/home" is already defined in route node');
50+
});
51+
3852
it('should instanciate a RouteNode object from RouteNode objects', function () {
3953
var node = new RouteNode('', '', [
4054
new RouteNode('home', '/home'),
@@ -59,5 +73,6 @@ describe('RouteNode', function () {
5973
node.getPath('users').should.equal('/users');
6074
node.getPath('users.list').should.equal('/users/list');
6175
node.getPath('users.view').should.equal('/users/view/:id');
76+
node.buildPath('users.view', {id: 1}).should.equal('/users/view/1');
6277
});
6378
});

0 commit comments

Comments
 (0)