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 angular-ui#1902
  • Loading branch information
JakobJingleheimer authored and ExpFront committed Jun 23, 2016
1 parent 627750d commit f7d952a
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 f7d952a

Please sign in to comment.