Skip to content
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

arrayFormat: "comma" behavior inconsistency across query-string versions when parsing arrays with commas #399

Open
sandeep-exe opened this issue Nov 20, 2024 · 0 comments

Comments

@sandeep-exe
Copy link

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:

  1. Why do these differences exist between the versions?
  2. What should be the expected behavior?
  3. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant