From daf52ca7c3cd0ed0adccb403e83b4edb19c4b490 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Mon, 23 Jan 2017 17:29:18 +0100 Subject: [PATCH] [fix] Convert plus signs to spaces Fixes #7 --- index.js | 13 ++++++++++++- test.js | 16 ++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index e1a2684..3ecf79d 100644 --- a/index.js +++ b/index.js @@ -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. * @@ -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; diff --git a/test.js b/test.js index 06d8cb2..78074d4 100644 --- a/test.js +++ b/test.js @@ -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); @@ -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'); + }); }); });