-
-
Notifications
You must be signed in to change notification settings - Fork 731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stringify with comma
format should add square brackets to parameter name to identity array with single value
#315
Comments
comma
format should add brackets to parameter name to identity array with single valuecomma
format should add square brackets to parameter name to identity array with single value
@daggerjames what you think? |
PR: |
I'm a little worry about that adding square brackets would cause version break rather than minor version update with feature improvement. A quick fix would be use another parameter to control whether or not the output of stringify should contains whether or not the brackets. But it would increment the complexity of this lib. In fact, there is no 'official' documentation that defines how 'comma' should behave in this situation. I'm cool with brackets, and when I do that PR, I made 'comma' as explicit required as an option, as a hint that 'you know you want to handle an array, and comma is the joint symbol'. But for now, I care more about existing code... people who upgrade with subsequence version of |
Yes, existing code is important, but firstly, notation with brackets already correctly parsed, and inconsistence between stringify of array with single item and with multiple items can cause some real errors (i got one and create this PR). It can be major version update, yes. |
I think your concern is more on If would it fix your case? Also, I check the I'm still not comfortable with add brackets as default. It would cause much confusion with the result of using It leave the last question, how we treat parse with The expected answer I believe is |
This, this is expected result for me and my teammates |
So to sum up - the confusion is around when |
The confusion is around when comma is true, and there is no way to determine that value was array with single element, after |
Agreed - the way to convey that it's an array is to add brackets on the end of the key, as asks the OP. This would be a breaking change at this point, but we could add another option (or make |
Instead of changing the default behavior of and for this issue case, an example would be Do I sum right? |
either that, or |
|
So, what should I do now if I want to preserve my one-element Array? Is there any workaround? As @daggerjames suggests, we need to add brackets on the end of the key, am I right? Here is my repl.it, const qs = require('qs');
const stringifyQuery = (query) => {
const cloneQuery = { ...query };
// add square brackkets to identify array with single value
Object.keys(cloneQuery).forEach((item) => {
if (
Array.isArray(cloneQuery[item]) &&
cloneQuery[item].length === 1 &&
!item.includes('[]')
) {
cloneQuery[`${item}[]`] = cloneQuery[item];
delete cloneQuery[item];
}
});
return qs.stringify(cloneQuery, {
encode: false,
indices: false,
arrayFormat: 'comma',
});
};
let parseQuery = (query) =>
qs.parse(query, {
comma: true,
});
const a = {des: ['hanoi']};
const b = {des: ['hanoi','hai phong']};
console.log(stringifyQuery(a)); // des[]=hanoi
console.log(stringifyQuery(b)); // des=hanoi,hai phong
console.log(parseQuery(stringifyQuery(a))); // { des: [ 'hanoi' ] }
console.log(parseQuery(stringifyQuery(b))); // { des: [ 'hanoi', 'hai phong' ] }
Is there any way shorter? Thank |
Two years of opening this issue and yet no solution?
No solution so far? |
This comment has been minimized.
This comment has been minimized.
@Mohamadhassan98 "no solution" because nobody's submitted a PR that fixes it. Anyone who wants to send a PR would be most appreciated. |
Howdy, folks that care about the My intuition is that what you want most is either to be able to round-trip things between parse and stringify (which requires If the latter, can folks please describe exactly what they're using and what format it requires? If it supports what |
Hi. With new
comma
array format,stringify
of array with single value will produce following result:So, on
parse
stage, parsing will produce single value.Maybe add square brackets to parameter name, when
comma
array format is used?Currently, parsing of the following query string
foo[]=1,2
working as expectedThe text was updated successfully, but these errors were encountered: