Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When only receiving values with encoded array value, decode values #287

Merged
merged 3 commits into from
Oct 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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