Skip to content

Commit 1e5bfd1

Browse files
cherry-pick 77fa11b from 0.2.17
1 parent 27a7b36 commit 1e5bfd1

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/url/urlMatcher.ts

+3
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ export class UrlMatcher {
372372
// TODO: rewrite as reduce over params with result as initial
373373
params.map((param: Param, i) => {
374374
let isPathParam = i < segments.length - 1;
375+
var isFinalPathParam = i + 2 === segments.length;
375376
let value = param.value(values[param.id]);
376377
let isDefaultValue = param.isDefaultValue(value);
377378
let squash = isDefaultValue ? param.squash : false;
@@ -396,6 +397,8 @@ export class UrlMatcher {
396397
if (param.type.raw) return encoded + segment;
397398
return encodeURIComponent(<string> encoded) + segment;
398399
})(segments[i + 1], result);
400+
401+
if (isFinalPathParam && squash === true && result.slice(-1) === '/') result = result.slice(0, -1);
399402
});
400403

401404
if (values["#"]) result += "#" + values["#"];

test/urlMatcherFactorySpec.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ describe("UrlMatcher", function () {
173173
var m = new UrlMatcher('/users/:id#:section');
174174
expect(m.format({ id: 'bob', section: 'contact-details' })).toEqual('/users/bob#contact-details');
175175
});
176+
177+
it("should trim trailing slashes when the terminal value is optional", function () {
178+
var config = { params: { id: { squash: true, value: '123' } } },
179+
m = new UrlMatcher('/users/:id', config),
180+
params = { id: '123' };
181+
182+
expect(m.format(params)).toEqual('/users');
183+
});
176184
});
177185

178186
describe(".append()", function() {
@@ -700,8 +708,8 @@ describe("urlMatcherFactory", function () {
700708
params: { id: { value: null, squash: true }, state: { value: null, squash: true } }
701709
});
702710

703-
expect(m.format()).toBe("/users/");
704-
expect(m.format({ id: 1138 })).toBe("/users/1138/");
711+
expect(m.format()).toBe("/users");
712+
expect(m.format({ id: 1138 })).toBe("/users/1138");
705713
expect(m.format({ state: "NY" })).toBe("/users/NY");
706714
expect(m.format({ id: 1138, state: "NY" })).toBe("/users/1138/NY");
707715
});

0 commit comments

Comments
 (0)