Skip to content

Commit b920cc2

Browse files
committed
Improve Swift URL example
1 parent 8578e71 commit b920cc2

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

apps/components_guide_web/lib/components_guide_web/templates/web_standards/url.html.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ ComponentsGuideWeb.WebStandards.Live.URL
88

99
## JavaScript’s `URL`
1010

11-
```js
12-
const url = new URL('https://example.org/songs?first=20&sortBy=releaseDate');
13-
url.protocol; // 'https:'
14-
url.hostname; // 'example.org'
15-
url.origin; // 'https://example.org'
16-
url.pathname; // '/songs'
17-
```
18-
1911
```js
2012
const root = new URL('/');
2113
```

apps/components_guide_web/lib/components_guide_web/templates/web_standards/url_live.ex

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,37 @@ defmodule ComponentsGuideWeb.WebStandards.Live.URL do
111111
112112
</form>
113113
114+
<section aria-labelledby=javascript-url-heading>
115+
<h2 id=javascript-url-heading>JavaScript’s <code>URL</code></h2>
116+
114117
<pre class="language-js" phx-hook=PreCode><code>const url = new URL(
115118
'<%= @state |> State.to_url() |> URI.to_string() %>'
116119
);
117120
url.protocol; // '<%= State.to_url(@state).scheme %>:'
118121
url.host; // '<%= State.to_url(@state).host %>'
122+
url.origin; // '<%= State.to_url(@state).scheme %>://<%= State.to_url(@state).host %>'
119123
url.pathname; // '<%= State.to_url(@state).path %>'
120124
121125
url.search; // '?<%= State.to_url(@state).query %>'
122126
const query = new URLSearchParams(url.search);
123127
<%= for {key, value} <- State.get_query_vars(@state) do
124-
"query.get('#{key}'); // '#{value}'\n"
125-
end %>
128+
"query.get('#{key}'); // '#{value}'"
129+
end |> Enum.join("\n") %>
130+
</code></pre>
131+
</section>
132+
133+
<section aria-labelledby=swift-url-heading>
134+
<h2 id=swift-url-heading>Swift’s <code>URL</code></h2>
135+
136+
<pre class="language-swift" phx-hook=PreCode><code>let url = URL(
137+
string: "<%= @state |> State.to_url() |> URI.to_string() %>"
138+
)!
139+
url.scheme // Optional("<%= State.to_url(@state).scheme %>")
140+
url.host // Optional("<%= State.to_url(@state).host %>")
141+
url.path // "<%= State.to_url(@state).path %>"
142+
url.query // Optional("<%= State.to_url(@state).query %>")
126143
</code></pre>
144+
</section>
127145
"""
128146
end
129147

0 commit comments

Comments
 (0)