Skip to content

Commit

Permalink
[fix] Convert plus signs to spaces (#8)
Browse files Browse the repository at this point in the history
Fixes #7
  • Loading branch information
lpinca authored Mar 20, 2017
1 parent ca090ae commit 0f3e729
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

var has = Object.prototype.hasOwnProperty;

/**
* Decode a URI encoded string.
*
* @param {String} input The URI encoded string.
* @returns {String} The decoded string.
* @api private
*/
function decode(input) {
return decodeURIComponent(input.replace(/\+/g, ' '));
}

/**
* Simple query string parser.
*
Expand All @@ -21,7 +32,7 @@ function querystring(query) {
//
for (;
part = parser.exec(query);
result[decodeURIComponent(part[1])] = decodeURIComponent(part[2])
result[decode(part[1])] = decode(part[2])
);

return result;
Expand Down
16 changes: 14 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('querystringify', function () {

it('works with object keys with empty string values', function () {
assume(qs.stringify({ foo: '' })).equals('foo=');
})
});

it('works with nulled objects', function () {
var obj = Object.create(null);
Expand Down Expand Up @@ -70,6 +70,18 @@ describe('querystringify', function () {
assume(obj.foo).equals('');
assume(obj.bar).equals('');
assume(obj.shizzle).equals('mynizzle');
})
});

it('decodes plus signs', function () {
var obj = qs.parse('foo+bar=baz+qux');

assume(obj).is.a('object');
assume(obj['foo bar']).equals('baz qux');

obj = qs.parse('foo+bar=baz%2Bqux');

assume(obj).is.a('object');
assume(obj['foo bar']).equals('baz+qux');
});
});
});

0 comments on commit 0f3e729

Please sign in to comment.