Skip to content

Commit

Permalink
allow multiple query parameters with same name
Browse files Browse the repository at this point in the history
Allows multiple query parameters to have the same name. The values are parsed into an array. As proposed in issue millermedeiros#90.
Changes interpolate in pattern_lexer.js and decodeQueryString in intro.js as discussed.
  • Loading branch information
Kamaruni authored and sjhewitt committed Apr 15, 2015
1 parent 1e5ac03 commit df5619d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
11 changes: 10 additions & 1 deletion dev/src/intro.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,16 @@
while (n--) {
item = queryArr[n].split('=');
val = shouldTypecast ? typecastValue(item[1]) : item[1];
obj[item[0]] = (typeof val === 'string')? decodeURIComponent(val) : val;
decodedVal = (typeof val === 'string')? decodeURIComponent(val) : val;
if (isArray(obj[item[0]])) {
obj[item[0]].unshift(decodedVal)
}
else if (obj[item[0]]) {
obj[item[0]] = [decodedVal, obj[item[0]]]
}
else {
obj[item[0]] = decodedVal
}
}
return obj;
}
12 changes: 10 additions & 2 deletions dev/src/pattern_lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,17 @@
prop = (prop.substr(0, 1) === '?')? prop.substr(1) : prop;
if (replacements[prop] != null) {
if (typeof replacements[prop] === 'object') {
var queryParts = [];
var queryParts = [], rep;
for(var key in replacements[prop]) {
queryParts.push(encodeURI(key + '=' + replacements[prop][key]));
rep = replacements[prop][key];
if (isArray(rep)) {
for (var k in rep) {
queryParts.push(encodeURI(key + '=' + rep[k]));
}
}
else {
queryParts.push(encodeURI(key + '=' + rep));
}
}
val = '?' + queryParts.join('&');
} else {
Expand Down
1 change: 1 addition & 0 deletions dev/tests/spec/interpolate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@ describe('Route.interpolate()', function(){
var a = crossroads.addRoute('/{foo}/:?query:');
expect( a.interpolate({foo: 'lorem', query: {some: 'test'}}) ).toEqual( '/lorem/?some=test' );
expect( a.interpolate({foo: 'dolor-sit', query: {multiple: 'params', works: 'fine'}}) ).toEqual( '/dolor-sit/?multiple=params&works=fine' );
expect( a.interpolate({foo: 'amet', query: {multiple: ['paramsWith', 'sameName'], works: 'fine2'}}) ).toEqual( '/amet/?multiple=paramsWith&multiple=sameName&works=fine2' );
});
});
16 changes: 15 additions & 1 deletion dev/tests/spec/parse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var crossroads = crossroads || require('../../../dist/crossroads');


describe('crossroads.parse()', function(){

var _prevTypecast;


Expand Down Expand Up @@ -1018,6 +1017,21 @@ describe('crossroads.parse()', function(){
expect( t2 ).toEqual( {lorem : 'ipsum', asd : '123', bar : 'false'} );
});
});

describe('multiple query parameters with same name', function () {
it('should parse values into an array', function () {
var t1;
var r = crossroads.addRoute('foo.php:?query:', function(a) {
t1 = a;
});

crossroads.parse('foo.php?name=x&name=y');
expect( t1 ).toEqual( {name : ['x', 'y']} );

crossroads.parse('foo.php?name=x&type=json&name=y&name=z');
expect( t1 ).toEqual( {name : ['x', 'y', 'z'], type : 'json'} );
});
});
});


Expand Down

0 comments on commit df5619d

Please sign in to comment.