query-string icon indicating copy to clipboard operation
query-string copied to clipboard

Encode empty arrays

Open mitar opened this issue 5 years ago • 4 comments

For my use case it would be great if there would be a way to encode an empty array as well. For example, in bracket style, maybe {a: []} could be encoded as a[] only (while {a: [1]} is encoded as a[]=1).

mitar avatar Jan 06 '20 09:01 mitar

See my answer in https://github.com/sindresorhus/query-string/issues/228#issuecomment-575197530. It applies to this too.

sindresorhus avatar Jan 16 '20 15:01 sindresorhus

Yes, but bracket style does not help here (which is your suggestion there).

So bracket style is less ambiguous, so adding a[] would solve this only ambiguous I know of which still exists for this style.

mitar avatar Jan 16 '20 19:01 mitar

That style is already used for null values: https://github.com/sindresorhus/query-string/blob/db66e68fc7e323995de713c38504a192472038fa/test/stringify.js#L117

I am tempted to say that null should not be included and use x[] to mean empty array. That would be a breaking change though, so probably better as an opt-in.

sindresorhus avatar Feb 02 '20 11:02 sindresorhus

I think bar[]= should be an empty array, while bar should be null, bar=null is string null, and bar= is empty string.

mitar avatar Feb 02 '20 20:02 mitar