Skip to content

Commit efc7210

Browse files
committed
fix(): handle optional parameter followed by required parameter in url format.
1 parent aa78ff9 commit efc7210

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/urlMatcherFactory.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ UrlMatcher.prototype.format = function (values) {
310310
value = values[param];
311311
cfg = this.params[param];
312312

313-
if (!isDefined(value) && (segments[i] === '/' || segments[i + 1] === '/')) continue;
313+
if (!isDefined(value) && (segments[i] === '/' && segments[i + 1] === '/')) continue;
314314
if (value != null) result += encodeURIComponent(cfg.type.encode(value));
315315
result += segments[i + 1];
316316
}
@@ -327,7 +327,7 @@ UrlMatcher.prototype.format = function (values) {
327327
result += (search ? '&' : '?') + param + '=' + (array ? value : encodeURIComponent(value));
328328
search = true;
329329
}
330-
return result;
330+
return result.replace('//', '/');
331331
};
332332

333333
UrlMatcher.prototype.$types = {};

test/urlMatcherFactorySpec.js

+11
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,17 @@ describe("urlMatcherFactory", function () {
320320
expect(m.format({ user: 1138 })).toBe("/users/1138/photos");
321321
});
322322

323+
it("should correctly format with an optional followed by a required parameter", function() {
324+
var m = new UrlMatcher('/:user/gallery/photos/:photo', {
325+
params: {
326+
user: {value: null},
327+
photo: {}
328+
}
329+
});
330+
expect(m.format({ photo: 12 })).toBe("/gallery/photos/12");
331+
expect(m.format({ user: 1138, photo: 13 })).toBe("/1138/gallery/photos/13");
332+
});
333+
323334
describe("default values", function() {
324335
it("should populate if not supplied in URL", function() {
325336
var m = new UrlMatcher('/users/{id:int}/{test}', {

0 commit comments

Comments
 (0)