Skip to content

Commit 960f607

Browse files
authored
Add URLSearchParams.prototype.sort()
This method is added to increase cache hits when making requests. It’s opt-in as the order of code points in a URL’s query is significant by default. It’s up to applications to decide if name order is not significant for them. Tests: web-platform-tests/wpt#4531. Fixes #26.
1 parent 3fe9696 commit 960f607

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

url.bs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2735,6 +2735,9 @@ interface URLSearchParams {
27352735
sequence<USVString> getAll(USVString name);
27362736
boolean has(USVString name);
27372737
void set(USVString name, USVString value);
2738+
2739+
void sort();
2740+
27382741
iterable<USVString, USVString>;
27392742
stringifier;
27402743
};
@@ -2832,6 +2835,11 @@ method, when invoked, must return the value of the first name-value pair whose n
28322835
method, when invoked, must return the values of all name-value pairs whose name is <var>name</var>,
28332836
in <a for=URLSearchParams>list</a>, in list order, and the empty sequence otherwise.
28342837

2838+
<p>The
2839+
<dfn method for=URLSearchParams><code>has(<var>name</var>)</code></dfn>
2840+
method, when invoked, must return true if there is a name-value pair whose name is <var>name</var>
2841+
in <a for=URLSearchParams>list</a>, and false otherwise.
2842+
28352843
<p>The
28362844
<dfn method for=URLSearchParams><code>set(<var>name</var>, <var>value</var>)</code></dfn>
28372845
method, when invoked, must run these steps:
@@ -2847,10 +2855,37 @@ method, when invoked, must run these steps:
28472855
<li><p>Run the <a for=URLSearchParams>update steps</a>.
28482856
</ol>
28492857

2850-
<p>The
2851-
<dfn method for=URLSearchParams><code>has(<var>name</var>)</code></dfn>
2852-
method, when invoked, must return true if there is a name-value pair whose name is <var>name</var>
2853-
in <a for=URLSearchParams>list</a>, and false otherwise.
2858+
<hr>
2859+
2860+
<div class=example id=example-searchparams-sort>
2861+
<p>It can be useful to sort the name-value pairs in a {{URLSearchParams}} object, in particular to
2862+
increase cache hits. This can be accomplished simply through invoking the
2863+
{{URLSearchParams/sort()}} method:
2864+
2865+
<pre><code class=lang-javascript>
2866+
const url = new URL("https://example.org/?q=🏳️‍🌈&amp;key=e1f7bc78");
2867+
url.searchParams.sort();
2868+
url.search; // "?key=e1f7bc78&amp;q=%F0%9F%8F%B3%EF%B8%8F%E2%80%8D%F0%9F%8C%88"</code></pre>
2869+
2870+
<p>To avoid altering the original input, e.g., for comparison purposes, construct a new
2871+
{{URLSearchParams}} object:
2872+
2873+
<pre><code class=lang-javascript>
2874+
const sorted = new URLSearchParams(url.search)
2875+
sorted.sort()</code></pre>
2876+
</div>
2877+
2878+
<p>The <dfn method for=URLSearchParams><code>sort()</code></dfn> method, when invoked, must run
2879+
these steps:
2880+
2881+
<ol>
2882+
<li><p>Sort all name-value pairs, if any, by their names. Sorting must be done by comparison of
2883+
code units. The relative order between name-value pairs with equal names must be preserved.
2884+
2885+
<li><p>Run the <a for=URLSearchParams>update steps</a>.
2886+
</ol>
2887+
2888+
<hr>
28542889

28552890
<p>The <a>value pairs to iterate over</a> are the
28562891
<a for=URLSearchParams>list</a> name-value pairs with the key being
@@ -2919,11 +2954,13 @@ Geoff Richards,
29192954
Glenn Maynard,
29202955
Henri Sivonen,
29212956
Ian Hickson,
2957+
Ilya Grigorik,
29222958
Italo A. Casas,
29232959
Jakub Gieryluk,
29242960
James Graham,
29252961
James Manger,
29262962
James Ross,
2963+
Jeffrey Posnick,
29272964
Joe Duarte,
29282965
Joshua Bell,
29292966
Jxck,
@@ -2957,6 +2994,7 @@ Sven Uhlig,
29572994
Tab Atkins,
29582995
吉野剛史 (Takeshi Yoshino),
29592996
Tantek Çelik,
2997+
Tiancheng "Timothy" Gu,
29602998
Tim Berners-Lee,
29612999
簡冠庭 (Tim Guan-tin Chien),
29623000
Titi_Alone,

0 commit comments

Comments
 (0)