Skip to content

Commit 9fc326b

Browse files
committed
feat: add support for non strict query parameters
1 parent 5940b2c commit 9fc326b

2 files changed

Lines changed: 16 additions & 13 deletions

File tree

modules/RouteNode.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ export default class RouteNode {
131131
return matched ? segments : null
132132
}
133133

134-
getSegmentsMatchingPath(path, trailingSlash = false) {
134+
getSegmentsMatchingPath(path, options) {
135+
const { trailingSlash, strictQueryParams } = options;
135136
let matchChildren = (nodes, pathSegment, segments) => {
136137
// for (child of node.children) {
137138
for (let i in nodes) {
@@ -159,7 +160,7 @@ export default class RouteNode {
159160
Object.keys(match).forEach(param => segments.params[param] = match[param])
160161

161162
if (!remainingPath.length || // fully matched
162-
remainingPath.indexOf('?') === 0 // we only got unmatched queryParams
163+
!strictQueryParams && remainingPath.indexOf('?') === 0 // unmatched queryParams in non strict mode
163164
) {
164165
return segments
165166
}
@@ -253,7 +254,9 @@ export default class RouteNode {
253254
};
254255
}
255256

256-
matchPath(path, trailingSlash = false) {
257-
return this.buildStateFromSegments(this.getSegmentsMatchingPath(path, trailingSlash))
257+
matchPath(path, options) {
258+
const defaultOptions = { trailingSlash: false, strictQueryParams: true };
259+
options = { ...defaultOptions, ...options };
260+
return this.buildStateFromSegments(this.getSegmentsMatchingPath(path, options))
258261
}
259262
}

test/main.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ describe('RouteNode', function () {
197197
withoutMeta(node.matchPath('/grand-parent/parent/child?nickname=gran&nickname=granny&name=maman&age=3')).should.eql({name: 'grandParent.parent.child', params: {nickname: ['gran', 'granny'], name: 'maman', age: '3'}});
198198

199199
// still matching remainingPath only consist of unknown qsParams
200-
node.matchPath('/grand-parent?nickname=gran&name=papa').should.eql({
200+
node.matchPath('/grand-parent?nickname=gran&name=papa', { strictQueryParams: false }).should.eql({
201201
_meta: {
202202
grandParent: {
203203
nickname: 'query'
@@ -206,7 +206,7 @@ describe('RouteNode', function () {
206206
name: 'grandParent',
207207
params: {nickname: 'gran'}
208208
});
209-
node.matchPath('/grand-parent/parent/child?nickname=gran&names=papa-maman').should.eql({
209+
node.matchPath('/grand-parent/parent/child?nickname=gran&names=papa-maman', { strictQueryParams: false }).should.eql({
210210
_meta: {
211211
grandParent: {
212212
nickname: 'query'
@@ -269,19 +269,19 @@ describe('RouteNode', function () {
269269
it('should match paths with optional trailing slashes', function () {
270270
var rootNode = getRoutes();
271271
should.not.exists(rootNode.matchPath('/users/list/'));
272-
withoutMeta(rootNode.matchPath('/users/list', true)).should.eql({name: 'users.list', params: {}});
272+
withoutMeta(rootNode.matchPath('/users/list', { trailingSlash: true })).should.eql({name: 'users.list', params: {}});
273273
withoutMeta(rootNode.matchPath('/users/list')).should.eql({name: 'users.list', params: {}});
274-
withoutMeta(rootNode.matchPath('/users/list/', true)).should.eql({name: 'users.list', params: {}});
275-
should.not.exists(rootNode.matchPath('/users/list//', true));
274+
withoutMeta(rootNode.matchPath('/users/list/', { trailingSlash: true })).should.eql({name: 'users.list', params: {}});
275+
should.not.exists(rootNode.matchPath('/users/list//', { trailingSlash: true }));
276276

277277
var rootNode = getRoutes(true);
278278
should.not.exists(rootNode.matchPath('/users/list'));
279-
withoutMeta(rootNode.matchPath('/users/list', true)).should.eql({name: 'users.list', params: {}});
280-
withoutMeta(rootNode.matchPath('/users/list/', true)).should.eql({name: 'users.list', params: {}});
279+
withoutMeta(rootNode.matchPath('/users/list', { trailingSlash: true })).should.eql({name: 'users.list', params: {}});
280+
withoutMeta(rootNode.matchPath('/users/list/', { trailingSlash: true })).should.eql({name: 'users.list', params: {}});
281281
withoutMeta(rootNode.matchPath('/users/list/')).should.eql({name: 'users.list', params: {}});
282282
withoutMeta(rootNode.matchPath('/')).should.eql({name: 'default', params: {}});
283-
withoutMeta(rootNode.matchPath('', true)).should.eql({name: 'default', params: {}});
284-
should.not.exists(rootNode.matchPath('/users/list//', true));
283+
withoutMeta(rootNode.matchPath('', { trailingSlash: true })).should.eql({name: 'default', params: {}});
284+
should.not.exists(rootNode.matchPath('/users/list//', { trailingSlash: true }));
285285
});
286286
});
287287

0 commit comments

Comments
 (0)