Skip to content

Commit 6a371f9

Browse files
committed
feat(UrlMatcher): Add param only type names
1 parent 6f723a5 commit 6a371f9

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/urlMatcherFactory.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,8 @@ function $UrlMatcherFactory() {
917917
}
918918

919919
function getType(config, urlType, location) {
920-
if (config.type && urlType) throw new Error("Param '"+id+"' has two type configurations.");
920+
if (config.type && urlType.name !== 'string') throw new Error("Param '"+id+"' has two type configurations.");
921+
if (config.type && urlType.name === 'string' && $types[config.type]) return $types[config.type];
921922
if (urlType) return urlType;
922923
if (!config.type) return (location === "config" ? $types.any : $types.string);
923924
return config.type instanceof Type ? config.type : new Type(config.type);

test/urlMatcherFactorySpec.js

+15
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,21 @@ describe("urlMatcherFactory", function () {
520520
expect(m.format({ foo: 5, flag: true })).toBe("/5/1");
521521
});
522522

523+
it("should match types named only in params", function () {
524+
var m = new UrlMatcher("/{foo}/{flag}", {
525+
params: {
526+
foo: {
527+
type: 'int'
528+
},
529+
flag: {
530+
type: 'bool'
531+
}
532+
}
533+
});
534+
expect(m.exec("/1138/1")).toEqual({ foo: 1138, flag: true });
535+
expect(m.format({ foo: 5, flag: true })).toBe("/5/1");
536+
});
537+
523538
it("should encode/decode dates", function () {
524539
var m = new UrlMatcher("/calendar/{date:date}"),
525540
result = m.exec("/calendar/2014-03-26");

0 commit comments

Comments
 (0)