Skip to content

Commit 4547126

Browse files
committed
feat: include this node in matching if it has a parser
1 parent 964127f commit 4547126

3 files changed

Lines changed: 27 additions & 15 deletions

File tree

index.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var RouteNode = (function () {
2424

2525
this.name = name;
2626
this.path = path;
27-
this.parser = path ? new _pathParser2['default'](path) : {};
27+
this.parser = path ? new _pathParser2['default'](path) : null;
2828
this.children = [];
2929

3030
this.add(childRoutes);
@@ -96,11 +96,11 @@ var RouteNode = (function () {
9696
return matched ? segments : false;
9797
}
9898
}, {
99-
key: 'getSegmentsByPath',
100-
value: function getSegmentsByPath(path) {
101-
var matchChildren = function matchChildren(node, pathSegment, segments) {
99+
key: 'getSegmentsMatchingPath',
100+
value: function getSegmentsMatchingPath(path) {
101+
var matchChildren = function matchChildren(nodes, pathSegment, segments) {
102102
var _loop = function (i) {
103-
var child = node.children[i];
103+
var child = nodes[i];
104104
// Partially match path
105105
var match = child.parser.partialMatch(pathSegment);
106106
if (match) {
@@ -124,24 +124,25 @@ var RouteNode = (function () {
124124
}
125125
// Else: remaining path and children
126126
return {
127-
v: matchChildren(child, remainingPath, segments)
127+
v: matchChildren(child.children, remainingPath, segments)
128128
};
129129
}
130130
};
131131

132132
// for (child of node.children) {
133-
for (var i in node.children) {
133+
for (var i in nodes) {
134134
var _ret = _loop(i);
135135

136136
if (typeof _ret === 'object') return _ret.v;
137137
}
138138
return false;
139139
};
140140

141+
var startingNodes = this.parser ? [this] : this.children;
141142
var segments = [];
142143
segments.params = {};
143144

144-
return matchChildren(this, path, segments);
145+
return matchChildren(startingNodes, path, segments);
145146
}
146147
}, {
147148
key: 'getPath',
@@ -166,7 +167,7 @@ var RouteNode = (function () {
166167
}, {
167168
key: 'matchPath',
168169
value: function matchPath(path) {
169-
var segments = this.getSegmentsByPath(path);
170+
var segments = this.getSegmentsMatchingPath(path);
170171

171172
if (!segments) return false;
172173

lib/RouteNode.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default class RouteNode {
44
constructor(name = '', path = '', childRoutes = []) {
55
this.name = name
66
this.path = path
7-
this.parser = path ? new Path(path) : {}
7+
this.parser = path ? new Path(path) : null
88
this.children = []
99

1010
this.add(childRoutes)
@@ -62,10 +62,10 @@ export default class RouteNode {
6262
}
6363

6464
getSegmentsMatchingPath(path) {
65-
let matchChildren = (node, pathSegment, segments) => {
65+
let matchChildren = (nodes, pathSegment, segments) => {
6666
// for (child of node.children) {
67-
for (let i in node.children) {
68-
let child = node.children[i]
67+
for (let i in nodes) {
68+
let child = nodes[i]
6969
// Partially match path
7070
let match = child.parser.partialMatch(pathSegment)
7171
if (match) {
@@ -82,16 +82,17 @@ export default class RouteNode {
8282
return false
8383
}
8484
// Else: remaining path and children
85-
return matchChildren(child, remainingPath, segments);
85+
return matchChildren(child.children, remainingPath, segments);
8686
}
8787
}
8888
return false;
8989
}
9090

91+
let startingNodes = this.parser ? [this] : this.children
9192
let segments = []
9293
segments.params = {}
9394

94-
return matchChildren(this, path, segments)
95+
return matchChildren(startingNodes, path, segments)
9596
}
9697

9798
getPath(routeName) {

test/main.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ describe('RouteNode', function () {
117117
node.matchPath('/users/profile/1').should.eql({name: 'users.splat', params: {id: 'profile/1'}});
118118
node.matchPath('/users/view/profile/1').should.be.false;
119119
});
120+
121+
it('should work on a tree without a root node', function () {
122+
var usersNode = new RouteNode('users', '/users', [
123+
new RouteNode('list', '/list'),
124+
new RouteNode('view', '/view/:id')
125+
]);
126+
127+
usersNode.matchPath('/users/view/1').should.eql({name: 'users.view', params: {id: '1'}});
128+
usersNode.matchPath('/users/list').should.eql({name: 'users.list', params: {}});
129+
})
120130
});
121131

122132

0 commit comments

Comments
 (0)