Skip to content

Commit 19c43d4

Browse files
authored
Fix arrayFormat bracket-separator with a URL encoded value (#392)
1 parent 4287e77 commit 19c43d4

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

base.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ function parserForArrayFormat(options) {
221221

222222
const arrayValue = value === null
223223
? []
224-
: value.split(options.arrayFormatSeparator).map(item => decode(item, options));
224+
: decode(value, options).split(options.arrayFormatSeparator);
225225

226226
if (accumulator[key] === undefined) {
227227
accumulator[key] = arrayValue;

test/parse.js

+25-10
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ test('handle multiple values and preserve appearance order with indexes', t => {
110110
});
111111

112112
test('query strings params including embedded `=`', t => {
113-
t.deepEqual(queryString.parse('?param=https%3A%2F%2Fsomeurl%3Fid%3D2837'), {param: 'https://someurl?id=2837'});
113+
const value = 'https://someurl?id=2837';
114+
t.deepEqual(queryString.parse(`param=${encodeURIComponent(value)}`), {param: 'https://someurl?id=2837'});
114115
});
115116

116117
test('object properties', t => {
@@ -215,6 +216,16 @@ test('query strings having a brackets+separator array and format option as `brac
215216
}), {foo: ['']});
216217
});
217218

219+
test('query strings having a brackets+separator array and format option as `bracket-separator` with a URL encoded value', t => {
220+
const key = 'foo[]';
221+
const value = 'a,b,c,d,e,f';
222+
t.deepEqual(queryString.parse(`?${encodeURIComponent(key)}=${encodeURIComponent(value)}`, {
223+
arrayFormat: 'bracket-separator',
224+
}), {
225+
foo: ['a', 'b', 'c', 'd', 'e', 'f'],
226+
});
227+
});
228+
218229
test('query strings having = within parameters (i.e. GraphQL IDs)', t => {
219230
t.deepEqual(queryString.parse('foo=bar=&foo=ba=z='), {foo: ['bar=', 'ba=z=']});
220231
});
@@ -305,7 +316,8 @@ test('decode keys and values', t => {
305316
});
306317

307318
test('disable decoding of keys and values', t => {
308-
t.deepEqual(queryString.parse('tags=postal%20office,burger%2C%20fries%20and%20coke', {decode: false}), {tags: 'postal%20office,burger%2C%20fries%20and%20coke'});
319+
const value = 'postal office,burger, fries and coke';
320+
t.deepEqual(queryString.parse(`tags=${encodeURIComponent(value)}`, {decode: false}), {tags: 'postal%20office%2Cburger%2C%20fries%20and%20coke'});
309321
});
310322

311323
test('number value returns as string by default', t => {
@@ -376,7 +388,8 @@ test('parse throws TypeError for invalid arrayFormatSeparator', t => {
376388
});
377389

378390
test('query strings having comma encoded and format option as `comma`', t => {
379-
t.deepEqual(queryString.parse('foo=zero%2Cone,two%2Cthree', {arrayFormat: 'comma'}), {
391+
const values = ['zero,one', 'two,three'];
392+
t.deepEqual(queryString.parse(`foo=${encodeURIComponent(values[0])},${encodeURIComponent(values[1])}`, {arrayFormat: 'comma'}), {
380393
foo: [
381394
'zero,one',
382395
'two,three',
@@ -392,7 +405,8 @@ test('value should not be decoded twice with `arrayFormat` option set as `separa
392405

393406
// See https://github.com/sindresorhus/query-string/issues/242
394407
test('value separated by encoded comma will not be parsed as array with `arrayFormat` option set to `comma`', t => {
395-
t.deepEqual(queryString.parse('id=1%2C2%2C3', {arrayFormat: 'comma', parseNumbers: true}), {
408+
const value = '1,2,3';
409+
t.deepEqual(queryString.parse(`id=${encodeURIComponent(value)}`, {arrayFormat: 'comma', parseNumbers: true}), {
396410
id: [1, 2, 3],
397411
});
398412
});
@@ -406,7 +420,8 @@ test('query strings having (:list) colon-list-separator arrays including null va
406420
});
407421

408422
test('types option: can override a parsed number to be a string ', t => {
409-
t.deepEqual(queryString.parse('phoneNumber=%2B380951234567', {
423+
const phoneNumber = '+380951234567';
424+
t.deepEqual(queryString.parse(`phoneNumber=${encodeURIComponent(phoneNumber)}`, {
410425
parseNumbers: true,
411426
types: {
412427
phoneNumber: 'string',
@@ -426,7 +441,7 @@ test('types option: can override a parsed boolean value to be a string', t => {
426441
});
427442

428443
test('types option: can override parsed numbers arrays to be string[]', t => {
429-
t.deepEqual(queryString.parse('ids=999%2C998%2C997&items=1%2C2%2C3', {
444+
t.deepEqual(queryString.parse('ids=999,998,997&items=1,2,3', {
430445
arrayFormat: 'comma',
431446
parseNumbers: true,
432447
types: {
@@ -439,7 +454,7 @@ test('types option: can override parsed numbers arrays to be string[]', t => {
439454
});
440455

441456
test('types option: can override string arrays to be number[]', t => {
442-
t.deepEqual(queryString.parse('ids=001%2C002%2C003&items=1%2C2%2C3', {
457+
t.deepEqual(queryString.parse('ids=1,2,3&items=1,2,3', {
443458
arrayFormat: 'comma',
444459
types: {
445460
ids: 'number[]',
@@ -451,7 +466,7 @@ test('types option: can override string arrays to be number[]', t => {
451466
});
452467

453468
test('types option: can override an array to be string', t => {
454-
t.deepEqual(queryString.parse('ids=001%2C002%2C003&items=1%2C2%2C3', {
469+
t.deepEqual(queryString.parse('ids=001,002,003&items=1,2,3', {
455470
arrayFormat: 'comma',
456471
parseNumbers: true,
457472
types: {
@@ -488,7 +503,7 @@ test('types option: when value is not of specified type, it will safely parse th
488503
});
489504

490505
test('types option: array types will have no effect if arrayFormat is set to "none"', t => {
491-
t.deepEqual(queryString.parse('ids=001%2C002%2C003&foods=apple%2Corange%2Cmango', {
506+
t.deepEqual(queryString.parse('ids=001,002,003&foods=apple,orange,mango', {
492507
arrayFormat: 'none',
493508
types: {
494509
ids: 'number[]',
@@ -512,7 +527,7 @@ test('types option: will parse the value as number if specified in type but pars
512527
});
513528

514529
test('types option: all supported types work in conjunction with one another', t => {
515-
t.deepEqual(queryString.parse('ids=001%2C002%2C003&items=1%2C2%2C3&price=22%2E00&numbers=1%2C2%2C3&double=5&number=20', {
530+
t.deepEqual(queryString.parse('ids=001,002,003&items=1,2,3&price=22.00&numbers=1,2,3&double=5&number=20', {
516531
arrayFormat: 'comma',
517532
types: {
518533
ids: 'string',

0 commit comments

Comments
 (0)