Skip to content

Commit a472b30

Browse files
committed
feat(UrlMatcher): default values & type decoding for query params
1 parent 5b72430 commit a472b30

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/urlMatcherFactory.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ UrlMatcher.prototype.toString = function () {
203203
UrlMatcher.prototype.exec = function (path, searchParams) {
204204
var m = this.regexp.exec(path);
205205
if (!m) return null;
206+
searchParams = searchParams || {};
206207

207208
var params = this.parameters(), nTotal = params.length,
208209
nPath = this.segments.length - 1,
@@ -215,7 +216,11 @@ UrlMatcher.prototype.exec = function (path, searchParams) {
215216
cfg = this.params[param];
216217
values[param] = cfg.type.decode(isDefined(m[i + 1]) ? m[i + 1] : cfg.value);
217218
}
218-
for (/**/; i < nTotal; i++) values[params[i]] = searchParams[params[i]];
219+
for (/**/; i < nTotal; i++) {
220+
param = params[i];
221+
cfg = this.params[param];
222+
values[param] = cfg.type.decode(isDefined(searchParams[param]) ? searchParams[param] : cfg.value);
223+
}
219224

220225
return values;
221226
};

test/urlMatcherFactorySpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,14 @@ describe("urlMatcherFactory", function () {
317317
});
318318
expect(m.exec("/foo")).toEqual({ foo: "bar" });
319319
});
320+
321+
it("should populate default values for query params", function() {
322+
var defaults = { order: "name", limit: 25, page: 1 };
323+
var m = new UrlMatcher('/foo?order&limit&page', {
324+
params: defaults
325+
});
326+
expect(m.exec("/foo")).toEqual(defaults);
327+
});
320328
});
321329
});
322330

0 commit comments

Comments
 (0)