Deviopath › JavaScript

Parse a Nested Query String

Hard · Parsing

Write a function parseQuery that turns a URL query string into an object.

- The string may start with "?", which is ignored. An empty string gives {}.

- Pairs are separated by "&". Key and value are separated by the first "=". A pair without "=" has the value "".

- Values stay strings. Replace every "+" with a space, then decode with decodeURIComponent (so %2B becomes a real "+"). Keys are used as written, without decoding.

- Brackets create nested objects: user[name]=Ana gives { user: { name: "Ana" } } (any depth).

- Empty brackets [] at the end of a key append the value to an array: tags[]=a&tags[]=b gives { tags: ["a", "b"] }.

- If the same normal key appears twice, the last value wins.

Next: Promise Pool With Concurrency Limit