From a472b301389fbe84d1c1fa9f24852b492a569d11 Mon Sep 17 00:00:00 2001 From: Nate Abele Date: Thu, 17 Apr 2014 08:59:04 -0400 Subject: [PATCH] feat(UrlMatcher): default values & type decoding for query params --- src/urlMatcherFactory.js | 7 ++++++- test/urlMatcherFactorySpec.js | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/urlMatcherFactory.js b/src/urlMatcherFactory.js index 0a7c6aa3f..c8bc59014 100644 --- a/src/urlMatcherFactory.js +++ b/src/urlMatcherFactory.js @@ -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, @@ -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; }; diff --git a/test/urlMatcherFactorySpec.js b/test/urlMatcherFactorySpec.js index 85d3209a6..5844a76ad 100644 --- a/test/urlMatcherFactorySpec.js +++ b/test/urlMatcherFactorySpec.js @@ -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); + }); }); });