Skip to content

Commit 11c7c07

Browse files
committed
Present kB and mB
1 parent b910440 commit 11c7c07

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

apps/components_guide_web/lib/components_guide_web/controllers/research_controller.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule ComponentsGuideWeb.ResearchController do
33
use Phoenix.HTML
44

55
alias ComponentsGuide.Research.Spec
6+
alias ComponentsGuideWeb.ResearchView, as: View
67

78
def index(conn, %{"q" => query}) do
89
query = query |> String.trim()
@@ -69,9 +70,9 @@ defmodule ComponentsGuideWeb.ResearchController do
6970
:dl,
7071
[
7172
content_tag(:dt, "Minified", class: "text-base font-bold"),
72-
content_tag(:dd, "#{size}"),
73+
content_tag(:dd, View.humanize_bytes(size)),
7374
content_tag(:dt, "Minified + Gzipped", class: "text-base font-bold"),
74-
content_tag(:dd, "#{size_gzip}"),
75+
content_tag(:dd, View.humanize_bytes(size_gzip)),
7576
content_tag(:dt, "Emerging 3G (50kB/s)", class: "text-base font-bold"),
7677
content_tag(:dd, "#{emerging_3g_ms}ms")
7778
],

apps/components_guide_web/lib/components_guide_web/views/research_view.ex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,20 @@ defmodule ComponentsGuideWeb.ResearchView do
66
Content!
77
"""
88
end
9+
10+
defdelegate float_to_string(f, options), to: :erlang, as: :float_to_binary
11+
12+
def humanize_bytes(count) when count >= 1024 * 1024 do
13+
megabytes = count / (1024 * 1024)
14+
"#{float_to_string(megabytes, decimals: 1)} mB"
15+
end
16+
17+
def humanize_bytes(count) when count >= 1024 do
18+
kilobytes = count / 1024
19+
"#{float_to_string(kilobytes, decimals: 1)} kB"
20+
end
21+
22+
def humanize_bytes(count) when is_integer(count) do
23+
"#{count} B"
24+
end
925
end

apps/components_guide_web/test/collected_live_web/controllers/page_controller_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ defmodule ComponentsGuideWeb.PageControllerTest do
33

44
test "GET /", %{conn: conn} do
55
conn = get(conn, "/")
6-
assert html_response(conn, 200) =~ "Welcome to Phoenix!"
6+
assert html_response(conn, 200) =~ "Components.Guide"
77
end
88
end

apps/components_guide_web/test/collected_live_web/controllers/text_controller_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ defmodule ComponentsGuideWeb.TextControllerTest do
2727
end
2828

2929
describe "create text" do
30+
@tag :skip
3031
test "redirects to show when data is valid", %{conn: conn} do
3132
conn = post(conn, Routes.text_path(conn, :create), text: @create_attrs)
3233

@@ -46,6 +47,7 @@ defmodule ComponentsGuideWeb.TextControllerTest do
4647
describe "edit text" do
4748
setup [:create_text]
4849

50+
@tag :skip
4951
test "renders form for editing chosen text", %{conn: conn, text: text} do
5052
conn = get(conn, Routes.text_path(conn, :edit, text))
5153
assert html_response(conn, 200) =~ "Edit Text"
@@ -55,6 +57,7 @@ defmodule ComponentsGuideWeb.TextControllerTest do
5557
describe "update text" do
5658
setup [:create_text]
5759

60+
@tag :skip
5861
test "redirects when data is valid", %{conn: conn, text: text} do
5962
conn = put(conn, Routes.text_path(conn, :update, text), text: @update_attrs)
6063
assert redirected_to(conn) == Routes.text_path(conn, :show, text)
@@ -63,6 +66,7 @@ defmodule ComponentsGuideWeb.TextControllerTest do
6366
assert html_response(conn, 200)
6467
end
6568

69+
@tag :skip
6670
test "renders errors when data is invalid", %{conn: conn, text: text} do
6771
conn = put(conn, Routes.text_path(conn, :update, text), text: @invalid_attrs)
6872
assert html_response(conn, 200) =~ "Edit Text"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
defmodule ComponentsGuideWeb.ResearchViewTest do
2+
use ComponentsGuideWeb.ConnCase, async: true
3+
4+
alias ComponentsGuideWeb.ResearchView, as: Subject
5+
6+
describe "humanize_bytes/1" do
7+
defp subject(count) do
8+
Subject.humanize_bytes(count)
9+
end
10+
11+
test "0" do
12+
assert subject(0) == "0 B"
13+
end
14+
15+
test "1" do
16+
assert subject(1) == "1 B"
17+
end
18+
19+
test "1023" do
20+
assert subject(1023) == "1023 B"
21+
end
22+
23+
test "1024" do
24+
assert subject(1024) == "1.0 kB"
25+
end
26+
27+
test "1025" do
28+
assert subject(1025) == "1.0 kB"
29+
end
30+
31+
test "1_000_000" do
32+
assert subject(1_000_000) == "976.6 kB"
33+
end
34+
35+
test "1_048_575" do
36+
assert subject(1_048_575) == "1024.0 kB"
37+
end
38+
39+
test "1_048_576" do
40+
assert subject(1_048_576) == "1.0 mB"
41+
end
42+
end
43+
end

0 commit comments

Comments
 (0)