Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 167: Replace multiple slashes with single slash. #175

Merged
merged 2 commits into from
Jun 24, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions client/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ Router.prototype.path = function(pathDef, fields, queryParams) {
pathDef = this._routesMap[pathDef].path;
}

// remove trailing slash(es)
// but keep the root slash if it's the only one
pathDef = pathDef.match(/^\/{1}$/) ? pathDef : pathDef.replace(/\/+$/, "");

fields = fields || {};
var regExp = /(:[\w\(\)\\\+\*\.\?]+)+/g;
var path = pathDef.replace(regExp, function(key) {
Expand All @@ -121,6 +117,12 @@ Router.prototype.path = function(pathDef, fields, queryParams) {
return fields[key] || "";
});

path = path.replace(/\/\/+/g, "/"); // Replace multiple slashes with single slash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, this only look for two '//`. Shall we write a one to deal with any amount of slashes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It handles multiples slashes. I added test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great.


// remove trailing slash
// but keep the root slash if it's the only one
path = path.match(/^\/{1}$/) ? path: path.replace(/\/$/, "");

var strQueryParams = this._qs.stringify(queryParams || {});
if(strQueryParams) {
path += "?" + strQueryParams;
Expand Down
10 changes: 6 additions & 4 deletions server/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ Router.prototype.path = function(pathDef, fields, queryParams) {
pathDef = this._routesMap[pathDef].path;
}

// remove trailing slash(es)
// but kepp the root slash if it's the only one
pathDef = pathDef.match(/^\/{1}$/) ? pathDef : pathDef.replace(/\/+$/, "");

fields = fields || {};
var regExp = /(:[\w\(\)\\\+\*\.\?]+)+/g;
var path = pathDef.replace(regExp, function(key) {
Expand All @@ -46,6 +42,12 @@ Router.prototype.path = function(pathDef, fields, queryParams) {
return fields[key] || "";
});

path = path.replace(/\/\/+/g, "/"); // Replace multiple slashes with single slash

// remove trailing slash
// but keep the root slash if it's the only one
path = path.match(/^\/{1}$/) ? path: path.replace(/\/$/, "");

var strQueryParams = Qs.stringify(queryParams || {});
if(strQueryParams) {
path += "?" + strQueryParams;
Expand Down
4 changes: 2 additions & 2 deletions test/common/router.path.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Tinytest.add('Common - Router - path - missing fields', function (test) {
var fields = {
blogId: "1001",
};
var expectedPath = "/blog/1001/some/";
var expectedPath = "/blog/1001/some";

var path = FlowRouter.path(pathDef, fields);
test.equal(path, expectedPath);
Expand Down Expand Up @@ -85,7 +85,7 @@ Tinytest.add('Common - Router - path - optional last param missing', function (t
var fields = {
blogId: "1001"
};
var expectedPath = "/blog/1001/some/";
var expectedPath = "/blog/1001/some";

var path = FlowRouter.path(pathDef, fields);
test.equal(path, expectedPath);
Expand Down