Skip to content

Commit 13776a9

Browse files
committed
Merge pull request #337 from broeks/master
modify url matcher to accomodate snake case GET request parameters
2 parents 93dd696 + 98917f8 commit 13776a9

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/urlMatcherFactory.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function UrlMatcher(pattern) {
6262
params = this.params = [];
6363

6464
function addParameter(id) {
65-
if (!/^\w+$/.test(id)) throw new Error("Invalid parameter name '" + id + "' in pattern '" + pattern + "'");
65+
if (!/^\w+(-+\w+)*$/.test(id)) throw new Error("Invalid parameter name '" + id + "' in pattern '" + pattern + "'");
6666
if (names[id]) throw new Error("Duplicate parameter name '" + id + "' in pattern '" + pattern + "'");
6767
names[id] = true;
6868
params.push(id);

test/urlMatcherFactorySpec.js

+20
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@ describe("UrlMatcher", function () {
2020
expect(params).toContain('to');
2121
});
2222

23+
it("handles proper snake case parameter names", function(){
24+
var matcher = new UrlMatcher('/users/?from&to&snake-case&snake-case-triple');
25+
var params = matcher.parameters();
26+
expect(params.length).toBe(4);
27+
expect(params).toContain('from');
28+
expect(params).toContain('to');
29+
expect(params).toContain('snake-case');
30+
expect(params).toContain('snake-case-triple');
31+
});
32+
33+
it("handles invalid snake case parameter names", function(){
34+
expect(function() { new UrlMatcher('/users/?from&to&-snake'); }).toThrow(
35+
"Invalid parameter name '-snake' in pattern '/users/?from&to&-snake'"
36+
);
37+
38+
expect(function() { new UrlMatcher('/users/?from&to&snake-'); }).toThrow(
39+
"Invalid parameter name 'snake-' in pattern '/users/?from&to&snake-'"
40+
);
41+
});
42+
2343
it(".exec() captures parameter values", function () {
2444
expect(
2545
new UrlMatcher('/users/:id/details/{type}/{repeat:[0-9]+}?from&to')

0 commit comments

Comments
 (0)