Skip to content

fix(urlMatcherFactory.js): change to allow url query param names to c… #2415

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

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 2 additions & 2 deletions src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function UrlMatcher(pattern, config, parentMatcher) {
// \\. - a backslash escape
// \{(?:[^{}\\]+|\\.)*\} - a matched set of curly braces containing other atoms
var placeholder = /([:*])([\w\[\]]+)|\{([\w\[\]]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
searchPlaceholder = /([:]?)([\w\[\]-]+)|\{([\w\[\]-]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
searchPlaceholder = /([:]?)([\w\[\].-]+)|\{([\w\[\].-]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
compiled = '^', last = 0, m,
segments = this.segments = [],
parentParams = parentMatcher ? parentMatcher.params : {},
Expand All @@ -92,7 +92,7 @@ function UrlMatcher(pattern, config, parentMatcher) {
function addParameter(id, type, config, location) {
paramNames.push(id);
if (parentParams[id]) return parentParams[id];
if (!/^\w+(-+\w+)*(?:\[\])?$/.test(id)) throw new Error("Invalid parameter name '" + id + "' in pattern '" + pattern + "'");
if (!/^\w+([-.]+\w+)*(?:\[\])?$/.test(id)) throw new Error("Invalid parameter name '" + id + "' in pattern '" + pattern + "'");
if (params[id]) throw new Error("Duplicate parameter name '" + id + "' in pattern '" + pattern + "'");
params[id] = new $$UMFP.Param(id, type, config, location);
return params[id];
Expand Down
22 changes: 22 additions & 0 deletions test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,28 @@ describe("UrlMatcher", function () {
});
});

describe("parameters containing periods", function() {
it("should match if properly formatted", function() {
var matcher = new UrlMatcher('/users/?from&to&with.periods&with.periods.also');
var params = matcher.parameters();

expect(params.length).toBe(4);
expect(params).toContain('from');
expect(params).toContain('to');
expect(params).toContain('with.periods');
expect(params).toContain('with.periods.also');
expect(matcher.parameters()).toEqual(['from','to','with.periods','with.periods.also']);
});

it("should not match if invalid", function() {
var err = "Invalid parameter name '.periods' in pattern '/users/?from&to&.periods'";
expect(function() { new UrlMatcher('/users/?from&to&.periods'); }).toThrow(err);

err = "Invalid parameter name 'periods.' in pattern '/users/?from&to&periods.'";
expect(function() { new UrlMatcher('/users/?from&to&periods.'); }).toThrow(err);
});
});

describe(".exec()", function() {
it("should capture parameter values", function () {
var m = new UrlMatcher('/users/:id/details/{type}/{repeat:[0-9]+}?from&to');
Expand Down