Closed
Description
Currently URLSearchParams
cannot be directly created from an Object
holding keys and values.
Combination of Object.keys
, Array.reduce
and append
feels like boilderplate given that creation of query strings from objects is a common pattern in popular JS libraries, e.g. jQuery, superagent, node's url.format.
I suggest accepting an Object
, Map
(or anything that is iterable and has string keys) in the constructor and the set
method.
i.e.
URLSearchParams({foo:"bar", baz:"quz"});
or
const tmp = new URLSearchParams();
tmp.set({foo:"bar", baz:"quz"});
could be equivalent to:
const tmp = new URLSearchParams();
tmp.append("baz", "quz");
tmp.append("foo", "bar");
I suggest always sorting the keys, since Object and Map don't guarantee any particular order, but a consistent order is very desirable for improved cacheability of resources.