Skip to content

Commit 99965a1

Browse files
committed
feat: build paths with nested query parameters
Issue router5/router5#19
1 parent 4437e38 commit 99965a1

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

modules/RouteNode.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,15 @@ export default class RouteNode {
159159
}
160160

161161
buildPathFromSegments(segments, params = {}) {
162-
return segments ? segments.map(segment => segment.parser.build(params)).join('') : null
162+
let searchPart = segments
163+
.filter(s => s.parser.hasQueryParams)
164+
.map(s => s.parser.queryParams)
165+
.reduce((queryParams, params) => queryParams.concat(params))
166+
.filter(p => Object.keys(params).indexOf(p) !== -1)
167+
.map(p => p + '=' + params[p])
168+
.join('&')
169+
170+
return segments ? segments.map(segment => segment.parser.build(params, {ignoreSearch: true})).join('') + (searchPart ? '?' + searchPart : '') : null
163171
}
164172

165173
buildPath(routeName, params = {}) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
"async": "^1.3.0"
3333
},
3434
"dependencies": {
35-
"path-parser": "~0.2.4"
35+
"path-parser": "~0.3.0"
3636
}
3737
}

test/main.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,21 @@ describe('RouteNode', function () {
123123
should.not.exists(node.matchPath('/users/view/profile/1'));
124124
});
125125

126+
it('should find build a path with nested query parameters', function () {
127+
var node = new RouteNode('', '', [
128+
new RouteNode('grandParent', '/grand-parent?nickame', [
129+
new RouteNode('parent', '/parent?name', [
130+
new RouteNode('child', '/child?age')
131+
])
132+
])
133+
]);
134+
135+
node.buildPath('grandParent', {nickame: 'gran'}).should.equal('/grand-parent?nickame=gran');
136+
node.buildPath('grandParent.parent', {nickame: 'gran', name: 'maman'}).should.equal('/grand-parent/parent?nickame=gran&name=maman');
137+
node.buildPath('grandParent.parent', {nickame: 'gran'}).should.equal('/grand-parent/parent?nickame=gran');
138+
node.buildPath('grandParent.parent', {name: 'maman'}).should.equal('/grand-parent/parent?name=maman');
139+
});
140+
126141
it('should find a nested route by matching a path with a splat', function () {
127142
var node = getRoutesWithSplat();
128143
// Building paths

0 commit comments

Comments
 (0)