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

Array with single value is stringified as string when using arrayFormat 'comma'

Open iyedb opened this issue 8 months ago • 1 comments

queryString.stringify({values: ['foo']}, {arrayFormat: 'comma'} ) => values='foo' when parsing back with arrayFormat 'comma', the result is {values: 'foo'}

here is a test to reproduce the bug (the test fails):

it("parses array with a single value bug", () => {
    const q = queryString.stringify(
      { values: ["foo"] },
      { arrayFormat: "comma" },
    );
    console.log(q);
    const parsed = queryString.parse(q, { arrayFormat: "comma" });
    console.log(parsed);
    expect(parsed).toEqual({
      values: ["foo"],
    });
  });

The information that values is an array is lost when stringifying. Workaround: use `arrayFormat: "bracket-separator"

This bug makes the "comma" format unusable.

iyedb avatar Mar 13 '25 02:03 iyedb

@iyedb looks like a duplicate of this issue

nik-webdevelop avatar Mar 18 '25 14:03 nik-webdevelop