Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(UrlMatcher): Add param only type names #2289

Merged
merged 1 commit into from
Oct 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since urlType is optional, this code will break on url-less parameters. I liked the direction of your previous logic better, where it checks if config.type is a string.

angular.isString is a handy utility for checking if something is a string or not

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like this:

if (angular.isString(config.type))
  return $types[config.type];
if (config.type instanceof Type)
  return config.type;
return new Type(config.type);

Expand Down
15 changes: 15 additions & 0 deletions test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for url-less parameters, like our discussion in issue #983

https://github.com/angular-ui/ui-router/blob/master/test/stateSpec.js#L1157-L1306

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got so caught up on the type tests in the urlMatcher that I forgot tests for url-less params. My bad!

it("should encode/decode dates", function () {
var m = new UrlMatcher("/calendar/{date:date}"),
result = m.exec("/calendar/2014-03-26");
Expand Down