Skip to content

Commit

Permalink
Fix value being decoded twice with arrayFormat option set to `separ…
Browse files Browse the repository at this point in the history
…ator` (#243)

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
cheqianxiao and sindresorhus committed Apr 12, 2020
1 parent 8a07aca commit 3b4c295
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -246,7 +246,7 @@ function parse(input, options) {

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

Expand Down
13 changes: 13 additions & 0 deletions test/parse.js
Expand Up @@ -310,3 +310,16 @@ test('query strings having comma encoded and format option as `comma`', t => {
]
});
});

test('value should not be decoded twice with `arrayFormat` option set as `separator`', t => {
t.deepEqual(queryString.parse('foo=2020-01-01T00:00:00%2B03:00', {arrayFormat: 'separator'}), {
foo: '2020-01-01T00:00:00+03:00'
});
});

// 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 => {
t.deepEqual(queryString.parse('id=1%2C2%2C3', {arrayFormat: 'comma', parseNumbers: true}), {
id: [1, 2, 3]
});
});

0 comments on commit 3b4c295

Please sign in to comment.