WHATWG URLSearchParams
Elide implements the WHATWG [URL Standard][spec] URLSearchParams global — an ordered list of
(name, value) string pairs serialised as application/x-www-form-urlencoded. All operations
(append, delete, get, getAll, has, set, sort) and the iterator protocols
(entries, keys, values, forEach, default @@iterator) match the spec exactly. A shared
iterator prototype inherits from %IteratorPrototype%, so for-of and spread work unmodified.
[spec]: https://url.spec.whatwg.org/#interface-urlsearchparams
Construction
new URLSearchParams() // empty
new URLSearchParams("?a=1&b=2") // USVString (leading `?` is stripped)
new URLSearchParams([["a", "1"], ["b", "2"]]) // sequence of pairs
new URLSearchParams({ a: "1", b: "2" }) // record<USVString, USVString>Any non-string, non-sequence, non-record value raises TypeError. A sequence element whose
length is not exactly two also raises TypeError, per the spec.
Operations
| Method | Returns | Notes | |
|---|---|---|---|
append(n, v) | undefined | Adds a pair at the end of the list. | |
delete(n) | undefined | Removes every pair whose name matches n. | |
delete(n, v) | undefined | Removes only pairs where both name and value match — spec's optional value argument. | |
get(n) | `string \ | null` | First value whose name matches, or null. |
getAll(n) | string[] | All values in insertion order. | |
has(n) | boolean | true if any pair with name n exists. | |
has(n, v) | boolean | true if any pair has both name n and value v. | |
set(n, v) | undefined | Replaces the first matching pair's value and drops later duplicates; appends if absent. | |
sort() | undefined | Stable sort by name in Unicode code-unit order. | |
toString() | string | application/x-www-form-urlencoded serialisation. |
Attributes
| Name | Type |
|---|---|
size | unsigned long |
Iteration
URLSearchParams is a pair iterable:
for (const [name, value] of new URLSearchParams("a=1&b=2")) {
console.log(name, value);
}entries()— pair iterator (the default).keys()— name-only iterator.values()— value-only iterator.forEach(cb, thisArg?)— imperative iteration; callback is invoked ascb.call(thisArg, value, name, searchParams).
All iterators are live — mutations during traversal (via append, delete, set, or
sort) are observable by subsequent next() calls. forEach walks by index and picks up
later-added pairs mid-loop.
Encoding
Serialisation uses the application/x-www-form-urlencoded percent-encode set: ASCII alphanumeric
plus *, -, ., _ pass through unchanged; space encodes as +; everything else encodes as
UTF-8 bytes in %HH form. Parsing reverses the encoding and converts unpaired UTF-16 surrogates
to U+FFFD per the WHATWG Encoding Standard.
const sp = new URLSearchParams();
sp.append("k", "hello world");
sp.append("unicode", "тест");
sp.toString();
// → "k=hello+world&unicode=%D1%82%D0%B5%D1%81%D1%82"Compatibility notes
URLSearchParamsis installed as a global on every JavaScript realm in Elide.- The class is fully conformant with WHATWG URL §5; behaviour matches Node.js, Deno, and modern browsers.
- Live binding with
URL.searchParamsis wired when the full WHATWGURLclass is also loaded — a standaloneURLSearchParamsbehaves identically without a backing URL.