@@ -10,6 +10,14 @@ defmodule ComponentsGuideWeb.WebStandards.Live.URL do
10
10
URI . parse ( raw_url )
11
11
end
12
12
13
+ def read ( % State { } = state , :query ) do
14
+ % URI { query: query } = to_url ( state )
15
+ case query do
16
+ "" -> ""
17
+ value -> "?" <> value
18
+ end
19
+ end
20
+
13
21
def to_url_string ( % State { raw_url: raw_url } ) do
14
22
URI . parse ( raw_url ) |> URI . to_string ( )
15
23
end
@@ -51,10 +59,15 @@ defmodule ComponentsGuideWeb.WebStandards.Live.URL do
51
59
52
60
def add_new_query ( % State { } = state ) do
53
61
url = to_url ( state )
54
- url = put_in ( url . query , url . query <> "&a=b" )
62
+ # url = put_in(url.query, url.query <> "&a=b")
63
+ # url = update_in(url.query, fn query_string -> add_query_params_to(query_string, "a=b") end)
64
+ url = update_in ( url . query , & add_query_params_to ( & 1 , "a=b" ) )
55
65
put_in ( state . raw_url , url |> URI . to_string ( ) )
56
66
end
57
67
68
+ defp add_query_params_to ( "" , new_pair ) , do: new_pair
69
+ defp add_query_params_to ( query_string , new_pair ) , do: query_string <> "&" <> new_pair
70
+
58
71
def change_query_vars ( % State { } = state , query_vars ) do
59
72
url = to_url ( state )
60
73
url = put_in ( url . query , URI . encode_query ( query_vars ) )
@@ -71,7 +84,7 @@ defmodule ComponentsGuideWeb.WebStandards.Live.URL do
71
84
<form phx-change=change>
72
85
73
86
<pre class="p-4 my-2" style="color: #d6deeb;">
74
- <code><span class="text-green-400"><%= State.to_url(@state).scheme %></span>://<span class="text-yellow-400"><%= State.to_url(@state).host %></span><span class="text-orange-400"><%= State.to_url(@state).path %></span><span class="text-indigo-400">? <%= State.to_url( @state). query %></span></code>
87
+ <code><span class="text-green-400"><%= State.to_url(@state).scheme %></span>://<span class="text-yellow-400"><%= State.to_url(@state).host %></span><span class="text-orange-400"><%= State.to_url(@state).path %></span><span class="text-indigo-400"><%= @state |> State.read(: query) %></span></code>
75
88
</pre>
76
89
77
90
<div class="flex flex-col space-y-4">
@@ -111,8 +124,10 @@ defmodule ComponentsGuideWeb.WebStandards.Live.URL do
111
124
112
125
</form>
113
126
127
+ <h2>Live code snippets</h2>
128
+
114
129
<section aria-labelledby=javascript-url-heading>
115
- <h2 id=javascript-url-heading>JavaScript’s <code>URL</code></h2 >
130
+ <h3 id=javascript-url-heading>JavaScript’s <code>URL</code></h3 >
116
131
117
132
<pre class="language-js" phx-hook=PreCode><code>const url = new URL(
118
133
'<%= @state |> State.to_url() |> URI.to_string() %>'
@@ -131,20 +146,53 @@ defmodule ComponentsGuideWeb.WebStandards.Live.URL do
131
146
</section>
132
147
133
148
<section aria-labelledby=swift-url-heading>
134
- <h2 id=swift-url-heading>Swift’s <code>URL</code></h2 >
149
+ <h3 id=swift-url-heading>Swift’s <code>URL</code></h3 >
135
150
136
151
<pre class="language-swift" phx-hook=PreCode><code>let url = URL(
137
152
string: "<%= @state |> State.to_url() |> URI.to_string() %>"
138
153
)!
139
- url.scheme // Optional(" <%= State.to_url(@state).scheme %>")
140
- url.host // Optional(" <%= State.to_url(@state).host %>")
154
+ url.scheme // <%= State.to_url(@state).scheme |> format(:swift_optional) %>
155
+ url.host // <%= State.to_url(@state).host |> format(:swift_optional) %>
141
156
url.path // "<%= State.to_url(@state).path %>"
142
- url.query // Optional("<%= State.to_url(@state).query %>")
157
+ url.query // <%= State.to_url(@state).query |> format(:swift_optional) %>
158
+ </code></pre>
159
+ </section>
160
+
161
+ <section aria-labelledby=golang-url-heading>
162
+ <h3 id=golang-url-heading>Golang’s <code>net/url</code></h3>
163
+
164
+ <pre class="language-go" phx-hook=PreCode><code>import (
165
+ "log"
166
+ "net/url"
167
+ )
168
+
169
+ exampleURL, err := url.Parse(
170
+ "<%= @state |> State.to_url() |> URI.to_string() %>"
171
+ )
172
+ if err != nil {
173
+ log.Fatal(err)
174
+ }
175
+ exampleURL.Scheme // "<%= State.to_url(@state).scheme %>"
176
+ exampleURL.Host // "<%= State.to_url(@state).host %>"
177
+ exampleURL.Path // "<%= State.to_url(@state).path %>"
178
+
179
+ exampleURL.RawQuery // "<%= State.to_url(@state).query %>"
180
+ query, err := url.ParseQuery(exampleURL.RawQuery)
181
+ if err != nil {
182
+ log.Fatal(err)
183
+ }
184
+ <%= for {key, value} <- State.get_query_vars(@state) do
185
+ "query.Get(\" #{key}\" ) // \" #{value}\" "
186
+ end |> Enum.join("\n") %>
143
187
</code></pre>
144
188
</section>
145
189
"""
146
190
end
147
191
192
+ defp format ( nil , :swift_optional ) , do: "nil"
193
+ defp format ( "" , :swift_optional ) , do: "nil"
194
+ defp format ( value , :swift_optional ) , do: "Optional(\" #{ value } \" )"
195
+
148
196
def handle_event (
149
197
"change" ,
150
198
changes = % {
0 commit comments