diff --git a/src/urlMatcherFactory.js b/src/urlMatcherFactory.js index 93c5f4232..7e6274dd3 100644 --- a/src/urlMatcherFactory.js +++ b/src/urlMatcherFactory.js @@ -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]; } @@ -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 = {}; diff --git a/test/urlMatcherFactorySpec.js b/test/urlMatcherFactorySpec.js index 0b253a123..0994bc7c0 100644 --- a/test/urlMatcherFactorySpec.js +++ b/test/urlMatcherFactorySpec.js @@ -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}', {