New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update search/query instead of add #64

Closed
techniq opened this Issue Jan 17, 2013 · 4 comments

Comments

Projects
None yet
2 participants
@techniq

techniq commented Jan 17, 2013

I've been trying to find a way to update an existing search argument if it exists, or have it added if it doesn't exist.

Something like:

var uri = URI("http://localhost?foo=bar")
uri.updateSearch("foo", "baz");
uri.toString(); => "http://localhost?foo=baz"

Is this possible with the current API? It seems addSearch will only append another argument, for example:

var uri = URI("http://localhost?foo=bar")
uri.addSearch("foo", "baz");
uri.toString(); => "http://localhost?foo=bar&foo=baz"
@techniq

This comment has been minimized.

Show comment
Hide comment
@techniq

techniq Jan 17, 2013

I'm using this as a workaround, but it would be nice if there was an exposed function for something like this

if (uri.query().indexOf(key + "=") != -1) {
    uri.removeQuery(key);
}

uri.addQuery(key, value);

techniq commented Jan 17, 2013

I'm using this as a workaround, but it would be nice if there was an exposed function for something like this

if (uri.query().indexOf(key + "=") != -1) {
    uri.removeQuery(key);
}

uri.addQuery(key, value);
@rodneyrehm

This comment has been minimized.

Show comment
Hide comment
@rodneyrehm

rodneyrehm Jan 17, 2013

Member

you could always use the wuery map:

var data = uri.query(true);
data.foo = "baz";
uri.query(data);

I see how that is inconvenient, though… without having the matter thought through, accepting a callback function would be nice:

uri.query(function(data) {
  data.foo = "bar";
});
Member

rodneyrehm commented Jan 17, 2013

you could always use the wuery map:

var data = uri.query(true);
data.foo = "baz";
uri.query(data);

I see how that is inconvenient, though… without having the matter thought through, accepting a callback function would be nice:

uri.query(function(data) {
  data.foo = "bar";
});
@techniq

This comment has been minimized.

Show comment
Hide comment
@techniq

techniq Jan 18, 2013

I overlooked the uri.query(true) API, which is much cleaner than my indexOf/removeQuery way. It would be nice if there was a 1 line call to set/update instead of just set/append. Not sure if any of these are better APIs

addQuery("foo", "bar", true)
addQuery({foo, "bar"}, true)
setQuery("foo", "bar")

The callback would be nice to have as well, although I think a compliment to addQuery/removeQuery would be best.

techniq commented Jan 18, 2013

I overlooked the uri.query(true) API, which is much cleaner than my indexOf/removeQuery way. It would be nice if there was a 1 line call to set/update instead of just set/append. Not sure if any of these are better APIs

addQuery("foo", "bar", true)
addQuery({foo, "bar"}, true)
setQuery("foo", "bar")

The callback would be nice to have as well, although I think a compliment to addQuery/removeQuery would be best.

@techniq

This comment has been minimized.

Show comment
Hide comment
@techniq

techniq commented Feb 12, 2013

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment