Skip to content

Commit

Permalink
querystring: Parse multiple separator characters
Browse files Browse the repository at this point in the history
Fix querystring.parse to handle multiple separator characters

PR-URL: #3807
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Brian White <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
yosuke-furukawa authored and rvagg committed Dec 4, 2015
1 parent 2c52bbc commit fe9baa7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ QueryString.stringify = QueryString.encode = function(obj, sep, eq, options) {
QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
sep = sep || '&';
eq = eq || '=';
const eqLen = eq.length;
var obj = {};

if (typeof qs !== 'string' || qs.length === 0) {
Expand Down Expand Up @@ -235,7 +236,7 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {

if (idx >= 0) {
k = decodeStr(x.substring(0, idx), decode);
v = decodeStr(x.substring(idx + 1), decode);
v = decodeStr(x.substring(idx + eqLen), decode);
} else {
k = decodeStr(x, decode);
v = '';
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-querystring-multichar-separator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const qs = require('querystring');

assert.deepEqual(
qs.parse('foo=>bar&&bar=>baz', '&&', '=>'),
{foo: 'bar', bar: 'baz'}
);

assert.strictEqual(
qs.stringify({foo: 'bar', bar: 'baz'}, '&&', '=>'),
'foo=>bar&&bar=>baz'
);

assert.deepEqual(
qs.parse('foo==>bar, bar==>baz', ', ', '==>'),
{foo: 'bar', bar: 'baz'}
);

assert.strictEqual(
qs.stringify({foo: 'bar', bar: 'baz'}, ', ', '==>'),
'foo==>bar, bar==>baz'
);

0 comments on commit fe9baa7

Please sign in to comment.