Skip to content

Commit 0f17684

Browse files
filipesilvawesleycho
authored andcommitted
feat(UrlMatcher): Add param only type names
1 parent fc53e92 commit 0f17684

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/params/param.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ export default class Param {
3838
}
3939

4040
function getType(config, urlType, location) {
41-
if (config.type && urlType) throw new Error(`Param '${id}' has two type configurations.`);
41+
if (config.type && urlType && urlType.name !== 'string') throw new Error(`Param '${id}' has two type configurations.`);
42+
if (config.type && urlType && urlType.name === 'string' && paramTypes.type(config.type)) return paramTypes.type(config.type);
4243
if (urlType) return urlType;
4344
if (!config.type) return (location === "config" ? paramTypes.type("any") : paramTypes.type("string"));
4445
return config.type instanceof Type ? config.type : paramTypes.type(config.type);

test/urlMatcherFactorySpec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,21 @@ describe("urlMatcherFactory", function () {
550550
expect(m.format({ foo: 5, flag: true })).toBe("/5/1");
551551
});
552552

553+
it("should match types named only in params", function () {
554+
var m = new UrlMatcher("/{foo}/{flag}", {
555+
params: {
556+
foo: {
557+
type: 'int'
558+
},
559+
flag: {
560+
type: 'bool'
561+
}
562+
}
563+
});
564+
expect(m.exec("/1138/1")).toEqual({ foo: 1138, flag: true });
565+
expect(m.format({ foo: 5, flag: true })).toBe("/5/1");
566+
});
567+
553568
it("should encode/decode dates", function () {
554569
var m = new UrlMatcher("/calendar/{date:date}"),
555570
result = m.exec("/calendar/2014-03-26");

0 commit comments

Comments
 (0)