medialize / URI.js

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

Query param with an empty value #384

Closed AlekseyLeshko closed 5 years ago

AlekseyLeshko commented 5 years ago

Thanks for the awesome library!

Sometimes I need to add a query, but I don’t know if exists value. For example

const epicUrlBuilder = (foo, bar) => URI().addQuery('foo', foo).addQuery('bar', bar)
epicUrlBuilder(42).query() === "foo=42&bar" // not cool
epicUrlBuilder(42).query() === "foo=42" // it’s cool

Yes, of course, I could implement a custom function with this logic. I think it would be cool if this feature was included in the library.

Maybe update readable or create a new method?

What are you thinking about it?

rodneyrehm commented 5 years ago

Hey there,

sorry for the late reply.

The docs show the following example:

uri.addSearch("no-value");

which is technically the same as

uri.addSearch("no-value", undefined);

and therefore the defined behavior of that function.

addSearch() accepts an object, which in your case could be used to rewrite epicUrlBuilder() so that it only adds values that are not undefined.