-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Creation of URLSearchParams from Object/Map #27
Comments
Yeah probably just match the accepted types in https://fetch.spec.whatwg.org/#headers-class. Although there is no WebIDL spec for this (and thus no bindings in browsers) so it's not that easy. Map guarantees order (insertion order). Objects guarantee order as well. |
OK, point taken about Object/Map order. Although order of query string arguments is theoretically meaningful per RFCs—and thus annoyingly problematic for cache hit ratios—I haven't yet seen any server-side framework that cares about order of names*. Most APIs that parse query string expose it as hash/dictionary/associative array. So I think it wouldn't harm to sort anyway. It's the common case. An extra In an edge case where somebody really needs a specific order, they can iterate and *) usually order of values with the same name is important, i.e. |
I hope that eventually URLSearchParams, Headers, and FormData can all share a bunch of generic infrastructure, so I would not want to sort by default I think... |
See also https://www.w3.org/Bugs/Public/show_bug.cgi?id=27941 by the way. Passing in an |
I created a PR for this. We'll also need tests. I think Gecko would be fairly happy to implement this change. |
https://bugzilla.mozilla.org/show_bug.cgi?id=1330678 |
I made an initial implementation for WebKit. I find the tests lacking, but it's a start. |
I tried to answer your questions in the WebKit bug. Some additional tests landed at web-platform-tests/wpt#4545. Order is specified everywhere as far as I'm aware, but it's a mixture of IDL, JavaScript, and URL that makes it work for the various pieces involved. |
Follow up recent spec addition[1,2] and support sequence<sequence<USVString>> initializers for URLSearchParams. 1 - whatwg/url#27 2 - whatwg/url#175 R=tyoshino,haraken BUG=680531 Review-Url: https://codereview.chromium.org/2725593003 Cr-Commit-Position: refs/heads/master@{#453903}
Currently
URLSearchParams
cannot be directly created from anObject
holding keys and values.Combination of
Object.keys
,Array.reduce
andappend
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 theset
method.i.e.
or
could be equivalent to:
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.
The text was updated successfully, but these errors were encountered: