Skip to content

Commit a237c5f

Browse files
committed
Format download count as e.g. 4M
1 parent 11c7c07 commit a237c5f

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ defmodule ComponentsGuideWeb.ResearchController do
108108
:dl,
109109
[
110110
content_tag(:dt, "Monthly downloads", class: "text-base font-bold"),
111-
content_tag(:dd, "#{downloads_count}")
111+
content_tag(:dd, View.humanize_count(downloads_count))
112112
]
113113
)
114114
])

apps/components_guide_web/lib/components_guide_web/templates/research/index.html.eex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<ul class="list-none flex flex-wrap italic" style="--links-padding: 0.75em">
3232
<li><%= link("react-dom", to: "?q=react-dom") %>
3333
<li><%= link("preact", to: "?q=preact") %>
34+
<li><%= link("vue", to: "?q=vue") %>
3435
</ul>
3536
</dd>
3637
<dt>Concepts</dt>

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,18 @@ defmodule ComponentsGuideWeb.ResearchView do
2222
def humanize_bytes(count) when is_integer(count) do
2323
"#{count} B"
2424
end
25+
26+
def humanize_count(count) when count >= 1_000_000 do
27+
formatted = count / 1_000_000
28+
"#{float_to_string(formatted, decimals: 1)}M"
29+
end
30+
31+
def humanize_count(count) when count >= 1000 do
32+
formatted = count / 1000
33+
"#{float_to_string(formatted, decimals: 1)}K"
34+
end
35+
36+
def humanize_count(count) when is_integer(count) do
37+
"#{count}"
38+
end
2539
end

apps/components_guide_web/test/collected_live_web/views/research_view_test.exs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,62 @@ defmodule ComponentsGuideWeb.ResearchViewTest do
44
alias ComponentsGuideWeb.ResearchView, as: Subject
55

66
describe "humanize_bytes/1" do
7-
defp subject(count) do
8-
Subject.humanize_bytes(count)
9-
end
10-
117
test "0" do
12-
assert subject(0) == "0 B"
8+
assert Subject.humanize_bytes(0) == "0 B"
139
end
1410

1511
test "1" do
16-
assert subject(1) == "1 B"
12+
assert Subject.humanize_bytes(1) == "1 B"
1713
end
1814

1915
test "1023" do
20-
assert subject(1023) == "1023 B"
16+
assert Subject.humanize_bytes(1023) == "1023 B"
2117
end
2218

2319
test "1024" do
24-
assert subject(1024) == "1.0 kB"
20+
assert Subject.humanize_bytes(1024) == "1.0 kB"
2521
end
2622

2723
test "1025" do
28-
assert subject(1025) == "1.0 kB"
24+
assert Subject.humanize_bytes(1025) == "1.0 kB"
2925
end
3026

3127
test "1_000_000" do
32-
assert subject(1_000_000) == "976.6 kB"
28+
assert Subject.humanize_bytes(1_000_000) == "976.6 kB"
3329
end
3430

3531
test "1_048_575" do
36-
assert subject(1_048_575) == "1024.0 kB"
32+
assert Subject.humanize_bytes(1_048_575) == "1024.0 kB"
3733
end
3834

3935
test "1_048_576" do
40-
assert subject(1_048_576) == "1.0 mB"
36+
assert Subject.humanize_bytes(1_048_576) == "1.0 mB"
37+
end
38+
end
39+
40+
describe "humanize_count/1" do
41+
test "0" do
42+
assert Subject.humanize_count(0) == "0"
43+
end
44+
45+
test "1" do
46+
assert Subject.humanize_count(1) == "1"
47+
end
48+
49+
test "999" do
50+
assert Subject.humanize_count(999) == "999"
51+
end
52+
53+
test "1000" do
54+
assert Subject.humanize_count(1000) == "1.0K"
55+
end
56+
57+
test "10,000" do
58+
assert Subject.humanize_count(10_000) == "10.0K"
59+
end
60+
61+
test "1,000,000" do
62+
assert Subject.humanize_count(1_000_000) == "1.0M"
4163
end
4264
end
4365
end

0 commit comments

Comments
 (0)