ljharb / qs

A querystring parser with nesting support
BSD 3-Clause "New" or "Revised" License
8.47k stars 731 forks source link

Trouble with commaRoundTrip #489

Closed dyakonovr closed 6 months ago

dyakonovr commented 6 months ago

Hi all. Please update the following fragment from the documentation:

"You may use the arrayFormat option to specify the format of the output array:

qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' })
// 'a[0]=b&a[1]=c'
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' })
// 'a[]=b&a[]=c'
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' })
// 'a=b&a=c'
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'comma' })
// 'a=b,c'

Note: when using arrayFormat set to 'comma', you can also pass the commaRoundTrip option set to true or false, to append [] on single-item arrays, so that they can round trip through a parse."

It is misleading. I was trying to convert an object of the form:

const object = {
    categories: [1],
    rating: {
      min: 17,
      max: 4
    },
    price: {
      min: 120 
    }
  };

to a url string. All my attempts to work with this library in a project before had failed, so I decided to work with the library in codepen. I tried converting the object to a url and got the result I wanted, but to do so I used a configuration object that had the fields arrayFormat: "comma" and (as the documentation says to help it define the array) commaRoundTip: true. Everything worked fine on codepen, but when I moved the code into my project, TypeScript told me that the commaRoundTip: true field didn't exist in the configuration object.

Tnx

ljharb commented 6 months ago

If typescript told you that, typescript is wrong. Make sure you’ve used the latest version of both qs and @types/qs.

If it still isn’t working, then the DT package needs updating, but that isn’t relevant to this JS repo.