Pairing on a parser
We parsed query strings by hand for an afternoon, then deleted it and used URLSearchParams. Notes from both sides of the keyboard.
Lene and I paired on stummel’s query parsing for an afternoon. We wrote 60 lines, found four bugs in them, and replaced all of it with URLSearchParams. The afternoon was not wasted: we now know which of those bugs the platform handles.
What the 60 lines got wrong
+is a space in a query string, not a plus.- A key without
=is a key with an empty value. - The same key twice is a list, not the last value.
- Percent-encoded UTF-8 needs decoding as a whole, not byte by byte.
const params = new URL(req.url).searchParams;
params.getAll('tag'); // ['ts', 'node'] for ?tag=ts&tag=nodeThe fastest way to learn what a standard library does is to write it badly first, with someone watching.
Lene wrote that sentence on the whiteboard and we left it there.
subscribe
New entries and the next chapter by mail, nothing else.