URLSearchParams is a browser API for parsing and working with URL query strings while preserving duplicate keys and insertion order.
In this question, implement an interview-sized subset of URLSearchParams as a class named URLSearchParamsPolyfill.
Do not call or wrap the native URLSearchParams in your implementation. Build the behavior yourself.
URLSearchParamsPolyfill APIImplement the following APIs on URLSearchParamsPolyfill:
new URLSearchParamsPolyfill([query])Creates an instance from a query string. query defaults to ''.
This question should support:
? being ignored.debug being treated as debug=.+ being treated as a space while parsing.This question is intentionally limited:
params.get(name)Returns the first matching value for name, or null if the name does not exist.
params.getAll(name)Returns all matching values for name in insertion order.
params.has(name)Returns true if at least one pair exists for name, otherwise false.
const params = new URLSearchParamsPolyfill('?a=1&a=2&debug');params.get('a'); // '1'params.getAll('a'); // ['1', '2']params.get('debug'); // ''params.get('missing'); // null
+ and percent-encoded values should be decoded while parsing.
const params = new URLSearchParamsPolyfill('city=New+York¬e=a%26b');params.get('city'); // 'New York'params.get('note'); // 'a&b'
Duplicate keys should be preserved.
const params = new URLSearchParamsPolyfill('tag=js&tag=ts');params.get('tag'); // 'js'params.getAll('tag'); // ['js', 'ts']
new URLSearchParamsPolyfill(query) accepts the following argument:
| Argument | Type | Description |
|---|---|---|
query | string | A query string to parse. Defaults to ''. |
Returns a new URLSearchParamsPolyfill instance.
URLSearchParams is off-limits for this question.console.log() statements will appear here.