Add support for `arrayFormat: 'bracket-separator'` #276
Conversation
I think dropping the |
|
You need to explicitly document the behavior with empty array, null in array, and empty string in array in the docs. |
|
I would also like to see more tests. |
| queryString.parse('foo[]=1', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); | ||
| //=> {foo: ['1']} | ||
| queryString.parse('foo[]=1|2|3', {arrayFormat: 'separator', arrayFormatSeparator: '|'}); |
sindresorhus
Dec 27, 2020
Owner
It would be more valuable to show
Suggested change
queryString.parse('foo[]=1|2|3', {arrayFormat: 'separator', arrayFormatSeparator: '|'});
queryString.parse('foo[]=1', {arrayFormat: 'separator', arrayFormatSeparator: '|'});
So the user could also see the difference.
It would be more valuable to show
| queryString.parse('foo[]=1|2|3', {arrayFormat: 'separator', arrayFormatSeparator: '|'}); | |
| queryString.parse('foo[]=1', {arrayFormat: 'separator', arrayFormatSeparator: '|'}); |
So the user could also see the difference.
DV8FromTheWorld
Jan 14, 2021
Author
I'm not quite following what this is wanting.
I did realize I accidently left the arrayFormat set to separator here instead of bracket-separator, so I've updated that as needed.
I'm not quite following what this is wanting.
I did realize I accidently left the arrayFormat set to separator here instead of bracket-separator, so I've updated that as needed.
|
And sorry for the slow reply. I've just been overwhelmed with notifications. |
|
I believe I've covered all that was asked. Please let me know if something else needs attention. |
| ``` | ||
| import queryString = require('query-string'); | ||
| queryString.parse('foo[]', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); |
sindresorhus
Jan 19, 2021
Owner
Suggested change
queryString.parse('foo[]', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
queryString.parse('foo[]', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
| queryString.parse('foo[]', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); | |
| queryString.parse('foo[]', {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); |
| @@ -231,7 +252,7 @@ export interface StringifyOptions { | |||
| //=> 'foo=1,2,3' | |||
| ``` | |||
| - `separator`: Serialize arrays by separating elements with character: | |||
| - `separator`: Serialize arrays by separating elements with character: | |||
sindresorhus
Jan 19, 2021
Owner
Suggested change
- `separator`: Serialize arrays by separating elements with character:
- `separator`: Serialize arrays by separating elements with character:
| - `separator`: Serialize arrays by separating elements with character: | |
| - `separator`: Serialize arrays by separating elements with character: |
| ``` | ||
| import queryString = require('query-string'); | ||
| queryString.stringify({foo: []}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); |
sindresorhus
Jan 19, 2021
Owner
Suggested change
queryString.stringify({foo: []}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
queryString.stringify({foo: []}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'});
| queryString.stringify({foo: []}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); | |
| queryString.stringify({foo: []}, {arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'}); |
| @@ -53,8 +53,29 @@ function encoderForArrayFormat(options) { | |||
| return result; | |||
| } | |||
|
|
|||
| const keyValueSep = options.arrayFormat === 'bracket-separator' ? | |||
sindresorhus
Jan 19, 2021
Owner
This code path cannot be hit.
This code path cannot be hit.
| ```js | ||
| const queryString = require('query-string'); | ||
| //Can handle arrays on a single value to product explicit array |
sindresorhus
Jan 19, 2021
Owner
Suggested change
//Can handle arrays on a single value to product explicit array
// Can handle arrays on a single value to product explicit array
| //Can handle arrays on a single value to product explicit array | |
| // Can handle arrays on a single value to product explicit array |
| ```js | ||
| const queryString = require('query-string'); | ||
| //Can handle arrays on a single value to product explicit array |
sindresorhus
Jan 19, 2021
Owner
Suggested change
//Can handle arrays on a single value to product explicit array
// Can handle arrays on a single value to product explicit array
| //Can handle arrays on a single value to product explicit array | |
| // Can handle arrays on a single value to product explicit array |
sindresorhus
Jan 19, 2021
Owner
The readme and index.d.ts should be in sync.
The readme and index.d.ts should be in sync.

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

The idea behind this format is to gain the advantages of short query strings that you have with
commaandseparatorformats while marking a field as explicitly an array via a postfixed[]syntax.There are still are 2 question in regards to implementation:
What should happen for
{foo: []}? Currently, it would drop the parameter from the stringified query which is the same as{foo: null}This is undesirable personally. I feel like
{foo: []}should produce?foo[]unless I specify a flag to drop it.This would be a deviation from the
?bar&bizthat non-arrays get the treatment of when they are null (no trailing=), but I feel like it makes sense as we have enough information to say, explicitly, that this is an array. Whereas if it were{foo: null}we don't know, explicitly, that this is an array, so it would produce?foowhich would mean that it would roundtrip correctly.However, the only way to produce this would be to change how the system is using Array.prototype.reduce as the reducer function is not ever invoked when the array is empty.
What should happen for
{foo: ['one', null, 'three']}. Currently it will drop the null and producefoo[]=one,threeI haven't a strong opinion on this as it matches the functionality of
separator. The only deviation from theseparatorfunctionality in regards to value dropping is thatbracket-separatordoes not drop empty strings.