medialize / URI.js

Javascript URL mutation library
http://medialize.github.io/URI.js/
MIT License
6.26k stars 475 forks source link

Possible Issue with query string de-duping #211

Open dannypurcell opened 9 years ago

dannypurcell commented 9 years ago

Thank you for releasing this fine project for all of us to benefit from!

While adjusting a page url with removeSearch() I noticed a slightly surprising side-effect. I thought it worth posting incase this was not the intended behavior.

Removing a key from the query string appears to also have the effect of removing duplicate entries of other keys in the query string.

> var testURI = URI("http://localhost:8080/test?test1=a&test2=b&test1=a")
 undefined
> testURI.toString()
"http://localhost:8080/test?test1=a&test2=b&test1=a"
> testURI.removeSearch("")
b {_string: "http://localhost:8080/test?test1=a&test2=b&test1=a", _parts: Object, _deferred_build: true, build: function, clone: function…}
> testURI.toString()
"http://localhost:8080/test?test1=a&test2=b"
rodneyrehm commented 9 years ago

I'm not sure I'd go as far as calling this a bug, but it certainly is not expected behavior either. I'll leave this issue open for the community to discuss.

s0ph1e commented 8 years ago

Hi guys I faced with similar issue and I believe it is a bug.

Example 1

var location = new URI('http://example.com/?a=2&a=2');
location.addSearch('b', '3');
console.log(location.toString()); 
// Actual result: http://example.com/?a=2&b=3
// Expected result: http://example.com/?a=2&a=2&b=3

I do not expect that on adding new param something will be removed.

Example 2

var location = new URI('http://example.com/?a=2&a=2');
var query = location.search(true);
console.log(query);     // { a: [ '2', '2' ] }
location.search(query); 
console.log(location.toString());
// Actual result: http://example.com/?a=2
// Expected result: http://example.com/?a=2&a=2

I found that there is duplicateQueryParameters param which is false by default. But seems it works only on set query, not on get. When I receive query object using .search(true) (it contains duplicated) I expect that passing exactly the same object without modifications to .search(obj) will give me the same url, but it returns different.

rodneyrehm commented 8 years ago

You can set URI.duplicateQueryParameters = true to have this behavior globally, or use loc.duplicateQueryParameters(true) to make it a per instance thing. The default is false for historic reasons and should probably be changed to true in the next major release

s0ph1e commented 8 years ago

Yep, with duplicateQueryParameters = true everything works as expected. Thanks :)