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

js
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

MethodReturnsNotes
append(n, v)undefinedAdds a pair at the end of the list.
delete(n)undefinedRemoves every pair whose name matches n.
delete(n, v)undefinedRemoves 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)booleantrue if any pair with name n exists.
has(n, v)booleantrue if any pair has both name n and value v.
set(n, v)undefinedReplaces the first matching pair's value and drops later duplicates; appends if absent.
sort()undefinedStable sort by name in Unicode code-unit order.
toString()stringapplication/x-www-form-urlencoded serialisation.

Attributes

NameType
sizeunsigned long

Iteration

URLSearchParams is a pair iterable:
js
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 as cb.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.

js
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

  • URLSearchParams is 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.searchParams is wired when the full WHATWG URL class is also loaded — a standalone URLSearchParams behaves identically without a backing URL.

See also