Skip to content

Commit

Permalink
fix(): handle optional parameter followed by required parameter in ur…
Browse files Browse the repository at this point in the history
…l format.
  • Loading branch information
lkogler committed Sep 17, 2014
1 parent aa78ff9 commit efc7210
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ UrlMatcher.prototype.format = function (values) {
value = values[param];
cfg = this.params[param];

if (!isDefined(value) && (segments[i] === '/' || segments[i + 1] === '/')) continue;
if (!isDefined(value) && (segments[i] === '/' && segments[i + 1] === '/')) continue;
if (value != null) result += encodeURIComponent(cfg.type.encode(value));
result += segments[i + 1];
}
Expand All @@ -327,7 +327,7 @@ UrlMatcher.prototype.format = function (values) {
result += (search ? '&' : '?') + param + '=' + (array ? value : encodeURIComponent(value));
search = true;
}
return result;
return result.replace('//', '/');
};

UrlMatcher.prototype.$types = {};
Expand Down
11 changes: 11 additions & 0 deletions test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,17 @@ describe("urlMatcherFactory", function () {
expect(m.format({ user: 1138 })).toBe("/users/1138/photos");
});

it("should correctly format with an optional followed by a required parameter", function() {
var m = new UrlMatcher('/:user/gallery/photos/:photo', {
params: {
user: {value: null},
photo: {}
}
});
expect(m.format({ photo: 12 })).toBe("/gallery/photos/12");
expect(m.format({ user: 1138, photo: 13 })).toBe("/1138/gallery/photos/13");
});

describe("default values", function() {
it("should populate if not supplied in URL", function() {
var m = new UrlMatcher('/users/{id:int}/{test}', {
Expand Down

0 comments on commit efc7210

Please sign in to comment.