From 8fb8114d5c3c46c13ec3ad7d8769169f6a8ffb0c Mon Sep 17 00:00:00 2001 From: scottenock Date: Thu, 19 Sep 2024 09:54:46 +0100 Subject: [PATCH] FIX: Decode brack-seperator value before splitting it with seperator The original implementation was splitting the received value before it then decoded the value. Because the value was URL encoded, the split function could not find the seperator to successfully split. Adjusted the order of operations which fixes #388 --- base.js | 2 +- test/parse.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/base.js b/base.js index ccbdb6e..f72e9a8 100644 --- a/base.js +++ b/base.js @@ -221,7 +221,7 @@ function parserForArrayFormat(options) { const arrayValue = value === null ? [] - : value.split(options.arrayFormatSeparator).map(item => decode(item, options)); + : decode(value, options).split(options.arrayFormatSeparator) if (accumulator[key] === undefined) { accumulator[key] = arrayValue; diff --git a/test/parse.js b/test/parse.js index 3409255..3b53844 100644 --- a/test/parse.js +++ b/test/parse.js @@ -215,6 +215,16 @@ test('query strings having a brackets+separator array and format option as `brac }), {foo: ['']}); }); +test('query strings having a brackets+separator array and format option as `bracket-separator` with a URL encoded value', t => { + t.deepEqual(queryString.parse('?testA%5B%5D=1&testB%5B%5D=a%2Cb%2Cc%2Cd%2Ce%2Cf&testC=true', { + arrayFormat: 'bracket-separator', + }), { + testA: ['1'], + testB: ['a', 'b', 'c', 'd', 'e', 'f'], + testC: 'true', + }); +}); + test('query strings having = within parameters (i.e. GraphQL IDs)', t => { t.deepEqual(queryString.parse('foo=bar=&foo=ba=z='), {foo: ['bar=', 'ba=z=']}); });