Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT
18.1. URLSearchParams from scratch
Medium
Topics
This question is in
18. URLs, storage and safe HTMLEvery filter, search box and paginated list keeps its state in the query string: ?q=café&tag=new&tag=sale&page=2. Reading it by hand with split("=") works until a value holds a +, a %, an = or an accented letter. The browser's URLSearchParams follows exact rules for all of these (the application/x-www-form-urlencoded format).
Write a SearchParams class in search-params.js that reads and writes query strings with the same rules as the built-in, for every case in the requirements below.
Signature
class SearchParams {
constructor(init) // init: a query string, with or without "?"; missing means empty
get(name) // the first value for name, or null
getAll(name) // an array of every value for name, in order
has(name) // true or false
append(name, value) // adds a pair at the end
set(name, value) // replaces, see requirement 12
delete(name) // removes every pair with that name
toString() // the pairs as a query string, without "?"
}Requirements
- The constructor removes one leading
?(only one), splits the rest at&, and skips empty pieces. - Each piece splits at its first
=: the name is before it, the value after it. A piece without=has the value"". +in a name or value decodes to a space;%2Bdecodes to+.%followed by two hex digits decodes to that byte; the bytes are read as UTF-8 (%C3%A9isé).- A
%that is not followed by two hex digits stays as it is (%zz,%4, a final%). Nothing throws. - Bytes that are not valid UTF-8 become
U+FFFD; a byte order mark at the start of a name or value is kept. getreturns the first value ornull.getAllreturns a new array every call ([]when the name is missing).toStringwritesname=valuepairs joined by&, in order, a missing value asname=.- In
toString, a space becomes+; letters, digits and* - . _stay; every other byte of the UTF-8 encoding becomes%XXwith upper-case hex. - Names and values are converted with
String(); a lone surrogate becomesU+FFFD. appendadds at the end and keeps duplicates.setgives the first pair with the name the new value, in its place, and removes the other pairs with that name; with no such pair, it appends.deleteremoves every pair with the name;hassays whether one is left.- Parsing the output of
toStringgives back the same pairs. - It does not use the built-in
URLSearchParamsorURL.
Examples
const params = new SearchParams("?q=caf%C3%A9+au+lait&tag=new&tag=sale&flag");
params.get("q"); // "café au lait"
params.getAll("tag"); // ["new", "sale"]
params.get("flag"); // ""
params.get("page"); // nullconst params = new SearchParams("a=1&b=2&a=3");
params.set("a", "x y");
params.append("c", "50%");
params.toString(); // "a=x+y&b=2&c=50%25"new SearchParams("x=100%&y=%zz&z=1=2").get("x"); // "100%" (not an error)
new SearchParams("z=1=2").get("z"); // "1=2"
new SearchParams().toString(); // ""Environment
- Plain JavaScript in a browser page; the tests create
new SearchParams(…)fromsearch-params.jsand compare every result with the browser's ownURLSearchParams. - Not allowed:
URLSearchParamsandURL(a test removes them while it runs).TextEncoder,TextDecoder,encodeURIComponentand string methods are fine. - Out of scope: building from an object or an array of pairs,
sort,size, iteration (entries,forEach).
Hints
- A query can hold
éas%C3%A9: two escapes for one character. At what level, characters or bytes, should the decoding happen? - If you replace
+after percent-decoding, what happens to a value that was%2B? encodeURIComponentis close fortoString. Which characters does it treat differently?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it
Back to the question
Loading the editor…