Skip to content

Commit

Permalink
fix(UrlMatcherFactory): Correct trailing slash of terminal optional p…
Browse files Browse the repository at this point in the history
…arameter

Fixes #1902
  • Loading branch information
JakobJingleheimer committed Aug 25, 2015
1 parent 9dc31c5 commit 77fa11b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ UrlMatcher.prototype.format = function (values) {

if (isPathParam) {
var nextSegment = segments[i + 1];
var isFinalPathParam = i + 1 === nPath;

if (squash === false) {
if (encoded != null) {
if (isArray(encoded)) {
Expand All @@ -357,6 +359,8 @@ UrlMatcher.prototype.format = function (values) {
} else if (isString(squash)) {
result += squash + nextSegment;
}

if (isFinalPathParam && param.squash === true && result.slice(-1) === '/') result = result.slice(0, -1);
} else {
if (encoded == null || (isDefaultValue && squash !== false)) continue;
if (!isArray(encoded)) encoded = [ encoded ];
Expand Down
12 changes: 10 additions & 2 deletions test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ describe("UrlMatcher", function () {
params = { id: 'bob', section: 'contact-details' };
expect(m.format(params)).toEqual('/users/bob#contact-details');
});

it("should trim trailing slashes when the terminal value is optional", function () {
var config = { params: { id: { squash: true, value: '123' } } },
m = new UrlMatcher('/users/:id', config),
params = { id: '123' };

expect(m.format(params)).toEqual('/users');
});
});

describe(".concat()", function() {
Expand Down Expand Up @@ -623,8 +631,8 @@ describe("urlMatcherFactory", function () {
params: { id: { value: null, squash: true }, state: { value: null, squash: true } }
});

expect(m.format()).toBe("/users/");
expect(m.format({ id: 1138 })).toBe("/users/1138/");
expect(m.format()).toBe("/users");
expect(m.format({ id: 1138 })).toBe("/users/1138");
expect(m.format({ state: "NY" })).toBe("/users/NY");
expect(m.format({ id: 1138, state: "NY" })).toBe("/users/1138/NY");
});
Expand Down

0 comments on commit 77fa11b

Please sign in to comment.