Description
I have a use case where I need to parse and stringify an object containing arrays of strings. These arrays may include strings that contain commas (","
) as part of their value. For example:
Example 1:
{
"prop": ["val1,val2", "val3"]
}
Example 2:
{
"prop": ["val1,val2"]
}
Code Example:
const stringified = queryString.stringify(a, {
arrayFormat: "comma",
});
const parsed = queryString.parse(stringified, {
arrayFormat: "comma",
});
console.log({ parsed, stringified });
The output of the above code varies depending on the version of query-string
used.
Version 6.12.1:
For Example 1, the output does not split "val1,val2"
:
{
"parsed": {
"prop": ["val1,val2", "val3"]
},
"stringified": "prop=val1%2Cval2,val3"
}
For Example 2, the output also does not split "val1,val2"
:
{
"parsed": {
"prop": "val1,val2"
},
"stringified": "prop=val1%2Cval2"
}
Version 6.14.1:
For Example 1, the output does not split "val1,val2"
:
{
"parsed": {
"prop": ["val1,val2", "val3"]
},
"stringified": "prop=val1%2Cval2,val3"
}
For Example 2, the output splits "val1,val2"
:
{
"parsed": {
"prop": ["val1", "val2"]
},
"stringified": "prop=val1%2Cval2"
}
Version 9.1.1:
For Example 1, the output does not split "val1,val2"
:
{
"parsed": {
"prop": ["val1,val2", "val3"]
},
"stringified": "prop=val1%2Cval2,val3"
}
For Example 2, the output splits "val1,val2"
:
{
"parsed": {
"prop": ["val1", "val2"]
},
"stringified": "prop=val1%2Cval2"
}
Question:
Version 6.12.1
works best for my use case. However, I want to understand:
- Why do these differences exist between the versions?
- What should be the expected behavior?
- Given that I must use
arrayFormat: "comma"
and cannot switch to another format, how should I handle this issue?
codesandbox: https://codesandbox.io/p/sandbox/query-string-forked-rwvl3y?file=%2Fsrc%2Findex.js