whatwg / xhr

XMLHttpRequest Standard
https://xhr.spec.whatwg.org/
Other
315 stars 131 forks source link

`FormData` `has()` should include a second param for matching the value like `URLSearchParams` #392

Open knowler opened 3 weeks ago

knowler commented 3 weeks ago

What problem are you trying to solve?

FormData objects can have multiple entries with the same key. The FormData has() method is useful checking a key exists in the object, however, when there are multiple keys with the same name it is less useful since in that case you don’t just care about the key, but also if a key and value are present.

What solutions exist today?

Today, it’s not terribly difficult to achieve this, but it does require getting all of the values as an array and then using Array methods to check if the value exists.

For example, where formData is a FormData object:

formData.getAll("fruit").includes("tomato");

How would you solve it?

The URLSearchParams has() method supports a second parameter for matching the value of the named param. For example, where searchParams is a URLSearchParams object:

searchParams.has("fruit", "tomato"); // returns a boolean if there’s a matching key with the specified value

This would be useful for FormData as well. So, again where formData is a FormData object and the has() method supports a second parameter for matching the name and value in the object:

formData.has("fruit", "tomato"); // returns a boolean if there’s a matching key with the specified value

Additionally, this would help authors as there would be increased API parity between FormData and URLSearchParams.

Anything else?

No response

annevk commented 3 weeks ago

Seems reasonable, we should do the same for delete() as per https://github.com/whatwg/url/commit/bfb9157186c237078cb1ac4998607d88242abe35.