Skip to content

Commit

Permalink
feat: store multiple query parameters with the same key in an array
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
troch committed Oct 7, 2015
1 parent 9e1af65 commit 3efd958
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
7 changes: 6 additions & 1 deletion modules/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ let parseQueryParams = path => {
return searchPart.split('&')
.map(_ => _.split('='))
.reduce((obj, m) => {
obj[m[0]] = m[1] === undefined ? '' : m[1]
let val = m[1] === undefined ? '' : m[1]
let existingVal = obj[m[0]]

if (existingVal === undefined) obj[m[0]] = val
else obj[m[0]] = Array.isArray(existingVal) ? existingVal.concat(val) : [ existingVal, val ]

return obj
}, {})
}
Expand Down
1 change: 1 addition & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('Path', function () {
var path = new Path('/users?offset&limit');
// Successful match & partial match
path.match('/users?offset=31&limit=15').should.eql({ offset: '31', limit: '15' });
path.match('/users?offset=31&offset=30&limit=15').should.eql({ offset: ['31', '30'], limit: '15' });
path.match('/users?offset&limit=15').should.eql({ offset: '', limit: '15' });
path.match('/users?limit=15').should.eql({ limit: '15' });
path.partialMatch('/users?offset&limits=1').should.eql({ offset: '' });
Expand Down

0 comments on commit 3efd958

Please sign in to comment.