Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ UrlMatcher.prototype.exec = function (path, searchParams) {

if (nPath !== m.length - 1) throw new Error("Unbalanced capture group in route '" + this.source + "'");

for (i=0; i<nPath; i++) values[params[i]] = decodeURIComponent(m[i+1]);
for (i=0; i<nPath; i++) values[params[i]] = m[i+1];
for (/**/; i<nTotal; i++) values[params[i]] = searchParams[params[i]];

return values;
Expand Down Expand Up @@ -202,7 +202,7 @@ UrlMatcher.prototype.format = function (values) {
for (i=0; i<nPath; i++) {
value = values[params[i]];
// TODO: Maybe we should throw on null here? It's not really good style to use '' and null interchangeabley
if (value != null) result += value;
if (value != null) result += encodeURIComponent(value);
result += segments[i+1];
}
for (/**/; i<nTotal; i++) {
Expand Down
8 changes: 8 additions & 0 deletions test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ describe("UrlMatcher", function () {
.toBeNull();
});

it(".exec() treats the URL as already decoded and does not decode it further", function () {
expect(new UrlMatcher('/users/:id').exec('/users/100%25', {})).toEqual({ id: '100%25'});
});

it('.exec() throws on unbalanced capture list', function () {
var shouldThrow = {
"/url/{matchedParam:([a-z]+)}/child/{childParam}": '/url/someword/child/childParam',
Expand Down Expand Up @@ -69,6 +73,10 @@ describe("UrlMatcher", function () {
.toEqual('/users/123/details/default/444?from=1970');
});

it(".format() encodes URL parameters", function () {
expect(new UrlMatcher('/users/:id').format({ id:'100%'})).toEqual('/users/100%25');
});

it(".concat() concatenates matchers", function () {
var matcher = new UrlMatcher('/users/:id/details/{type}?from').concat('/{repeat:[0-9]+}?to');
var params = matcher.parameters();
Expand Down