Skip to content

Commit

Permalink
Preserve encoded commas when using arrayFormat: 'comma' (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheqianxiao committed Feb 2, 2020
1 parent db66e68 commit a1d108f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Expand Up @@ -106,7 +106,7 @@ function parserForArrayFormat(options) {
case 'comma':
return (key, value, accumulator) => {
const isArray = typeof value === 'string' && value.split('').indexOf(',') > -1;
const newValue = isArray ? value.split(',') : value;
const newValue = isArray ? value.split(',').map(item => decode(item, options)) : value === null ? value : decode(value, options);
accumulator[key] = newValue;
};

Expand Down Expand Up @@ -220,7 +220,7 @@ function parse(input, options) {

// Missing `=` should be `null`:
// http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
value = value === undefined ? null : decode(value, options);
value = value === undefined ? null : options.arrayFormat === 'comma' ? value : decode(value, options);
formatter(decode(key, options), value, ret);
}

Expand Down
9 changes: 9 additions & 0 deletions test/parse.js
Expand Up @@ -284,3 +284,12 @@ test('parseNumbers and parseBooleans can work with arrayFormat at the same time'
t.deepEqual(queryString.parse('foo=true,false&bar=1,2', {parseNumbers: true, parseBooleans: true, arrayFormat: 'comma'}), {foo: [true, false], bar: [1, 2]});
t.deepEqual(queryString.parse('foo[0]=true&foo[1]=false&bar[0]=1&bar[1]=2', {parseNumbers: true, parseBooleans: true, arrayFormat: 'index'}), {foo: [true, false], bar: [1, 2]});
});

test('query strings having comma encoded and format option as `comma`', t => {
t.deepEqual(queryString.parse('foo=zero%2Cone,two%2Cthree', {arrayFormat: 'comma'}), {
foo: [
'zero,one',
'two,three'
]
});
});

0 comments on commit a1d108f

Please sign in to comment.