Skip to content

Commit

Permalink
fix: build path correctly when 'trailingSlash' is set and with query …
Browse files Browse the repository at this point in the history
…params
  • Loading branch information
troch committed Aug 4, 2017
1 parent 2a49f9f commit f7a18b0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
16 changes: 9 additions & 7 deletions modules/RouteNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,15 @@ export default class RouteNode {
return segment.absolute ? segmentPath : path + segmentPath;
}, '');

return path + (searchPart ? '?' + searchPart : '');
let finalPath = path;

if (options.trailingSlash === true) {
finalPath = /\/$/.test(path) ? path : `${path}/`;
} else if (options.trailingSlash === false) {
finalPath = /\/$/.test(path) ? path.slice(0, -1) : path;
}

return finalPath + (searchPart ? '?' + searchPart : '');
}

getMetaFromSegments(segments) {
Expand Down Expand Up @@ -354,12 +362,6 @@ export default class RouteNode {
const options = { ...defaultOptions, ...opts };
const path = this.buildPathFromSegments(this.getSegmentsByName(routeName), params, options);

if (options.trailingSlash === true) {
return /\/$/.test(path) ? path : `${path}/`;
} else if (options.trailingSlash === false) {
return /\/$/.test(path) ? path.slice(0, -1) : path;
}

return path;
}

Expand Down
11 changes: 11 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,17 @@ describe('RouteNode', function () {
withoutMeta(match2).should.eql({ name: 'home', params: { s: '3' }});
});
});

it('should trim trailing slashes when building paths', () => {
const node = new RouteNode('', '', [
new RouteNode('a', '/a', [
new RouteNode('b', '/?c')
])
]);

node.buildPath('a.b', {}, { trailingSlash: false }).should.eql('/a');
node.buildPath('a.b', { c: 1 }, { trailingSlash: false }).should.eql('/a?c=1');
});
});


Expand Down

0 comments on commit f7a18b0

Please sign in to comment.