riot / route

Simple isomorphic router
MIT License
212 stars 40 forks source link

Query function: Multiple parameters with same key #99

Closed struct78 closed 4 years ago

struct78 commented 7 years ago

Hi there,

I noticed a small issue with route.query() when using a URL that has multiple parameters with the same key e.g. /locate?services=10&services=359&services=1248, the property with that key in the query object only has the last value. e.g.

{
    services: 1248
}

I've solved this temporarily in my codebase by overriding the query function with the following, which simply checks if a key/value pair already exists, and if it does, it concatenates the current value and the new value into a comma-separated string.

route.query = function() {
  var q = {};
  var href = loc.href || current;
  href.replace(/[?&](.+?)=([^&]*)/g, function(_, k, v) => {
          if (q[k]) {
            q[k] = [q[k], v].join(',');
          }
          else {
            q[k] = v;
          }
        });
  return q
};
GianlucaGuarini commented 4 years ago

Closing this issue because it's related to an old router version. Please update to the latest @riotjs/route version if you can.