python-hyper/hyperlink

Extend `URL().remove()` to support removing a single key-value pair

alexwlchan opened this issue · 2 comments

I have some URLs that use multiple query parameters to track filters, e.g.:

/documents?filter=apple&filter=banana&filter=coconut

If I parse this with URL(), I can use remove("filter") to remove all the instances of this query parameter, but removing just the "banana" filter is a little more fiddly.

How would you feel about extending remove() to support passing an optional query parameter value to remove, so you only remove that one pair (if it exists)?

Something like:

def remove(self, name, value=None):
    if value is None:
        new_query = ((k, v) for (k, v) in self.query if k != name)
    else:
        new_query = ((k, v) for (k, v) in self.query if k != name and v != name)
    return self.replace(query=new_query)

Sounds good to me! I think I see a bug at the end of the else block there, but I get the picture enough for a PR :)

@alexwlchan care to review #71?