From 6a371f9b70e37a82eb324122879e4473c3f6d526 Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Tue, 6 Oct 2015 16:59:56 +0100 Subject: [PATCH] feat(UrlMatcher): Add param only type names --- src/urlMatcherFactory.js | 3 ++- test/urlMatcherFactorySpec.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/urlMatcherFactory.js b/src/urlMatcherFactory.js index 3b733f9ae..ea5071adc 100644 --- a/src/urlMatcherFactory.js +++ b/src/urlMatcherFactory.js @@ -917,7 +917,8 @@ function $UrlMatcherFactory() { } function getType(config, urlType, location) { - if (config.type && urlType) throw new Error("Param '"+id+"' has two type configurations."); + if (config.type && urlType.name !== 'string') throw new Error("Param '"+id+"' has two type configurations."); + if (config.type && urlType.name === 'string' && $types[config.type]) return $types[config.type]; if (urlType) return urlType; if (!config.type) return (location === "config" ? $types.any : $types.string); return config.type instanceof Type ? config.type : new Type(config.type); diff --git a/test/urlMatcherFactorySpec.js b/test/urlMatcherFactorySpec.js index 3d6cf15d4..aa42f12f3 100755 --- a/test/urlMatcherFactorySpec.js +++ b/test/urlMatcherFactorySpec.js @@ -520,6 +520,21 @@ describe("urlMatcherFactory", function () { expect(m.format({ foo: 5, flag: true })).toBe("/5/1"); }); + it("should match types named only in params", function () { + var m = new UrlMatcher("/{foo}/{flag}", { + params: { + foo: { + type: 'int' + }, + flag: { + type: 'bool' + } + } + }); + expect(m.exec("/1138/1")).toEqual({ foo: 1138, flag: true }); + expect(m.format({ foo: 5, flag: true })).toBe("/5/1"); + }); + it("should encode/decode dates", function () { var m = new UrlMatcher("/calendar/{date:date}"), result = m.exec("/calendar/2014-03-26");