Skip to content

Commit

Permalink
feat(UrlMatcher): default values & type decoding for query params
Browse files Browse the repository at this point in the history
  • Loading branch information
nateabele committed Apr 17, 2014
1 parent 5b72430 commit a472b30
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ UrlMatcher.prototype.toString = function () {
UrlMatcher.prototype.exec = function (path, searchParams) {
var m = this.regexp.exec(path);
if (!m) return null;
searchParams = searchParams || {};

var params = this.parameters(), nTotal = params.length,
nPath = this.segments.length - 1,
Expand All @@ -215,7 +216,11 @@ UrlMatcher.prototype.exec = function (path, searchParams) {
cfg = this.params[param];
values[param] = cfg.type.decode(isDefined(m[i + 1]) ? m[i + 1] : cfg.value);
}
for (/**/; i < nTotal; i++) values[params[i]] = searchParams[params[i]];
for (/**/; i < nTotal; i++) {
param = params[i];
cfg = this.params[param];
values[param] = cfg.type.decode(isDefined(searchParams[param]) ? searchParams[param] : cfg.value);
}

return values;
};
Expand Down
8 changes: 8 additions & 0 deletions test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,14 @@ describe("urlMatcherFactory", function () {
});
expect(m.exec("/foo")).toEqual({ foo: "bar" });
});

it("should populate default values for query params", function() {
var defaults = { order: "name", limit: 25, page: 1 };
var m = new UrlMatcher('/foo?order&limit&page', {
params: defaults
});
expect(m.exec("/foo")).toEqual(defaults);
});
});
});

Expand Down

0 comments on commit a472b30

Please sign in to comment.