ljharb / qs

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

Problem with arrayFormat: "comma" #490

Closed dyakonovr closed 6 months ago

dyakonovr commented 6 months ago

Hi all. In my task I have to sparse the following object:

const object = {
  filters: {
    categories: [1, 2, 3, 4, 5],
    rating: {
      min: 2,
    },
    price: {
      min: 170,
      max: 500
    }
  },
  page: 7,
  perPage: 9,
  searchTerm: "Some text and another text"
};

The actual problem: if you specify arrayFormat: "comma" parameter for such object in .stringify(), everything will be fine with url, but when .parse() you will get a string from which you forgot to make an array via .split(',').

What I get after .parse():

{
  "filters": {
    "categories": "1,2,3,4,5",
    "rating": {
      "min": "2"
    },
    "price": {
      "min": "170",
      "max": "500"
    }
  },
  "page": "7",
  "perPage": "9",
  "searchTerm": "Some text and another text"
}

Please fix this problem, thanks.

Link to codepen: https://codepen.io/dyakonovr/pen/dyQawze?editors=1111

ljharb commented 6 months ago

That’s the entire reason for the commaRoundTrip option, which the readme and your #489 indicate you’re aware of.

ayuka-bg commented 5 months ago

have a head pain today with it 😂

solved by encode: false in stringify that's example - https://codepen.io/ayuka-bg/pen/qBwZwKm?editors=0011

res1 is default, res2 and res3 differ only in encode: false

parseArrayValue uses ',' symbol, it would be cool, if it uses %2C as delimiter too (mb commaSymbol: ',' | '%2C' as parameter)

don't know this is bug or not, but this is well for me (but not for project)