Skip to content

Commit f299327

Browse files
committed
feat: update to v2.0.0 of path-parser
1 parent 072e74d commit f299327

4 files changed

Lines changed: 26 additions & 11 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
22
coverage/
33
npm-debug.log
4+
yarn.lock

modules/RouteNode.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,29 +186,33 @@ export default class RouteNode {
186186
const child = nodes[i];
187187

188188
// Partially match path
189-
let match = child.parser.partialMatch(pathSegment);
189+
let match;
190190
let remainingPath;
191191

192-
if (!match && trailingSlash) {
193-
// Try with optional trailing slash
194-
match = child.parser.match(pathSegment, true);
195-
remainingPath = '';
196-
} else if (match) {
192+
if (!child.children.length) {
193+
match = child.parser.test(pathSegment, { trailingSlash });
194+
}
195+
196+
if (!match) {
197+
match = child.parser.partialTest(pathSegment);
198+
}
199+
200+
if (match) {
197201
// Remove consumed segment from path
198-
const consumedPath = child.parser.build(match, {ignoreSearch: true});
202+
let consumedPath = child.parser.build(match, {ignoreSearch: true});
203+
if (trailingSlash && !child.children.length) {
204+
consumedPath = consumedPath.replace(/\/$/, '');
205+
}
199206
remainingPath = pathSegment.replace(consumedPath, '');
200207
const search = omit(
201208
getSearch(pathSegment.replace(consumedPath, '')),
202209
child.parser.queryParams.concat(child.parser.queryParamsBr)
203210
);
204211
remainingPath = getPath(remainingPath) + (search ? `?${search}` : '');
205-
206212
if (trailingSlash && !isRoot && remainingPath === '/' && !/\/$/.test(consumedPath)) {
207213
remainingPath = '';
208214
}
209-
}
210215

211-
if (match) {
212216
segments.push(child);
213217
Object.keys(match).forEach(param => segments.params[param] = match[param]);
214218

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"yargs": "^4.8.1"
5555
},
5656
"dependencies": {
57-
"path-parser": "~1.0.4",
57+
"path-parser": "2.0.0",
5858
"search-params": "~1.3.0"
5959
}
6060
}

test/main.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,16 @@ describe('RouteNode', function () {
475475
withoutMeta(node.matchPath('/admin/?page=1')).should.eql({ name: 'app.admin.users', params: { page: '1' }});
476476
withoutMeta(node.matchPath('/admin/')).should.eql({ name: 'app.admin.users', params: {}});
477477
});
478+
479+
it('should fully match route nodes who have no children', () => {
480+
var node = new RouteNode('', '', [
481+
new RouteNode('home', '/home'),
482+
new RouteNode('section', '/:section')
483+
]);
484+
485+
withoutMeta(node.matchPath('/homeland')).should.eql({ name: 'section', params: { section: 'homeland' }});
486+
withoutMeta(node.matchPath('/hom')).should.eql({ name: 'section', params: { section: 'hom' }});
487+
});
478488
});
479489

480490

0 commit comments

Comments
 (0)