Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Commit 79f6ecc

Browse files
committed
Merge pull request #3 from aaronhaines/master
Handle simple query string parameters
2 parents 63e8155 + 429c95e commit 79f6ecc

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

src/rhumb.js

+21-6
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,35 @@ function create (){
112112
}
113113

114114
router.match = function(path){
115-
var parts = path.split("/").filter(falsy)
116-
, match = findIn(parts, tree)
117-
118-
if(match){
119-
return match.fn.apply(match.fn, [match.params])
115+
116+
var split = path.split("?").filter(falsy)
117+
, parts = split[0].split("/").filter(falsy)
118+
, params = parseQueryString(split[1])
119+
, match = findIn(parts, tree)
120+
121+
if(match){
122+
for (var prop in match.params) {
123+
params[prop] = match.params[prop]
120124
}
121-
}
125+
return match.fn.apply(match.fn, [params])
126+
}
127+
}
122128
return router
123129
}
124130

125131
function falsy(d){
126132
return !!d
127133
}
128134

135+
function parseQueryString(s) {
136+
if(!s) return {}
137+
return s.split("&").filter(falsy).reduce(function(qs, kv) {
138+
var pair = kv.split('=').filter(falsy)
139+
qs[pair[0]] = pair[1]
140+
return qs
141+
}, {})
142+
}
143+
129144

130145
function parse(ptn){
131146
var variable = /^{(\w+)}$/

test/test.rhumb.js

+47-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,53 @@ describe("routing", function(){
2828
expect(router.match("/sing/bird-song")).to.eql({
2929
sound : "bird-song"
3030
})
31-
})
31+
})
32+
it("should pass query string params to callback", function(){
33+
var router = rhumb.create()
34+
35+
router.add("/sing", function(params){
36+
return params
37+
})
38+
expect(router.match("/sing?foo=bar&baz=bam")).to.eql({
39+
foo : "bar"
40+
, baz : "bam"
41+
})
42+
})
43+
it("should pass query string params and path params to callback", function(){
44+
var router = rhumb.create()
45+
46+
router.add("/sing/{sound}", function(params){
47+
return params
48+
})
49+
expect(router.match("/sing/bird-song?foo=bar&baz=bam")).to.eql({
50+
sound : "bird-song"
51+
, foo : "bar"
52+
, baz : "bam"
53+
})
54+
})
55+
it("should add query string params without values as undefined", function(){
56+
var router = rhumb.create()
57+
58+
router.add("/sing/{sound}", function(params){
59+
return params
60+
})
61+
expect(router.match("/sing/bird-song?foo=&bar&baz=bop")).to.eql({
62+
sound : "bird-song"
63+
, foo : undefined
64+
, bar : undefined
65+
, baz : "bop"
66+
})
67+
})
68+
it("should handle incomplete query strings", function(){
69+
var router = rhumb.create()
70+
71+
router.add("/sing", function(params){
72+
return params
73+
})
74+
expect(router.match("/sing?foo=&")).to.eql({
75+
foo : undefined
76+
})
77+
})
3278
})
3379
describe("matching with variable paths", function(){
3480
it("should match /{foo} with path /bar", function(){

0 commit comments

Comments
 (0)