Skip to content

Commit

Permalink
query object preserves missing equal signs for keys without values
Browse files Browse the repository at this point in the history
  • Loading branch information
derek-watson committed Nov 19, 2011
1 parent db9aa9a commit ae7c16f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
26 changes: 26 additions & 0 deletions spec/javascripts/query.spec.js
@@ -1,4 +1,28 @@
describe("Query", function() {

describe('Construction', function() {

it('should include an equal sign if there was one present without a query value', function() {
q = new Query('?11=');
expect(q.toString()).toEqual('?11=');
});

it('should not include an equal sign if one was not present originally', function() {
q = new Query('?11');
expect(q.toString()).toEqual('?11');
});

it('should preserve missing equals signs across many keys', function() {
q = new Query('?11&12&13&14');
expect(q.toString()).toEqual('?11&12&13&14');
});

it('should preserve missing equals signs in a mixed scenario', function() {
q = new Query('?11=eleven&12=&13&14=fourteen');
expect(q.toString()).toEqual('?11=eleven&12=&13&14=fourteen');
});

});

describe("Manipulation", function() {
var q;
Expand Down Expand Up @@ -117,5 +141,7 @@ describe("Query", function() {
q = new Query('?multi+word=true').replaceParam('multi word', 2);
expect(q.toString()).toEqual('?multi word=2');
});


});
});
15 changes: 11 additions & 4 deletions src/query.js
Expand Up @@ -7,7 +7,7 @@ var Query = function (queryString) {

var // parseQuery(q) parses the uri query string and returns a multi-dimensional array of the components
parseQuery = function (q) {
var arr = [], i, ps, p, keyval;
var arr = [], i, ps, p, kvp, k, v;

if (typeof (q) === 'undefined' || q === null || q === '') {
return arr;
Expand All @@ -21,8 +21,10 @@ var Query = function (queryString) {

for (i = 0; i < ps.length; i++) {
p = ps[i];
keyval = p.split('=');
arr.push([keyval[0], keyval[1]]);
kvp = p.split('=');
k = kvp[0];
v = p.indexOf('=') === -1 ? null : (kvp[1] === null ? '' : kvp[1]);
arr.push([k, v]);
}

return arr;
Expand All @@ -38,7 +40,12 @@ var Query = function (queryString) {
if (s.length > 0) {
s += '&';
}
s += param.join('=');
if (param[1] === null) {
s += param[0];
}
else {
s += param.join('=');
}
}
return s.length > 0 ? '?' + s : s;
},
Expand Down

0 comments on commit ae7c16f

Please sign in to comment.