Skip to content

URLSearchParams append/set: variadic values argument? #762

Open
@bathos

Description

@bathos

When assembling URLs with parameters where the same key is used with multiple values, I’ve often needed to set those multiple values at once and found the URLSearchParams API surface slightly clunky for this. I’d mentioned this in another issue and @annevk pointed out that this could be facilitated backwards-compatibly without any new API surface just by making the set and append operations variadic. Instead of:

interface URLSearchParams {
  /* ... */
  undefined append(USVString name, USVString value);
  undefined set(USVString name, USVString value);
};

They would be:

interface URLSearchParams {
  /* ... */
  undefined append(USVString name, USVString ...values);
  undefined set(USVString name, USVString ...values);
};

An example of what this change facilitates would be the filtering or mapping of parameters as may occur when applying application-specific URL canonicalization rules or setting parameters based on the current state of a search form. Instead of:

let foos = usp.getAll("foo").filter(isFooValue);

usp.delete("foo"):

for (let foo of foos) usp.append("foo", foo);

One could write:

usp.set("foo", ...usp.getAll("foo").filter(isFooValue));

Is there interest from others in this change? It’s a minor ergonomics improvement, but I’d guess it’s also pretty low-hanging from an implementer POV.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions