Skip to content

Commit

Permalink
When only receiving values with encoded array value, decode values (#287
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mishugana committed Oct 17, 2020
1 parent 6427722 commit b38f06c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions index.js
Expand Up @@ -122,8 +122,10 @@ function parserForArrayFormat(options) {
case 'comma':
case 'separator':
return (key, value, accumulator) => {
const isArray = typeof value === 'string' && value.split('').indexOf(options.arrayFormatSeparator) > -1;
const newValue = isArray ? value.split(options.arrayFormatSeparator).map(item => decode(item, options)) : value === null ? value : decode(value, options);
const isArray = typeof value === 'string' && value.includes(options.arrayFormatSeparator);
const isEncodedArray = (typeof value === 'string' && !isArray && decode(value, options).includes(options.arrayFormatSeparator));
value = isEncodedArray ? decode(value, options) : value;
const newValue = isArray || isEncodedArray ? value.split(options.arrayFormatSeparator).map(item => decode(item, options)) : value === null ? value : decode(value, options);
accumulator[key] = newValue;
};

Expand Down
2 changes: 1 addition & 1 deletion test/parse.js
Expand Up @@ -323,7 +323,7 @@ test('value should not be decoded twice with `arrayFormat` option set as `separa
});

// See https://github.com/sindresorhus/query-string/issues/242
test.failing('value separated by encoded comma will not be parsed as array with `arrayFormat` option set to `comma`', t => {
test('value separated by encoded comma will not be parsed as array with `arrayFormat` option set to `comma`', t => {
t.deepEqual(queryString.parse('id=1%2C2%2C3', {arrayFormat: 'comma', parseNumbers: true}), {
id: [1, 2, 3]
});
Expand Down

0 comments on commit b38f06c

Please sign in to comment.